通过单击应用程序小部件来空指针

时间:2019-04-20 15:10:03

标签: android android-widget

我正在从事一个名为Baking App的Udacity项目。该应用程序显示一些蛋糕食谱。 该应用程序应具有一个主屏幕小部件,该小部件应显示所需配方的成分列表。 现在,我的应用程序有一个小部件,显示最后一次看到的成分列表,但是当我点击它时,该应用程序由于Npe而崩溃。 这是logcat:

data-selected-value

这是导致崩溃的行:

04-20 16:42:41.719 31369-31369/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.android.bakingapp, PID: 31369
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.bakingapp/com.example.android.bakingapp.RecipeDetailActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.ArrayList android.os.Bundle.getParcelableArrayList(java.lang.String)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2434)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
        at android.app.ActivityThread.access$900(ActivityThread.java:153)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5451)
        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.util.ArrayList android.os.Bundle.getParcelableArrayList(java.lang.String)' on a null object reference
        at com.example.android.bakingapp.RecipeDetailActivity.onCreate(RecipeDetailActivity.java:39)
        at android.app.Activity.performCreate(Activity.java:6323)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2387)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494) 
        at android.app.ActivityThread.access$900(ActivityThread.java:153) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5451) 
        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)strong text

我还提供了活动代码:

 recipeArrayList = indexRecipe.getParcelableArrayList(INDEX_RECIPE);

有人可以帮助我解决此问题吗?

1 个答案:

答案 0 :(得分:-1)

if (savedInstanceState == null){
    Bundle indexRecipe = getIntent().getExtras();

您应改为选中savedInstanceState!= null