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)
Community
  • 1
  • 1
sjain
  • 23,126
  • 28
  • 107
  • 185
  • Where do you call getFailureDialog()? And how do you declare your fragment: in code or in xml? – tundundun Mar 22 '13 at 12:59
  • please take a look to that: http://stackoverflow.com/questions/6215239/getactivity-returns-null-in-fragment-function its the same problem... – nano_nano Mar 22 '13 at 13:00
  • Are you using having the minimum API level 8 so that you are using `getSupportFragmentManager()` instead of `getFragmentManager()` ? – GrIsHu Mar 22 '13 at 13:03
  • I'm pretty sure that you call this method before your fragment is attached to an activity. – tundundun Mar 22 '13 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 '13 at 13:12
  • RecordFailure is the popup dialogue that is shown on click of item. So I am using fd.show() to show popup. If I use `this.getActivity().getSupportFragmentManager()` then I get syntax error- The method `getActivity()` is undefined for the type new Common.OnSelectedListener(){}. – sjain Mar 22 '13 at 13:21
  • See my updated question. `OnSelectedListener()` is an anonymous interface and I am doing `mCallback.getFailureDialog()` to call the method. – sjain Mar 22 '13 at 13:25
  • @tundundun you are right but if I define that method onattach call then its scope gets limited till that onattach() method. So its never get called because I am calling `mCallback.getFailureDialog()` from other method in the same class. – sjain Mar 22 '13 at 13:29

1 Answers1

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.

tundundun
  • 634
  • 1
  • 5
  • 12