OnCreateView NullPointerException

549 Views Asked by At

I have an app up in the Google Play store. I am getting crash notifications through Crashlytics/Fabric, so I don't have a true LogCat to reference. Also, I am unable to figure out how to replicate this bug on my own system.

The error I'm getting from Crashlytics is as follows:

com.company.appname.Fragment.onCreateView (Fragment.java:48)

Here's the pertinent detail for the class declaration:

import android.support.v4.app.Fragment;
public class MyTypeFragment extends Fragment {...

Here's the code from that portion of the fragment:

44 mRestartButton = (Button)contentView.findViewById(R.id.restart_button);
45 mRestartButton.setTypeface(regularTf);
46 mRestartButton.setOnClickListener(new View.OnClickListener() {
47     @Override
48     public void onClick(View v) {
49         getFragmentManager().popBackStack("startingFragment", 0);
50     }
51 });

I find it interesting that the crash report is listing the crash as being in the onCreateView() method, yet line 48 is the declaration of the onClick() method on my button's onClickListener.

I've looked into the following questions:

Both of them seem to indicate the problem being related to an incorrect id when accessing the XML for this fragment. I don't think that's the problem, however, as this is an XML portion for this fragment (and there's only one layout for this fragment):

<LinearLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"

    >

    <Button
        android:id="@+id/restart_button"
        android:layout_gravity="right"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:paddingBottom="8dp"
        android:paddingTop="8dp"
        android:text="Restart"

        />

One thing that might be of concern: I use this same paradigm on many of my fragments. The app's function is to drill through a series of questions to achieve an answer. The "reset" button allows them to go all the way back to the beginning without going through any of the intermediate fragments they've seen along the way. Each of those fragments has a button on it that is named with R.id.reset_button. As I said, though, I've attempted to replicate this problem on my end and cannot figure out how it occurs.

I've read some stuff that indicates this problem might be related to a fragment somehow being detached from its activity, but I've even tried leaving my app and returning, and still can't replicate this bug.

1

There are 1 best solutions below

11
On

I assume the code works in many devices and the bug is not easily reproducible. The only recommendation I have and it is my own coding technique is to move the posted code to onViewCreated() method instead of onCreateView(). The reason is sometimes onCreateView could not finish processing the GUI layout in time. At least, this is easy to try. Good luck in testing with different devices.

Sample code:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.list_layout, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
   Context thisContext = view.getContext();

   mRestartButton = (Button) view.findViewById(R.id.restart_button);
   mRestartButton.setTypeface(regularTf);
   mRestartButton.setOnClickListener(new View.OnClickListener() {
   ...

Notes:

  • onCreateView() is changed to only returning the root view to pass onto onViewCreated()
  • onViewCreated() is now doing the UI work.