2012-04-04 9 views
0

Soweit ich das beurteilen kann, wird jede Variable, die ich verwende, nicht verwendet, ohne zuerst einen Wert zu erhalten. Die Nullpointerexzeption passiert im Moment, in dem diese Aktivität beginnt. Entschuldigung, dass ich nicht genauer sein kann.Ich bekomme immer eine NullPointerException aber kann nicht finden, wo ich den Fehler gemacht habe

package pd.wilson.com; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.CheckBox; 
import android.widget.RadioButton; 
import android.widget.RadioGroup; 
import android.widget.TextView; 

public class Page3 extends Activity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.page3); 
     final RadioGroup size = (RadioGroup)findViewById(R.id.RGSize); 
     final RadioGroup crust = (RadioGroup)findViewById(R.id.RGCrust); 
     final RadioButton small = (RadioButton)findViewById(R.id.RBS); 
     final RadioButton medium = (RadioButton)findViewById(R.id.RBM); 
     final RadioButton large = (RadioButton)findViewById(R.id.RBL); 
     final RadioButton regular = (RadioButton)findViewById(R.id.RBRegular); 
     final RadioButton thin = (RadioButton)findViewById(R.id.RBThin); 

     final String sizeS, crustS; 
     if (size.getCheckedRadioButtonId() == small.getId()){ 
      sizeS = "Small"; 
      } 
      else if(size.getCheckedRadioButtonId() == medium.getId()){ 
      sizeS = "Medium"; 
      } 
      else{ 
      sizeS = "Large"; 
      } 
     if (crust.getCheckedRadioButtonId() == regular.getId()){ 
      crustS = "Regular"; 
      } 
      else{ 
      crustS = "Thin"; 
      } 

     TextView size1 = (TextView)findViewById(R.id.tvsize); 
     size1.setText(sizeS); 
     TextView crust1 = (TextView)findViewById(R.id.tvcrust); 
     crust1.setText(crustS); 


      final CheckBox sausage = (CheckBox)findViewById(R.id.cbsausage); 
       int i1 = Ifchecked.gettrue(sausage); 
      final CheckBox mushrooms = (CheckBox)findViewById(R.id.cbmushrooms); 
       int i2 = Ifchecked.gettrue(mushrooms); 
      final CheckBox pepperoni = (CheckBox)findViewById(R.id.cbpepperoni); 
       int i3 = Ifchecked.gettrue(pepperoni); 
      final CheckBox onions = (CheckBox)findViewById(R.id.cbonions); 
       int i4 = Ifchecked.gettrue(onions); 
      final CheckBox ham = (CheckBox)findViewById(R.id.cbham); 
       int i5 = Ifchecked.gettrue(ham); 
      final CheckBox peppers = (CheckBox)findViewById(R.id.cbpeppers); 
       int i6 = Ifchecked.gettrue(peppers); 
      final CheckBox beef = (CheckBox)findViewById(R.id.cbbeef); 
       int i7 = Ifchecked.gettrue(beef); 
      final CheckBox tomatoes = (CheckBox)findViewById(R.id.cbtomatoes); 
       int i8 = Ifchecked.gettrue(tomatoes); 
      final CheckBox bacon = (CheckBox)findViewById(R.id.cbbacon); 
       int i9 = Ifchecked.gettrue(bacon); 
      final CheckBox green = (CheckBox)findViewById(R.id.cbgreen); 
       int i10 = Ifchecked.gettrue(green); 
      final CheckBox olives = (CheckBox)findViewById(R.id.cbolives); 
       int i11 = Ifchecked.gettrue(olives); 
    int numtop = i1+i2+i3+i4+i5+i6+i7+i8+i9+i10+i11; 


} 
} 

Stack Trace:

04-04 01:41:46.536: D/AndroidRuntime(676): Shutting down VM 
04-04 01:41:46.536: W/dalvikvm(676): threadid=1: thread exiting with uncaught exception (group=0x40015560) 
04-04 01:41:46.556: E/AndroidRuntime(676): FATAL EXCEPTION: main 
04-04 01:41:46.556: E/AndroidRuntime(676): java.lang.RuntimeException: Unable to start activity ComponentInfo{pd.wilson.com/pd.wilson.com.Page3}: java.lang.NullPointerException 
04-04 01:41:46.556: E/AndroidRuntime(676): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 
04-04 01:41:46.556: E/AndroidRuntime(676): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
04-04 01:41:46.556: E/AndroidRuntime(676): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
04-04 01:41:46.556: E/AndroidRuntime(676): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
04-04 01:41:46.556: E/AndroidRuntime(676): at android.os.Handler.dispatchMessage(Handler.java:99) 
04-04 01:41:46.556: E/AndroidRuntime(676): at android.os.Looper.loop(Looper.java:123) 
04-04 01:41:46.556: E/AndroidRuntime(676): at android.app.ActivityThread.main(ActivityThread.java:3683) 
04-04 01:41:46.556: E/AndroidRuntime(676): at java.lang.reflect.Method.invokeNative(Native Method) 
04-04 01:41:46.556: E/AndroidRuntime(676): at java.lang.reflect.Method.invoke(Method.java:507) 
04-04 01:41:46.556: E/AndroidRuntime(676): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
04-04 01:41:46.556: E/AndroidRuntime(676): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
04-04 01:41:46.556: E/AndroidRuntime(676): at dalvik.system.NativeStart.main(Native Method) 
04-04 01:41:46.556: E/AndroidRuntime(676): Caused by: java.lang.NullPointerException 
04-04 01:41:46.556: E/AndroidRuntime(676): at pd.wilson.com.Page3.onCreate(Page3.java:29) 
04-04 01:41:46.556: E/AndroidRuntime(676): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
04-04 01:41:46.556: E/AndroidRuntime(676): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 
04-04 01:41:46.556: E/AndroidRuntime(676): ... 11 more 
04-04 01:41:48.256: I/Process(676): Sending signal. PID: 676 SIG: 9 
04-04 01:45:18.077: D/AndroidRuntime(733): Shutting down VM 
04-04 01:45:18.077: W/dalvikvm(733): threadid=1: thread exiting with uncaught exception (group=0x40015560) 
04-04 01:45:18.099: E/AndroidRuntime(733): FATAL EXCEPTION: main 
04-04 01:45:18.099: E/AndroidRuntime(733): java.lang.RuntimeException: Unable to start activity ComponentInfo{pd.wilson.com/pd.wilson.com.Page3}: java.lang.NullPointerException 
04-04 01:45:18.099: E/AndroidRuntime(733): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 
04-04 01:45:18.099: E/AndroidRuntime(733): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
04-04 01:45:18.099: E/AndroidRuntime(733): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
04-04 01:45:18.099: E/AndroidRuntime(733): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
04-04 01:45:18.099: E/AndroidRuntime(733): at android.os.Handler.dispatchMessage(Handler.java:99) 
04-04 01:45:18.099: E/AndroidRuntime(733): at android.os.Looper.loop(Looper.java:123) 
04-04 01:45:18.099: E/AndroidRuntime(733): at android.app.ActivityThread.main(ActivityThread.java:3683) 
04-04 01:45:18.099: E/AndroidRuntime(733): at java.lang.reflect.Method.invokeNative(Native Method) 
04-04 01:45:18.099: E/AndroidRuntime(733): at java.lang.reflect.Method.invoke(Method.java:507) 
04-04 01:45:18.099: E/AndroidRuntime(733): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
04-04 01:45:18.099: E/AndroidRuntime(733): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
04-04 01:45:18.099: E/AndroidRuntime(733): at dalvik.system.NativeStart.main(Native Method) 
04-04 01:45:18.099: E/AndroidRuntime(733): Caused by: java.lang.NullPointerException 
04-04 01:45:18.099: E/AndroidRuntime(733): at pd.wilson.com.Page3.onCreate(Page3.java:29) 
04-04 01:45:18.099: E/AndroidRuntime(733): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
04-04 01:45:18.099: E/AndroidRuntime(733): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 
04-04 01:45:18.099: E/AndroidRuntime(733): ... 11 more 
04-04 01:46:10.457: D/AndroidRuntime(767): Shutting down VM 
04-04 01:46:10.457: W/dalvikvm(767): threadid=1: thread exiting with uncaught exception (group=0x40015560) 
04-04 01:46:10.496: E/AndroidRuntime(767): FATAL EXCEPTION: main 
04-04 01:46:10.496: E/AndroidRuntime(767): java.lang.RuntimeException: Unable to start activity ComponentInfo{pd.wilson.com/pd.wilson.com.Page3}: java.lang.NullPointerException 
04-04 01:46:10.496: E/AndroidRuntime(767): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 
04-04 01:46:10.496: E/AndroidRuntime(767): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
04-04 01:46:10.496: E/AndroidRuntime(767): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
04-04 01:46:10.496: E/AndroidRuntime(767): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
04-04 01:46:10.496: E/AndroidRuntime(767): at android.os.Handler.dispatchMessage(Handler.java:99) 
04-04 01:46:10.496: E/AndroidRuntime(767): at android.os.Looper.loop(Looper.java:123) 
04-04 01:46:10.496: E/AndroidRuntime(767): at android.app.ActivityThread.main(ActivityThread.java:3683) 
04-04 01:46:10.496: E/AndroidRuntime(767): at java.lang.reflect.Method.invokeNative(Native Method) 
04-04 01:46:10.496: E/AndroidRuntime(767): at java.lang.reflect.Method.invoke(Method.java:507) 
04-04 01:46:10.496: E/AndroidRuntime(767): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
04-04 01:46:10.496: E/AndroidRuntime(767): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
04-04 01:46:10.496: E/AndroidRuntime(767): at dalvik.system.NativeStart.main(Native Method) 
04-04 01:46:10.496: E/AndroidRuntime(767): Caused by: java.lang.NullPointerException 
04-04 01:46:10.496: E/AndroidRuntime(767): at pd.wilson.com.Page3.onCreate(Page3.java:29) 
04-04 01:46:10.496: E/AndroidRuntime(767): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
04-04 01:46:10.496: E/AndroidRuntime(767): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 
04-04 01:46:10.496: E/AndroidRuntime(767): ... 11 more 
04-04 01:46:12.348: I/Process(767): Sending signal. PID: 767 SIG: 9 
04-04 01:55:31.597: D/AndroidRuntime(800): Shutting down VM 
04-04 01:55:31.597: W/dalvikvm(800): threadid=1: thread exiting with uncaught exception (group=0x40015560) 
04-04 01:55:31.607: E/AndroidRuntime(800): FATAL EXCEPTION: main 
04-04 01:55:31.607: E/AndroidRuntime(800): java.lang.RuntimeException: Unable to start activity ComponentInfo{pd.wilson.com/pd.wilson.com.Page3}: java.lang.NullPointerException 
04-04 01:55:31.607: E/AndroidRuntime(800): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 
04-04 01:55:31.607: E/AndroidRuntime(800): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
04-04 01:55:31.607: E/AndroidRuntime(800): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
04-04 01:55:31.607: E/AndroidRuntime(800): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
04-04 01:55:31.607: E/AndroidRuntime(800): at android.os.Handler.dispatchMessage(Handler.java:99) 
04-04 01:55:31.607: E/AndroidRuntime(800): at android.os.Looper.loop(Looper.java:123) 
04-04 01:55:31.607: E/AndroidRuntime(800): at android.app.ActivityThread.main(ActivityThread.java:3683) 
04-04 01:55:31.607: E/AndroidRuntime(800): at java.lang.reflect.Method.invokeNative(Native Method) 
04-04 01:55:31.607: E/AndroidRuntime(800): at java.lang.reflect.Method.invoke(Method.java:507) 
04-04 01:55:31.607: E/AndroidRuntime(800): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
04-04 01:55:31.607: E/AndroidRuntime(800): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
04-04 01:55:31.607: E/AndroidRuntime(800): at dalvik.system.NativeStart.main(Native Method) 
04-04 01:55:31.607: E/AndroidRuntime(800): Caused by: java.lang.NullPointerException 
04-04 01:55:31.607: E/AndroidRuntime(800): at pd.wilson.com.Page3.onCreate(Page3.java:29) 
04-04 01:55:31.607: E/AndroidRuntime(800): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
04-04 01:55:31.607: E/AndroidRuntime(800): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 
04-04 01:55:31.607: E/AndroidRuntime(800): ... 11 more 
04-04 01:55:33.357: I/Process(800): Sending signal. PID: 800 SIG: 9 
+9

Sie haben einen StackTrace, nicht wahr? Was ist die Ausgabe von 'logcat'? –

+0

@TravisWebb http://www.text-upload.com/read.php?id=331977&c=5801421 – Wilson

+2

[Stack-Trace in der Frage bitte.] (Http://imgur.com/jacoj) – Whymarrh

Antwort

1

nach Ihren Stacktrace:

Caused by: java.lang.NullPointerException 
E/AndroidRuntime(676): at pd.wilson.com.Page3.onCreate(Page3.java:29) 

Deshalb würde ich Linie überprüfen von Page3.java. Die NullPointerException wird von dieser Linie geworfen, so sollten Sie auf dieser Linie schauen und versuchen, zu folgern, welche Variable Sie dereferencing (Dereferenzierungsoperator = .)

+0

Dies sollte ein Kommentar sein. – Ryan

+3

Er versteht offensichtlich nicht, wie man einen StackTrace liest, also ist dies eine Antwort. –

+0

Soweit ich das beurteilen kann, haben die radiobutton/radioogroups Werte. Ist es möglich, dass die Radiobuttons und Gruppen auf einem XML-Dokument sind, das während einer anderen Aktivität angezeigt wird, auf die ich nicht mehr verweisen kann? – Wilson

2

Basierend auf dem Stack-Trace:

04-04 01:41:46.556: E/AndroidRuntime(676): Caused by: java.lang.NullPointerException 
04-04 01:41:46.556: E/AndroidRuntime(676): at pd.wilson.com.Page3.onCreate(Page3.java:29) 

es sieht aus wie Zeile 29 der Datei ist, wo das Problem liegt:

if (size.getCheckedRadioButtonId() == small.getId()){ 

Entweder size oder small ist wahrscheinlich eine null-Referenz, so dass Sie das überprüfen sollten.

Wenn es sich herausstellt, dass einer von ihnen ist null (wahrscheinlich), ein Google für android findviewbyid return null ergibt sich recht viele mögliche Ursachen.

+0

+1 für die Stärke, die mir fehlte, um seinen Code in einen Editor zu kopieren, um festzustellen, was in Zeile 29 ist. –

0

Wenn Sie die obigen Zeilen betrachten, werden die Größen als endgültig deklariert, was bedeutet, dass sie nicht mehr geändert werden können. Das ist jedoch nicht das Problem für Absturz.

Entweder size.getCheckedRadioButtonId() oder small.getId() ist null.

+0

besser, wenn Sie Ihre XML-Datei auch bereitstellen können –

Verwandte Themen