android android
android android

Reputation: 23

Onactivityresult is not working fine in nested fragment class android

I have mainfragment.java and am added the viewpager in mainfragment.java. In that viewpager have mobile_fragment.java.When i click the edit-text in mobile_fragment.java using Intent to open the phone contacts after user click the contacts, get the result using Onactivityresult, but the result was not coming in mobile_fragment.java. The Onactivityresult comes till mainfragment.java, not comes for mobile_fragment.java in viewpager. I think the problem comes for nested fragment.I done some thing in both class but i got NullpointerException error.I don't know how to solve this issue.Please help me out to solve this issue.

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.
            }
  }

Error log

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)

onCreateview in mobile_fragment:

 @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;
    }

Upvotes: 1

Views: 436

Answers (1)

IshRoid
IshRoid

Reputation: 3735

Change your OnCreateView as

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;
    }

And Update your activity result as

    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"));


                }

Upvotes: 2

Related Questions