繁体   English   中英

我在 android 中收到 null 指针异常

[英]I am getting null pointer exception in android

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

02-10 22:42:11.702 30997-30997/? 我/艺术:延迟启用 -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 Z4D236 D9A2D102C5FE6AD1C50DA4BEC50Z.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:没有 package 标识符获取资源编号 0x00000000 02-10 02-10 22:42:11.803 30997-30997/com.androidexample.courtcounter W/System 的名称时引用的未知路径:/data/app /com.androidexample.courtcounter-1/lib/arm64 02-10 22:42:11.826 30997-30997/com.androidexample.courtcounter D/AndroidRuntime:关闭 VM 02-10 22:42:11.827 30997-30997/com。 androidexample.courtcounter E/AndroidRuntime:致命异常:主进程:com.androidexample.courtcounter,PID:30997 java.lang.RuntimeException:Un 能够实例化活动ComponentInfo{com.androidexample.courtcounter/com.androidexample.courtcounter.MainActivity}:java.lang.NullPointerException:尝试调用虚拟方法'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 ) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1396) 在 android.os.Handler.dispatchMessage(Ha ndler.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在 null objectCE7EC988DFC46 参考 null 上调用虚拟方法 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' E58Z.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 Z93F725A07423FE1C8 89F448B33D21F46Z.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.Activit yThread.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

它看起来像这样的代码:

 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);

是方法外的成员变量定义。 这在 Android 上不起作用,因为此代码在 object 实例化时间执行,并且您不能在调用setContentView() findViewById() 您在onCreate()中调用setContentView()是正确的,但是在实例化 object之后调用onCreate()

您需要从初始化中拆分成员变量定义。 为此,请在方法之外声明成员变量而不进行初始化:

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

然后,在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);

暂无
暂无

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

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