NullPointerException in restartLoader LoaderManager method

I have the following interface implementation in my fragment:

@Override public void onReportChanged(Fragment sender, long id, int position) { // Views ein und ausblenden _List.setVisibility(View.GONE); _OnLoading.setVisibility(View.VISIBLE); _NoDataView.setVisibility(View.GONE); _ReportId = id; getLoaderManager().restartLoader(_LM_REPORTS, null, this); }; 

The FragmentActivity report for this fragment, this other fragment (selection list) selected one element. After this, the ListFragment should load the new data with the reported identifier (_ReportId).

But I get a NullPoinerException in this code before moving on to the onCreateLoader-Method. Here is the LogCat:

 05-28 14:24:37.905: E/AndroidRuntime(1775): FATAL EXCEPTION: main 05-28 14:24:37.905: E/AndroidRuntime(1775): java.lang.NullPointerException 05-28 14:24:37.905: E/AndroidRuntime(1775): at android.support.v4.app.LoaderManagerImpl.restartLoader(LoaderManager.java:637) 05-28 14:24:37.905: E/AndroidRuntime(1775): at de.webducer.android.worktime.beta.ui.fragment.ReportListFragment.onReportChanged(ReportListFra gment.java:142) 05-28 14:24:37.905: E/AndroidRuntime(1775): at de.webducer.android.worktime.beta.ui.ReportListActivity.onReportSelected(ReportListActivity.java:97) 05-28 14:24:37.905: E/AndroidRuntime(1775): at de.webducer.android.worktime.beta.ui.fragment.ReportSelectorSpinnerFragment$1.onItemSelected(ReportSelectorSpinnerFragment.java:78) 05-28 14:24:37.905: E/AndroidRuntime(1775): at android.widget.AdapterView.fireOnSelected(AdapterView.java:882) 05-28 14:24:37.905: E/AndroidRuntime(1775): at android.widget.AdapterView.access$200(AdapterView.java:48) 05-28 14:24:37.905: E/AndroidRuntime(1775): at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:848) 05-28 14:24:37.905: E/AndroidRuntime(1775): at android.os.Handler.handleCallback(Handler.java:605) 05-28 14:24:37.905: E/AndroidRuntime(1775): at android.os.Handler.dispatchMessage(Handler.java:92) 05-28 14:24:37.905: E/AndroidRuntime(1775): at android.os.Looper.loop(Looper.java:137) 05-28 14:24:37.905: E/AndroidRuntime(1775): at android.app.ActivityThread.main(ActivityThread.java:4424) 05-28 14:24:37.905: E/AndroidRuntime(1775): at java.lang.reflect.Method.invokeNative(Native Method) 05-28 14:24:37.905: E/AndroidRuntime(1775): at java.lang.reflect.Method.invoke(Method.java:511) 05-28 14:24:37.905: E/AndroidRuntime(1775): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 05-28 14:24:37.905: E/AndroidRuntime(1775): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 05-28 14:24:37.905: E/AndroidRuntime(1775): at dalvik.system.NativeStart.main(Native Method) 

I do not understand why. The same code (but for a different list) works fine on a different FragmentActivity.

+6
source share
1 answer

I found a reason:

 @Override public Loader<Cursor> onCreateLoader(int id, Bundle extra) { CursorLoader returnValue = null; if (_ReportId > 0) { switch (id) { case _LM_REPORTS: returnValue = new CursorLoader(getActivity(), ReportContentProvider.CONTENT_URI_REPORT_DATA, null, ReportTable.COLUMN_ID + " =?", new String[] { String.valueOf(_ReportId) }, null); break; default: break; } } return returnValue; } 

The loader always starts. An exception is the if (_ReportId > 1) condition if (_ReportId > 1) . Without a condition, the code works fine.

+3
source

Source: https://habr.com/ru/post/916844/


All Articles