在android.support.v4.app.Fragment.onActivityResult(int,int,android.content.Intent)上获取NullPointerException

时间:2018-08-30 12:57:37

标签: android biometrics

我正在使用NSDL Esign门户通过生物识别设备对Aadhar详细信息进行身份验证。指纹数据已成功扫描,但不幸的是,在过程结束时,我的应用程序因上述错误而崩溃。这就是我在“活动”中实现的

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    for (Fragment fragment : getSupportFragmentManager().getFragments()) {
        fragment.onActivityResult(requestCode, resultCode, data);
    }
}

这是我在Fragment中添加的代码,用于将指纹数据添加到我的服务器中。

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case Constants.NSDL_REQUEST_CODE:
            if (resultCode == RESULT_OK && null != data) {
                String eSignResponse = data.getStringExtra("signedResponse");
                try {
                    String postData = "msg=" + URLEncoder.encode(eSignResponse, "UTF-8") + "&type=M";
                    webView.postUrl(responseURL, postData.getBytes("UTF-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            break;
    }
}

LogCat中发生错误是

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=Intent { (has extras) }} to activity {com.fin.esign/com.fin.esign.BaseActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.Fragment.onActivityResult(int, int, android.content.Intent)' on a null object reference
                                                             at android.app.ActivityThread.deliverResults(ActivityThread.java:4255)
                                                             at android.app.ActivityThread.handleSendResult(ActivityThread.java:4298)
                                                             at android.app.ActivityThread.-wrap20(ActivityThread.java)
                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1613)
                                                             at android.os.Handler.dispatchMessage(Handler.java:110)
                                                             at android.os.Looper.loop(Looper.java:203)
                                                             at android.app.ActivityThread.main(ActivityThread.java:6339)
                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1084)
                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:945)
                                                          Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.Fragment.onActivityResult(int, int, android.content.Intent)' on a null object reference
                                                             at com.fin.esign.BaseActivity.onActivityResult(BaseActivity.java:122)
                                                             at android.app.Activity.dispatchActivityResult(Activity.java:6948)
                                                             at android.app.ActivityThread.deliverResults(ActivityThread.java:4251)
                                                             at android.app.ActivityThread.handleSendResult(ActivityThread.java:4298) 
                                                             at android.app.ActivityThread.-wrap20(ActivityThread.java) 
                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1613) 
                                                             at android.os.Handler.dispatchMessage(Handler.java:110) 
                                                             at android.os.Looper.loop(Looper.java:203) 
                                                             at android.app.ActivityThread.main(ActivityThread.java:6339) 
                                                             at java.lang.reflect.Method.invoke(Native Method) 
                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1084) 
                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:945) 

2 个答案:

答案 0 :(得分:1)

我通过添加删除片段的for循环并仅在希望接收结果的地方添加特定片段来解决了这个问题。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    for (Fragment fragment : getSupportFragmentManager().getFragments()) {
        if (null != fragment) {
            fragment.onActivityResult(requestCode, resultCode, data);
        } else {
            new MyFragment().onActivityResult(requestCode, resultCode, data);
        }
    }
}

答案 1 :(得分:0)

根据您发布的每个异常,您在null调用返回的集合中有getFragments()个项目。之所以有它,还有另一个问题,但与此同时,我建议您使用Fragment#startActivityForResult,以便在以后将结果自动分派到正确的片段。