Attempt to invoke virtual method '…' on a

2019-07-30 06:55发布

This question already has an answer here:

I getting the "Attempt to invoke virtual method 'java.lang.String[] android.os.Bundle.getStringArray(java.lang.String)' on a null " error in some devices:

Stack Trace:

Exception java.lang.RuntimeException: Unable to start activity     ComponentInfo{com.facebook.misstest/com.facebook.misstest.LoveMeterResultActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String[] android.os.Bundle.getStringArray(java.lang.String)' on a null object reference
android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3254)
android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3350)
android.app.ActivityThread.access$1100 (ActivityThread.java:222)
android.app.ActivityThread$H.handleMessage (ActivityThread.java:1795)
android.os.Handler.dispatchMessage (Handler.java:102)
android.os.Looper.loop (Looper.java:158)
android.app.ActivityThread.main (ActivityThread.java:7229)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run       
(ZygoteInit.java:1230)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1120)
Caused by java.lang.NullPointerException: Attempt to invoke virtual method    'java.lang.String[] android.os.Bundle.getStringArray(java.lang.String)' on a null object reference
com.facebook.misstest.LoveMeterResultActivity.onCreate      
(LoveMeterResultActivity.java:133)
android.app.Activity.performCreate (Activity.java:6876)
android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1135)
android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3207)
android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3350)

Here is the code which is send data to the next activity:

            //send this result to LoveMeterResultActivity
            final Bundle basket = new Bundle();
            basket.putStringArray("key", loveData);
                    Intent intent = new Intent(LoveMeterActivity.this, LoveMeterResultActivity.class);
                    intent.putExtras(basket);
                    startActivity(intent);

1条回答
姐就是有狂的资本
2楼-- · 2019-07-30 07:14

Check intent is null or not before you get data

Intent intent=this.getIntent();
if(intent !=null){
       // do something you want
    }

example

In ClassA:

Intent intent = new Intent(this, ClassB);
String[] myStrings = new String[] {"test", "test2"};
intent.putExtra("strings", myStrings);
startActivity(intent);

In ClassB:

  Intent intent = getIntent();
     if(intent !=null){
          String[] myStrings = intent.getStringArrayExtra("strings");
      }
查看更多
登录 后发表回答