PhoneGap2.7.0调用拍照和相册时出现onActivityResult NullPointerException问题

最近一直在做native+phonegap,在调用拍照和相册时出NullPointerException,以下是我log stack

06-26 15:43:31.661: E/AndroidRuntime(26585): FATAL EXCEPTION: main
06-26 15:43:31.661: E/AndroidRuntime(26585): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=34, result=-1, data=null} to activity {com.house.activity/com.house.activity.PersonalCenterActivity}: java.lang.NullPointerException
06-26 15:43:31.661: E/AndroidRuntime(26585):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3162)
06-26 15:43:31.661: E/AndroidRuntime(26585):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3205)
06-26 15:43:31.661: E/AndroidRuntime(26585):    at android.app.ActivityThread.access$1100(ActivityThread.java:136)
06-26 15:43:31.661: E/AndroidRuntime(26585):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1249)
06-26 15:43:31.661: E/AndroidRuntime(26585):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-26 15:43:31.661: E/AndroidRuntime(26585):    at android.os.Looper.loop(Looper.java:137)
06-26 15:43:31.661: E/AndroidRuntime(26585):    at android.app.ActivityThread.main(ActivityThread.java:4797)
06-26 15:43:31.661: E/AndroidRuntime(26585):    at java.lang.reflect.Method.invokeNative(Native Method)
06-26 15:43:31.661: E/AndroidRuntime(26585):    at java.lang.reflect.Method.invoke(Method.java:511)
06-26 15:43:31.661: E/AndroidRuntime(26585):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
06-26 15:43:31.661: E/AndroidRuntime(26585):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
06-26 15:43:31.661: E/AndroidRuntime(26585):    at dalvik.system.NativeStart.main(Native Method)
06-26 15:43:31.661: E/AndroidRuntime(26585): Caused by: java.lang.NullPointerException
06-26 15:43:31.661: E/AndroidRuntime(26585):    at org.apache.cordova.DroidGap.onActivityResult(DroidGap.java:838)
06-26 15:43:31.661: E/AndroidRuntime(26585):    at com.house.activity.BaseActivity.onActivityResult(BaseActivity.java:80)
06-26 15:43:31.661: E/AndroidRuntime(26585):    at android.app.Activity.dispatchActivityResult(Activity.java:5255)
06-26 15:43:31.661: E/AndroidRuntime(26585):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3158)
06-26 15:43:31.661: E/AndroidRuntime(26585):    ... 11 more


这个bug纠结我很久了,今天总算解决了...

在http://stackoverflow.com

上找了很久也没找到原因在那里,但上面的回答多多少少对我有一点帮助.最后还是自己慢慢看,经过调试一步步才测试原因所在,所以特别特别要记录一下,

这里我主要贴一些重要代码
PersonalCenterActivity .java
public class PersonalCenterActivity extends BaseActivity{
    @Override
    public void onCreate(Bundle savedInstanceState) {
                                                                                           
        cordovaWebView = (CordovaWebView) findViewById(R.id.cordovaWebView);
        cordovaWebView.loadUrl("file:///android_asset/www/add.html");
    }
}
BaseActivity.java
public class BaseActivity extends DroidGap {
    private final ExecutorService threadPool = Executors.newCachedThreadPool();
                                                                                           
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        HouseApp houseApp = (HouseApp) this.getActivity().getApplication();
        houseApp.getInstance().pushActivity(this);
    }
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        HouseApp houseApp = (HouseApp) this.getActivity().getApplication();
        houseApp.getInstance().popActivity(this);
    }
                                                                                           
    @Override
    public void onDestroy() {
        super.onDestroy();
        HouseApp houseApp = (HouseApp) this.getActivity().getApplication();
        if(houseApp !=null){
            houseApp.getInstance().popActivity(this);
        }
    }
    public Context getContext(){
        return this;
    }
    @Override
    public Activity getActivity() {
                                                                                               
        return this;
    }
    @Override
    public ExecutorService getThreadPool() {
                                                                                               
        return threadPool;
    }
        //如果没有重写,cordova.exec()方法无法使用同样也会所nullpointerexectption
    @Override
    public Object onMessage(String str, Object state) {
        //Log.v(TAG, "&State = " + state);
        return null;
    }
    @Override
    public void setActivityResultCallback(CordovaPlugin plugin) {
        Log.v(TAG, "cordovaPlugin" + plugin.toString());
    }
                                                                                               
        //此方法如果没有重写,调用拍照和相册无响应
    @Override
    public void startActivityForResult(CordovaPlugin command, Intent intent, int requestCode) {
        Log.v(TAG, "command = " + command.toString() + "&intent = " + intent.toString() + "&requestCode = " + requestCode);
        super.startActivityForResult(command, intent, requestCode);
    }
                                                                                               
        //此方法特别要注意,这也是拍照后回调图片结果发送到js的一个url
        ////此方法如果没有重写,调用拍照和相册回调成功/失败无url
    @Override
    public void onActivityResult(int arg0, int arg1, Intent arg2) {
        Log.v(TAG, "arg0=" + arg0 + "---arg1 = " + arg1 + "---arg2 = " + arg2);
                //出现nullPointerExcepotion原因就是它。
                //为什么,因为你直接调用super。onActivityResult,回调成功后是进入super.onActivityResult方法处理结果
        super.onActivityResult(arg0, arg1, arg2);
//为什么要加 activityResultCallback原因在于你当前在Stack中的Activity(也就是html页面)回调成功结果是在当前activity中你重写的onActivityResult处理  
super.activityResultCallback.onActivityResult(arg0, arg1, arg2);
    }
                                                                                           
}


以上如有解释不清楚,请谅解。。。。


因此问题我原先自己有单独写了一个小demo

此demo没有问题,可以运行

但是把里面拍照和相册代码整合就出现上面stack里面的信息

demo可以参考 见附件

你可能感兴趣的:(android,PhoneGap,Camera)