gpt4 book ai didi

java - Android Intent/IntentService 的空指针异常

转载 作者:行者123 更新时间:2023-12-02 04:29:47 25 4
gpt4 key购买 nike

我的应用程序崩溃,返回以下堆栈跟踪:

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.**REDACTED**.copyright/com.example.**REDACTED**.copyright.MainActivity}: java.lang.NullPointerException
07-21 22:26:18.499 E/AndroidRuntime(15933): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
07-21 22:26:18.499 E/AndroidRuntime(15933): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2252)
07-21 22:26:18.499 E/AndroidRuntime(15933): at android.app.ActivityThread.access$800(ActivityThread.java:139)
07-21 22:26:18.499 E/AndroidRuntime(15933): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1200)
07-21 22:26:18.499 E/AndroidRuntime(15933): at android.os.Handler.dispatchMessage(Handler.java:102)
07-21 22:26:18.499 E/AndroidRuntime(15933): at android.os.Looper.loop(Looper.java:136)
07-21 22:26:18.499 E/AndroidRuntime(15933): at android.app.ActivityThread.main(ActivityThread.java:5103)
07-21 22:26:18.499 E/AndroidRuntime(15933): at java.lang.reflect.Method.invokeNative(Native Method)
07-21 22:26:18.499 E/AndroidRuntime(15933): at java.lang.reflect.Method.invoke(Method.java:515)
07-21 22:26:18.499 E/AndroidRuntime(15933): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
07-21 22:26:18.499 E/AndroidRuntime(15933): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
07-21 22:26:18.499 E/AndroidRuntime(15933): at dalvik.system.NativeStart.main(Native Method)
07-21 22:26:18.499 E/AndroidRuntime(15933): Caused by: java.lang.NullPointerException
07-21 22:26:18.499 E/AndroidRuntime(15933): at android.content.ContextWrapper.getPackageName(ContextWrapper.java:135)
07-21 22:26:18.499 E/AndroidRuntime(15933): at android.content.ComponentName.<init>(ComponentName.java:77)
07-21 22:26:18.499 E/AndroidRuntime(15933): at android.content.Intent.<init>(Intent.java:3834)
07-21 22:26:18.499 E/AndroidRuntime(15933): at com.example.**REDACTED**.copyright.MainActivity.<init>(MainActivity.java:31)
07-21 22:26:18.499 E/AndroidRuntime(15933): at java.lang.Class.newInstanceImpl(Native Method)
07-21 22:26:18.499 E/AndroidRuntime(15933): at java.lang.Class.newInstance(Class.java:1208)
07-21 22:26:18.499 E/AndroidRuntime(15933): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
07-21 22:26:18.499 E/AndroidRuntime(15933): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2119)

据我所知,错误发生在 MainActivity 类第 31 行的第 31 行,周围的一些行如下所示:

public class MainActivity extends Activity {
Intent mServiceIntent = new Intent(this, Scan.class);

mServiceIntent 不会再次被引用,直到下面显示的行也位于 MainActivity 中:

 @Override
protected void onStop() {
super.onStop();

this.startService(mServiceIntent);
Log.v("MainActivity", "onStop called");

如果您需要更多信息,我很乐意添加。预先感谢您的耐心和帮助。

最佳答案

您应该在 onStop() 方法中实例化 Intent (这在运行时发生),而不是在编译时定义它。例如:

public class MainActivity extends Activity {
Intent mServiceIntent;
...
@Override
protected void onStop() {
super.onStop();
mServiceIntent = new Intent(this, Scan.class);
this.startService(mServiceIntent);
Log.v("MainActivity", "onStop called");

关于java - Android Intent/IntentService 的空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31621411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com