我一直得到一个NullPointerException,但找不到我犯错误的地方

时间:2012-04-04 01:51:39

标签: java android nullpointerexception

据我所知,我使用的每个变量都没有在没有给出值的情况下使用。 nullpointerexception在此活动开始时发生。道歉,我不能更具体。

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;


}
}  

堆栈追踪:

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 

3 个答案:

答案 0 :(得分:2)

根据您的堆栈跟踪:

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) 

看起来该文件的第29行是问题所在:

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

sizesmall可能是空引用,因此您应该检查它。

如果事实证明其中一个 为null(可能),那么android findviewbyid return null的google就会出现很多可能的原因。

答案 1 :(得分:1)

根据你的stacktrace:

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

因此,我会检查 Page3.java 29 行。从该行抛出NullPointerException,因此您应该查看该行并尝试推断出您要解除引用的变量(取消引用运算符= .

答案 2 :(得分:0)

   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";
            }

在上面的行中,您将sizeS声明为final,这意味着它无法再次更改。但这不是崩溃的问题..

size.getCheckedRadioButtonId()或small.getId()都为null。