I need to disable headers in BrowseFragment, and add to the ArrayObjectAdapter card. When I do a setHeadersState(HEADERS_DISABLED)
application crushes. If I run setHeadersState(HEADERS_DISABLED)
method later, for example in onLoadFinished
, the application does not crash, but the header bar immediately visible and then hidden, not every time an item is displayed in the adapter.
MainFragment.class
public class MainFragment extends BrowseFragment implements LoaderManager.LoaderCallbacks<List<Module>> {
private static final int MODULES_LOADER_ID = 100;
private ArrayObjectAdapter mRowsAdapter;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setupUIElements();
setupRowAdapter();
setupListeners();
getLoaderManager().initLoader(MODULES_LOADER_ID, null, this);
}
private void setupUIElements() {
setTitle(getActivity().getString(R.string.app_name));
setBadgeDrawable(getResources().getDrawable(R.drawable.title, getActivity().getTheme()));
setHeadersTransitionOnBackEnabled(false);
setBrandColor(getActivity().getResources().getColor(R.color.fastlane_background));
setHeadersState(HEADERS_DISABLED);
}
private void setupRowAdapter() {
mRowsAdapter = new ArrayObjectAdapter(new ListRowPresenter());
setAdapter(mRowsAdapter);
}
private void setupListeners() {
setOnItemViewSelectedListener(new ItemViewSelectedListener());
setOnItemViewClickedListener(new ItemViewClickedListener());
}
private void loadModules(List<Module> modules) {
ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(new ModulesCardPresenter());
for (Module module : modules) {
listRowAdapter.add(module);
}
HeaderItem header = new HeaderItem(0, "Меню");
mRowsAdapter.add(new ListRow(header, listRowAdapter));
}
@Override
public Loader<List<Module>> onCreateLoader(int id, Bundle args) {
switch (id) {
case MODULES_LOADER_ID:
return new ModulesLoader(getActivity());
default:
return null;
}
}
@Override
public void onLoadFinished(Loader<List<Module>> loader, List<Module> data) {
switch (loader.getId()) {
case MODULES_LOADER_ID:
loadModules(data);
break;
}
}
@Override
public void onLoaderReset(Loader<List<Module>> loader) {
}
static class ModulesLoader extends SimpleAsyncTaskLoader<List<Module>> {
public ModulesLoader(Context context) {
super(context);
}
@Override
public List<Module> loadInBackground() {
Call<Modules> mCall = ServiceHolder.getService()
.getModules(Constants.GET_MODULES_REQUEST);
try {
return mCall.execute().body().getModules();
} catch (Exception e) {
e.printStackTrace();
return Collections.EMPTY_LIST;
}
}
}
private final class ItemViewSelectedListener implements OnItemViewSelectedListener {
@Override
public void onItemSelected(Presenter.ViewHolder itemViewHolder, Object item,
RowPresenter.ViewHolder rowViewHolder, Row row) {
}
}
private final class ItemViewClickedListener implements OnItemViewClickedListener {
@Override
public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item,
RowPresenter.ViewHolder rowViewHolder, Row row) {
}
}
Log:
08-18 16:06:08.331 29647-29647/ E/AndroidRuntime: FATAL EXCEPTION: main
Process: , PID: 29647
**java.lang.RuntimeException: Unable to start activity ComponentInfo{.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.app.Fragment.getView()' on a null object reference**
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
**Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.app.Fragment.getView()' on a null object reference**
at android.support.v17.leanback.app.BrowseFragment.onStart(BrowseFragment.java:1511)
at android.app.Fragment.performStart(Fragment.java:2244)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1002)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1130)
at android.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:1958)
at android.app.FragmentController.dispatchStart(FragmentController.java:163)
at android.app.Activity.performStart(Activity.java:6274)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) </code>