我在android中收到空指针异常

I am getting null pointer exception in android

提问人:Gunjan jha 提问时间:2/11/2021 更新时间:2/11/2021 访问量:236

问:

    The below code gives null pointer exception in android studio    
please help me to solve that 

02-10 22:42:11.702 30997-30997/? I/art: Late-enabling -Xcheck:jni 02-10 22:42:11.749 30997-30997/? D/TidaProvider: TidaProvider() 02-10 22:42:11.757 30997-30997/? W/ReflectionUtils: java.lang.NoSuchMethodException: android.os.MessageQueue#enableMonitor()#bestmatch at miui.util.ReflectionUtils.findMethodBestMatch(ReflectionUtils.java:338) at miui.util.ReflectionUtils.findMethodBestMatch(ReflectionUtils.java:375) at miui.util.ReflectionUtils.callMethod(ReflectionUtils.java:800) at miui.util.ReflectionUtils.tryCallMethod(ReflectionUtils.java:818) at android.os.BaseLooper.enableMonitor(BaseLooper.java:47) at android.os.Looper.prepareMainLooper(Looper.java:111) at android.app.ActivityThread.main(ActivityThread.java:5584) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652) 02-10 22:42:11.789 30997-30997/com.androidexample.courtcounter W/ResourceType: No package identifier when getting name for resource number 0x00000000 02-10 22:42:11.803 30997-30997/com.androidexample.courtcounter W/System: ClassLoader referenced unknown path: /data/app/com.androidexample.courtcounter-1/lib/arm64 02-10 22:42:11.826 30997-30997/com.androidexample.courtcounter D/AndroidRuntime: Shutting down VM 02-10 22:42:11.827 30997-30997/com.androidexample.courtcounter E/AndroidRuntime: FATAL EXCEPTION: main Process: com.androidexample.courtcounter, PID: 30997 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.androidexample.courtcounter/com.androidexample.courtcounter.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2396) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2545) at android.app.ActivityThread.access$1100(ActivityThread.java:151) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1396) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:157) at android.app.ActivityThread.main(ActivityThread.java:5601) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:160) at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:109) at android.content.Context.obtainStyledAttributes(Context.java:517) at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:839) at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:806) at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:630) at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:223) at com.androidexample.courtcounter.MainActivity.(MainActivity.java:19) at java.lang.Class.newInstance(Native Method) at android.app.Instrumentation.newActivity(Instrumentation.java:1068) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2386) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2545)  at android.app.ActivityThread.access$1100(ActivityThread.java:151)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1396)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:157)  at android.app.ActivityThread.main(ActivityThread.java:5601)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652) 

     public class MainActivity extends AppCompatActivity {
         int team_a_Score=0;
         int team_b_Score=0;
         @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
        
            }
            final Button button1=(Button)findViewById(R.id.score2);
            final Button button_teamB1=(Button)findViewById(R.id.TeamB_score1);
            final Button buttonScore3A=(Button)findViewById(R.id.score3);
            final Button buttonScore3B=(Button)findViewById(R.id.TeamB_score3);
            final Button buttonScore_freeshotA=(Button)findViewById(R.id.freeshot);
            final Button buttonScore_freeshotB=(Button)findViewById(R.id.TeamB_freeshot);
        
                public void onClick(View v) {
                    if (v.equals(button1)) {
                        team_a_Score = team_a_Score + 1;
                        TextView text = (TextView) findViewById(R.id.score_team_a);
                        text.setText(team_a_Score);
        
                    } else if (v.equals(button_teamB1)) {
                        team_b_Score = team_b_Score + 1;
                        TextView text = (TextView) findViewById(R.id.score_teamB);
                        text.setText(team_b_Score);
                    } else if (v.equals(buttonScore3A)) {
                        team_a_Score = team_a_Score + 3;
                        TextView text = (TextView) findViewById(R.id.score_team_a);
                        text.setText(team_b_Score);
                    } else if (v.equals(buttonScore3B)) {
                        team_b_Score = team_b_Score + 3;
                        TextView text = (TextView) findViewById(R.id.score_teamB);
                        text.setText(team_b_Score);
                    } else if (v.equals(buttonScore_freeshotA)) {
                        team_a_Score = team_a_Score + 4;
                        TextView text = (TextView) findViewById(R.id.score_team_a);
                        text.setText(team_a_Score);
                    } else if (v.equals(buttonScore_freeshotB)) {
                        team_b_Score = team_b_Score + 4;
                        TextView text = (TextView) findViewById(R.id.score_teamB);
                        text.setText(team_b_Score);
                    }
        
                }
            }
        
        List item
java android xml nullpointerexception

评论

0赞 David Wasser 2/11/2021
重新打开这个问题。在这里,作为规范 NPE 问题的 dup 关闭是没有帮助的,因为崩溃发生在框架中的代码中,而不是 OP 的代码中。事实上,整个堆栈跟踪都是框架代码!OP 不可能找到 NPE,因为它都不是他/她的代码。在这种情况下,OP 需要了解框架工作原理的人的帮助。

答:

0赞 David Wasser 2/11/2021 #1

It looks like this code:

 final Button button1=(Button)findViewById(R.id.score2);
 final Button button_teamB1=(Button)findViewById(R.id.TeamB_score1);
 final Button buttonScore3A=(Button)findViewById(R.id.score3);
 final Button buttonScore3B=(Button)findViewById(R.id.TeamB_score3);
 final Button buttonScore_freeshotA=(Button)findViewById(R.id.freeshot);

is member variable definitions outside of a method. That doesn't work on Android, because this code executes at object instantiation time and you cannot call before is called. You are calling in which is correct, but is called after the object is instantiated.findViewById()setContentView()setContentView()onCreate()onCreate()

You need to split the member variable definitions from the initialization. To do that, declare the member variables without initialization outside of a method:

 final Button button1;
 final Button button_teamB1;
 final Button buttonScore3A;
 final Button buttonScore3B;
 final Button buttonScore_freeshotA;

Then, in , after calling , you can initialize the member variables like this:onCreate()setContentView()

 button1=(Button)findViewById(R.id.score2);
 button_teamB1=(Button)findViewById(R.id.TeamB_score1);
 buttonScore3A=(Button)findViewById(R.id.score3);
 buttonScore3B=(Button)findViewById(R.id.TeamB_score3);
 buttonScore_freeshotA=(Button)findViewById(R.id.freeshot);