0

The problem is similar to this SO post.

@Jason replies that -

Your Fragment hasn't attached to the Activity yet, which also means your layout hasn't been inflated yet.

In that case, the issue was to simply pass the string from one fragment to another.

However, here I need to do something different:

Common.java

public class Common extends Fragment
{
   OnSelectedListener mCallback = new OnSelectedListener(){
     public void getFailureDialog(){

                RecordFailure fd = new RecordFailure(); 
                fd.show(getActivity().getSupportFragmentManager(), "dialog"); //Null pointer exception here.

        }
};

I call mCallback.getFailureDialog() to call that method.

Error Log:

03-22 18:21:30.022: E/AndroidRuntime(9385): java.lang.NullPointerException
03-22 18:21:30.022: E/AndroidRuntime(9385):     at com.cornmain.util.Common$1.getFailureDialog(Common.java:112)

How to do that?

UPDATE:

If I use fd.show(getFragmentManager(), "dialog");, then I get the following error:

Error Log:

03-22 18:35:18.432: E/AndroidRuntime(11872): java.lang.NullPointerException
03-22 18:35:18.432: E/AndroidRuntime(11872):    at android.support.v4.app.DialogFragment.show(DialogFragment.java:125)
03-22 18:35:18.432: E/AndroidRuntime(11872):    at com.cornmain.util.Common$1.getFailureDialog(Common.java:112)
8
  • Where do you call getFailureDialog()? And how do you declare your fragment: in code or in xml?
    – tundundun
    Mar 22, 2013 at 12:59
  • please take a look to that: stackoverflow.com/questions/6215239/… its the same problem...
    – nano_nano
    Mar 22, 2013 at 13:00
  • Are you using having the minimum API level 8 so that you are using getSupportFragmentManager() instead of getFragmentManager() ?
    – GrIsHu
    Mar 22, 2013 at 13:03
  • I'm pretty sure that you call this method before your fragment is attached to an activity.
    – tundundun
    Mar 22, 2013 at 13:09
  • Where are you trying to replace your RecordFailure fragment class ? Your getSupportFragmentManager() manager was not wrong it was right. Try to add this.getActivity().getSupportFragmentManager() and then test.
    – GrIsHu
    Mar 22, 2013 at 13:12

1 Answer 1

0

If you create your fragment via code then you can pass your activity in fragment constructor and use it instead of getActivity(). In another case calling this method before onActivityCreated() makes no sense at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.