在 android.support.v4.app.Fragment.onActivityResult(int, int, android.content.Intent) 获取 NullPointerException
Getting NullPointerException at android.support.v4.app.Fragment.onActivityResult(int, int, android.content.Intent)
我正在使用 NSDL Esign 门户通过生物识别设备验证 Aadhar 详细信息。指纹数据已成功扫描,但不幸的是,在该过程结束时,我的应用程序因上述错误而崩溃。这就是我在 Activity
中实现的
@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);
}
}
这是我在片段中添加的代码,用于将指纹数据添加到我的服务器中。
@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)
根据您发布的异常,您在 getFragments()
调用返回的集合中有 null
项。你有它的原因是另一个问题,但与此同时我建议你使用 Fragment#startActivityForResult 以便结果在之后自动发送到正确的片段。
我通过添加删除片段的 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);
}
}
}
我正在使用 NSDL Esign 门户通过生物识别设备验证 Aadhar 详细信息。指纹数据已成功扫描,但不幸的是,在该过程结束时,我的应用程序因上述错误而崩溃。这就是我在 Activity
中实现的@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);
}
}
这是我在片段中添加的代码,用于将指纹数据添加到我的服务器中。
@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)
根据您发布的异常,您在 getFragments()
调用返回的集合中有 null
项。你有它的原因是另一个问题,但与此同时我建议你使用 Fragment#startActivityForResult 以便结果在之后自动发送到正确的片段。
我通过添加删除片段的 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);
}
}
}