I can not reach the database from the fragment

@Override public void onActivityCreated(Bundle savedInstanceState) { try { Log.i("LOG_Fragemnt ", "ROW "); DataBase sqh = new DataBase(getActivity()); 

From the activit I give him the context and it works, but it doesn’t work in the fragment,

Displays error

12-18 05: 22: 37.983 4052-4052 / com.example.eldos.callreport E / AndroidRuntime ﹕ FATAL EXCEPTION: main Process: com.example.eldos.callreport, PID: 4052 java.lang.NullPointerException

What is wrong doing?

  • Need more info. Where exactly falls. Bring more code if necessary. - andreich

2 answers 2

in onActivityCreated first method must be called

 super.onActivityCreated(); 
  • 2
    No, not necessarily the first, you can make a call at the very end of the method, but you need to be aware of this. - andreich
  • @Andreich, yes, you're right, I put it wrong. - Vladyslav Matviienko

This is how it works, and although it just restarted the studio

 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { LocalBroadcastManager.getInstance(getActivity()).registerReceiver(receiver, new IntentFilter("setAdaperToListView")); sqh = new DataBase(getActivity()); sqdb = sqh.getWritableDatabase(); final View content = inflater.inflate(R.layout.fragment2,null); return content; }