我有mainfragment.java
,并在viewpager
添加了mainfragment.java
。在该viewpager中有mobile_fragment.java
。当我点击联系人后,使用意图点击mobile_fragment.java
中的编辑文字打开手机通讯录时,使用获取结果Onactivityresult ,但结果没有进入mobile_fragment.java
。 Onactivityresult 一直到mainfragment.java
,而不是来自view {0}的mobile_fragment.java
。我认为问题出现在嵌套片段上。我在两个类中都做了一些事情,但我得到了NullpointerException
错误。我不知道如何解决这个问题。请帮我解决这个问题。
mainfragment.java
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (other_fragments != null){
String phoneNo = null ;
Uri uri = data.getData();
Cursor cursor = getActivity().getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
int phoneIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
phoneNo = cursor.getString(phoneIndex);
Intent output = new Intent();
output.putExtra("number", ""+phoneNo);
other_fragments.onActivityResult(requestCode, resultCode, output);
}
}
mobile_fragment.java
//This fragment i have edit-text.
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_CONTACT_REQUEST) {
if (resultCode == getActivity().RESULT_OK) {
String te=data.getStringExtra("number");
Log.v("TAG_TA",""+te);
// It prints the log values correctly.
prepaid_mobilenum_edt.setText(""+data.getStringExtra("number"));//NullPointerException comes here.
}
}
错误日志
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65537, result=-1, data=Intent { dat=content://com.android.contacts/data/80704 flg=0x1 }} to activity {com.reloadapp.reload/com.reloadapp.reload.fragments.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:2997)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3040)
at android.app.ActivityThread.access$1100(ActivityThread.java:128)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1191)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4517)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.reloadapp.reload.fragments.Mobile_Fragment.onActivityResult(Mobile_Fragment.java:1236)
at com.reloadapp.reload.fragments.Main_frag_one.onActivityResult(Main_frag_one.java:97)
at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:163)
at android.app.Activity.dispatchActivityResult(Activity.java:4654)
at android.app.ActivityThread.deliverResults(ActivityThread.java:2993)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3040)
at android.app.ActivityThread.access$1100(ActivityThread.java:128)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1191)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4517)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
mobile_fragment中的onCreateview:
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
if (container == null) {
return null;
}
prepaid_mobilenum_edt = (EditText) lay.findViewById(R.id.mobile_prepaid_mobilenum_edttxt);
return lay;
}
答案 0 :(得分:2)
将OnCreateView更改为
static View mContainer;
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
mContainer=inflater.inflate(R.layout.your_layout, container, false);
prepaid_mobilenum_edt = (EditText) mContainer.findViewById(R.id.mobile_prepaid_mobilenum_edttxt);
return mContainer;
}
并将您的活动结果更新为
if (requestCode == PICK_CONTACT_REQUEST) {
if (resultCode == getActivity().RESULT_OK) {
String te=data.getStringExtra("number");
Log.v("TAG_TA",""+te);
// It prints the log values correctly.
if(prepaid_mobilenum_edt==null)
prepaid_mobilenum_edt = (EditText) mContainer.findViewById(R.id.mobile_prepaid_mobilenum_edttxt);
prepaid_mobilenum_edt.setText(""+data.getStringExtra("number"));
}