Tuesday, April 9, 2013

[android help] initLoader() and onCreateOptionsMenu() threading issue


I am fetching some data from SQLite in a Fragment using a CursorLoader. The initLoader() for this is executed in onActivityCreated()...



public class MyFragment extends Fragment implements LoaderCallbacks
{
MenuItem menuItemOne;

@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
getLoaderManager().initLoader(0, null, this);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
inflater.inflate(R.menu.myMenu, menu);
menuItemOne = menu.findItem(R.id.menuItemOne);
}

@Override
public void onLoadFinished(Loader loader, Cursor cursor)
{
menuItemOne.doSomething();
}
}


The above code is simplified for ease of reading. Here is my problem...


  • On a Nexus S, there is no problem.

  • On a Nexus 7, a null pointer arises in onLoadFinished. menuItemOne is null here.

The Nexus S is using Android 4.1.2 and the Nexus 7 is using Android 4.2.2.


  • Should I be running initLoader() elsewhere?

  • Do you think there is a bug/difference in the Android versions?


.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...