-1

I have a class which uses AndroidAnnotations and @ViewById annotation. I've seen many crashes because of some of the fields with @ViewById annotations being null.

Attempt to invoke virtual method 'void com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayout.setRefreshing(boolean)' on a null object reference a problem with two of them which

Here's part of the class:

@EFragment(R.layout.fragment_categories_tab)
public class CategoriesTabFragment extends BaseFragment implements CategoryAdapter.OnItemSelected, SwipyRefreshLayout.OnRefreshListener {
    @ViewById(R.id.swipeRefreshLayout)
    protected SwipyRefreshLayout swipeRefreshLayout;
    @ViewById(R.id.placeholder_textview)
    protected CustomTextView statusLabel;

    private void loadChildren() {
        AndroidUtil.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                swipeRefreshLayout.setRefreshing(true); //crash happens here, but not always
            }
        });
    }

    categoryService.getCategories(new CommListener<Pair<Category[], Boolean>>() {
            @Override
            public void onSuccess(NetworkResult result, Pair<Category[], Boolean> object) {
                AndroidUtil.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (getActivity() != null) {
                            statusLabel.setText(""); //another point that crash happens, but again sometime and only in early stage of the fragment's life
                        }
                    }
            }
    }
}

And this is the AndroidUtil class:

public class AndroidUtil {
    private static Handler mainHandler = new Handler(Looper.getMainLooper());

    public static void runOnUiThread(Runnable runnable) {
        mainHandler.post(runnable);
    }
}

I'm new to Android; and I don't seem to find the reason. I'd appreciate any help, in advance.

Erfan
  • 143
  • 10
  • It because fragment you are calling it before fragment call onCreateView – Selvin Aug 09 '17 at 08:11
  • @Selvin 'm using AndroidAnnotations and everything get called from @ AfterView afterView() method and afterward. Isn't the fragment attached to the Activity in these points of time? – Erfan Aug 09 '17 at 08:33

1 Answers1

0

Change AndroidUtil.runOnUiThread to getActvity() and check null before using.

In your case, it's crashing because sometimes your fragment is not attached to the Activity so all you layout inside is null.

Muthukrishnan Rajendran
  • 11,122
  • 3
  • 31
  • 41
  • 1.how do you mean by change it to getActivity()? 2.All of these methods get called in @AfterView afterView() method doesn't this mean that at this point Fragment is attached to the Activity? – Erfan Aug 09 '17 at 08:21
  • You are doing asynchronous call, so the response may come with a delay, In that time your Fragment might not be attached to the Activity, So the View object will be null. – Muthukrishnan Rajendran Aug 09 '17 at 08:34