尝试调用虚拟方法' ...'在null对象引用上

时间:2017-01-08 11:27:52

标签: java android nullpointerexception

我得到"尝试调用虚方法' java.lang.String [] android.os.Bundle.getStringArray(java.lang.String)'在null" 某些设备出错:

堆栈追踪:

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)

以下是将数据发送到下一个活动的代码:

            //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 个答案:

答案 0 :(得分:0)

在获取数据之前检查intent是否为null

.htaccess

例如

在ClassA中:

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

在ClassB中:

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