android onActivityResult:尝试在空对象引用上调用虚方法

时间:2016-03-22 05:13:26

标签: android cordova-plugins

即时开发cordova android的插件。它打开一个自定义活动并将数据集返回给插件。

所有事情都在活动上运行良好。但是当活动结束时,它崩溃并给我这个错误:

Attempt to invoke virtual method 'void org.apache.cordova.CallbackContext.success(java.lang.String)' on a null object reference
E/AndroidRuntime( 2697):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3539)
E/AndroidRuntime( 2697):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3582)
E/AndroidRuntime( 2697):    at android.app.ActivityThread.access$1300(ActivityThread.java:144)
E/AndroidRuntime( 2697):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1327)
E/AndroidRuntime( 2697):    at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime( 2697):    at android.os.Looper.loop(Looper.java:135)
E/AndroidRuntime( 2697):    at android.app.ActivityThread.main(ActivityThread.java:5221)
E/AndroidRuntime( 2697):    at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime( 2697):    at java.lang.reflect.Method.invoke(Method.java:372)
E/AndroidRuntime( 2697):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
E/AndroidRuntime( 2697):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
E/AndroidRuntime( 2697): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void org.apache.cordova.CallbackContext.success(java.lang.String)' on a null object reference
E/AndroidRuntime( 2697):    at com.faiz.faizOk.onActivityResult(faizOk.java:147)
E/AndroidRuntime( 2697):    at org.apache.cordova.CordovaInterfaceImpl.onActivityResult(CordovaInterfaceImpl.java:151)
E/AndroidRuntime( 2697):    at org.apache.cordova.CordovaActivity.onActivityResult(CordovaActivity.java:348)
E/AndroidRuntime( 2697):    at android.app.Activity.dispatchActivityResult(Activity.java:6139)
E/AndroidRuntime( 2697):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3535)
E/AndroidRuntime( 2697):    
... 10 more

这是我的android onActivityResult方法

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    System.out.println("finished native activity proceess...");

    String message = " :finished activity from native: ";
    callbackContext.success(message);
}

2 个答案:

答案 0 :(得分:1)

你必须初始化回调上下文。可能你已经声明它但是没有初始化,如: 1. Callbackcontext myvar; Myvar.mymethod

这将抛出一个空指针,因为callbackcontext已声明但未初始化。 2.要解决上述问题,您必须初始化。

Callbackcontext myvar = new callbackcontext ;; Myvar.mymethod

答案 1 :(得分:0)

您尚未初始化变量callbackContext。在onCreate()方法上,您应该初始化它,例如callbackContext = new CallbackContext()是CallbackContext对应于callbackContext

类型的类