0

I tried to get activity's context from Fragment's onAttach() method.

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    context = activity.getApplicationContext();

     obj = new MySQLiteHelper(context);
   ...

}

But, I'm still getting NullPointerException. How should I structure my code to avoid this?

Here is my Fragment code :

public class ListTab extends Fragment {

    View view;

    Context context ;       // I just created reference here
    MySQLiteHelper obj;    // and initialised in onAttach()

    String[][] table;
    byte[][] images;
    Bitmap[] bitmap;

    String[] title = new String[10];
    String[] init_price = new String[10];
    String[] cur_price = new String[10];    

    int len,i;

    private OnFragmentInteractionListener mListener;

    public ListTab() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

     // not sure when this is called, so left this empty.
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // Inflate the layout for this fragment

        view = inflater.inflate(R.layout.fragment_list_items, container, false);

        obj.open(); //I'm getting an exception here - NullPointerException
        obj.read();

        table = obj.getTable();
        images = obj.getImages();

        len = obj.getLength();

      ...
      //some code to inflate fragment with Listview

    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        context = activity.getApplicationContext();

        obj = new MySQLiteHelper(context);
     ...
     //some code
    }

 ... 
// some other methods             
}

What is the cause of the problem? Can anyone explain me please?

Nikhil
  • 6,493
  • 10
  • 31
  • 68
  • Always provide a stacktrace if your question involves debugging, makes the process for readers a whole lot easier. – Marcus Mar 15 '15 at 14:07
  • The problem was not with the context. It was with bitmap array in my code. Commenting that worked. Will solve that issue now. Thanks for your time folks. – Nikhil Mar 15 '15 at 14:22

1 Answers1

0

Android provides you a getActivity() in a fragment for the same scenario.

This is from the documentation for onAttach().The onCreate is called immedaitely after onAttach().

I think if you check the onCreate(),you could use that instead for your purpose.

If you carefully read the fragment lifecycle,the activity's onActivityCreated onccurs after onAttach().You need the activity created to get context.So if possible you could shift your code to the onCreate of the fragment.

Droidekas
  • 3,464
  • 2
  • 26
  • 40
  • Yes, I used getActivity(), but it returns the context only when Fragment is loaded, otherwise before that it returns NULL. I referred : http://stackoverflow.com/questions/8215308/using-context-in-fragment – Nikhil Mar 15 '15 at 14:05
  • is there any specific reason you want context before the fragment is loaded? – Droidekas Mar 15 '15 at 14:07
  • Yes, I need the context , so that I can use it to create MySQLiteHelper object. I have to get some data from my database and use that data to update my Fragment UI i.e. listview. – Nikhil Mar 15 '15 at 14:09
  • ok so you could initialize that in the onCreate() of the fragment also right? – Droidekas Mar 15 '15 at 14:13
  • Yep @Droidekas. We can do that. Somehow, it worked now. The problem was with the bitmap image in my code, not the context. Commenting that, worked. Thanks for your time. – Nikhil Mar 15 '15 at 14:21
  • And from a design perspective,your SQLite CRUD should be a singleton .You should initialize that when you first start your first activity and keep calling that object instance henceforth.That way,you do nto face the issues that you are currently facing – Droidekas Mar 15 '15 at 14:21
  • Oh! Okay! I'm very new to Android. Will take your suggestion mate. Thanks :) – Nikhil Mar 15 '15 at 14:23