I am new in programming under androyd, and I have the following problem: I use the navigation drawer, when I select an item in the menu, I change the fragment. In the layout of the fragment, which I load when I click on a menu item. I have a button and a listview. And in the fragment class I have this code:

public class InboxFragment extends Fragment{ private ListView ll; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.inbox_fragment, container, false); } public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Button filter_bt; // Краш происходит здесь filter_bt = (Button) getView().findViewById(R.id.filter_button); filter_bt.setOnClickListener((OnClickListener) this); } } 

The code is nowhere easier, but for some reason my application crashes. Why does this happen if there is a button? Error - NullPointerException, something returns zero. But what and why?

Here are some bugs:

 FATAL EXCEPTION: main java.lang.NullPointerException at com.example.android.navigationdrawerexample.InboxFragment.onCreate(InboxFragment.java:29) at android.app.Fragment.performCreate(Fragment.java:1673) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:872) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1075) at android.app.BackStackRecord.run(BackStackRecord.java:682) at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1455) at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441) at android.os.Handler.handleCallback(Handler.java:725) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5306) 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:1102) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) at dalvik.system.NativeStart.main(Native Method) 
  • NullPointerException occurs when you try to call an object method on an uninitialized reference. Crash on the 29th line in the onCreate method. If it is filter_bt = (Button) getView (). FindViewById (R.id.filter_button); then probably getView() returns null . - Nofate
  • How then do I "pick up" the button in the fragment? - Godfather
  • one
    onCreate is called before onCreateView . - falstaf

2 answers 2

Transfer the code from OnCreate to OnViewCreated.

Use not getView (), but View, which is passed to OnViewCreated.

Fragment must implement the OnClickListener interface. (implements View.OnClickListener)

 public class InboxFragment extends Fragment implements View.OnClickListener{ private ListView ll; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.inbox_fragment, container, false); } public void onViewCreated(View view, Bundle state) { super.onViewCreated(view, state); Button filter_bt; // Краш происходит здесь filter_bt = (Button) view.findViewById(R.id.filter_button); filter_bt.setOnClickListener(this); } @Override public void OnClick(View view) { //ЛАЛАЛАЛЛАЛА } } 
  • Delaa, still crash - Godfather
  • Can an example code? Thanksgiving - Godfather
  • I'll check it out - Godfather
  • Ok, works! But the button is not pressed, the budding is somehow blocked by somethingGodfather
  • Is the button background static? - Deadkenny

onCreate is called before onCreateView, so you have a getView () method that returns null. Then you have null trying to call findViewById. And get a NullPointerException. I recommend you rewrite the code like this:

 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.inbox_fragment, container, false); Button filter_bt = (Button) v.findViewById(R.id.filter_button); filter_bt.setOnClickListener(this); return v; }