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.