我正在 Android Studio 中开发一个应用程序,由于某种原因,它不断抛出错误“不幸的是,MyApp 已停止”。这只发生在我运行应用程序,在它运行时对代码进行更改,然后尝试再次运行它时。如果我停止“运行”任务并重新启动它,我不会收到此错误。
这是日志:
06-14 15:54:06.213 25752-25752/com.myapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.myapp, PID: 25752
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp/com.myapp.activity_host}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.myapp.MB_item.getItemId()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4077)
at android.app.ActivityThread.-wrap15(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1350)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.myapp.MB_item.getItemId()' on a null object reference
at com.myapp.BT_screen_menuButtons$override.onCreateView(MB_screen_menuButtons.java:119)
at com.myapp.BT_screen_menuButtons$override.access$dispatch(MB_screen_menuButtons.java)
at com.myapp.BT_screen_menuButtons.onCreateView(MB_screen_menuButtons.java:0)
at android.app.Fragment.performCreateView(Fragment.java:2220)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:973)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1130)
at android.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1953)
at android.app.FragmentController.dispatchActivityCreated(FragmentController.java:152)
at android.app.Activity.performCreateCommon(Activity.java:6232)
at android.app.Activity.performCreate(Activity.java:6239)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4077)
at android.app.ActivityThread.-wrap15(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1350)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
为什么代码只会在后续启动时表现不同,而不是在我最初运行应用程序时?
附加代码:
这是引发错误的行(MB_screen_menuButtons.java 的第 119 行)。screenData.getItemId() 显然是传递空值的位置,但我不明白为什么它只会在即时运行更新期间为空。
debugger.showIt(fragmentName + ":onCreateView JSON itemId: \"" + screenData.getItemId() + "\" itemType: \"" + screenData.getItemType() + "\" itemNickname: \"" + screenData.getItemNickname() + "\"");