简体   繁体   English

我不断收到 NullPointerException,但找不到我犯错的地方

[英]I keep getting a NullPointerException but can't find where I made the mistake

As far as I can tell, every variable that I use is not used without being given a value first.据我所知,我使用的每个变量都不会在没有先赋值的情况下使用。 The nullpointerexception happens at the moment this activity begins.. Apologies that I cannot be more specific. 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;


}
}  

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 

Based on your 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) 

it looks like line 29 of that file is where the problem lies:看起来问题出在该文件的第 29 行:

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

Either size or small is likely to be a null reference so you should check that. sizesmall可能是 null 参考,因此您应该检查一下。

If it turns out one of them is null (likely), a google for android findviewbyid return null turns up quite a few potential causes.如果发现其中之一null(可能),谷歌搜索android findviewbyid return null会找到很多潜在原因。

According to your stacktrace:根据您的堆栈跟踪:

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

Therefore, I would check line 29 of Page3.java .因此,我会检查Page3.java的第29行。 The NullPointerException is thrown from that line, so you should look at that line and try to deduce what variable you are dereferencing (dereference operator = . ) NullPointerException是从该行抛出的,因此您应该查看该行并尝试推断您正在取消引用的变量(取消引用运算符 = .

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

Looking at above lines you are declarring sizeS as final that means that it cant be changed again.查看上面的行,您将 sizeS 声明为最终的,这意味着它不能再次更改。 however that is not the problem for crash..然而,这不是崩溃的问题..

Either size.getCheckedRadioButtonId() or small.getId() is null. size.getCheckedRadioButtonId() 或 small.getId() 为 null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM