MenuInflater NullPointerException (NPE)

I just recently published my first app on Google Play, and after I had published it I was playing with it on my Galaxy S3 and it crashed when I tried to delete a page. I know I had this working correctly on my Nexus 7 prior to publishing it, but I didn’t test it on my phone (shame on me :(). So, I went to work trying to figure out what the problem was, and here’s part of the stack trace:

	at com.actionbarsherlock.view.MenuInflater.inflate(MenuInflater.java:109)
	at com.test.app.SomeFragment.onCreateOptionsMenu(SomeFragment.java:1921)
	at android.support.v4.app.Watson.onCreatePanelMenu(Watson.java:55)
	at com.actionbarsherlock.ActionBarSherlock.callbackCreateOptionsMenu(ActionBarSherlock.java:559)
	at com.actionbarsherlock.internal.ActionBarSherlockNative.dispatchCreateOptionsMenu(ActionBarSherlockNative.java:65)
	at com.actionbarsherlock.app.SherlockFragmentActivity.onCreatePanelMenu(SherlockFragmentActivity.java:165)
	at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:446)
	at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:820)
	at com.actionbarsherlock.internal.ActionBarSherlockNative.dispatchInvalidateOptionsMenu(ActionBarSherlockNative.java:52)
	at com.actionbarsherlock.app.SherlockFragmentActivity.invalidateOptionsMenu(SherlockFragmentActivity.java:150)
	at com.actionbarsherlock.app.SherlockFragmentActivity.supportInvalidateOptionsMenu(SherlockFragmentActivity.java:156)
	at android.support.v4.app.Fragment.setHasOptionsMenu(Fragment.java:763)
	at com.test.app.SomeFragment.onCreate(SomeFragment.java:190)
	at android.support.v4.app.Fragment.performCreate(Fragment.java:1437)
	at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:877)
	at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
	at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
	at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
	at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
	at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
	at android.support.v4.view.ViewPager.populate(ViewPager.java:1064)
	at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:548)
	at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:507)
	at android.support.v4.view.ViewPager.dataSetChanged(ViewPager.java:905)
	at android.support.v4.view.ViewPager$PagerObserver.onChanged(ViewPager.java:2772)
	at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37)
	at android.support.v4.view.PagerAdapter.notifyDataSetChanged(PagerAdapter.java:276)
	at com.test.app.SomeActivity.updateView(SomeActivity.java:624)
	at com.test.app.SomeActivity$LoadInfo.onPostExecute(SomeActivity.java:353)
	at com.test.app.SomeActivity$LoadInfo.onPostExecute(SomeActivity.java:1)
	at android.os.AsyncTask.finish(AsyncTask.java:631)
	at android.os.AsyncTask.access$600(AsyncTask.java:177)
	at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
	at android.os.Handler.dispatchMessage(Handler.java:99)
	at android.os.Looper.loop(Looper.java:137)
	at android.app.ActivityThread.main(ActivityThread.java:4950)
	at java.lang.reflect.Method.invokeNative(Native Method)
	at java.lang.reflect.Method.invoke(Method.java:511)
	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
	at dalvik.system.NativeStart.main(Native Method)

After checking the stack trace and doing some research, I narrowed it down to an issue with setHasOptionsMenu(true) and I thought it might be a problem with the ActionBarSherlock Library. Well, I stumbled upon this post and found that setHasOptionsMenu(true) was set in onActivityCreated(). I had set it in onCreate(), since when I had looked at how to use it that’s where everyone seemed to put it…at least in the posts I looked at. Well, it was already broken, so I had nothing to lose and the other stuff I had tried didn’t fix it. I moved setHasOptionsMenu(true) from onCreate() to OnActivityCreated() and then ran a test. Bingo! That fixed the problem on my phone! I tested on my other two tablets (Nexus 7 and Motorola Xoom) and it worked like it should on both of those as well.

Anyway, just throwing this out there in case anyone else runs into this problem.

Android Tutorial: Implement A Shake Listener

So I’ve been playing around with Android and have a little app that I wanted the user to be able to shake the phone and have something happen. I did some digging and the following is a tutorial on how to setup a shake listener to capture a shake and then do whatever you want.

This is by no means something that I’ve created, I just used examples that I found on stackoverflow.com. Also, this uses G-force to calculate the shake threshold. Many thanks to Peterdk and Akos Cz for their input and answer for this solution. Peterdk recommended using the G-Force app by Blake La Pierre on Google Play Store if you want to get the actual G-force value on your phone and tweak it in the code below.

Long story short, there are 5 things that need to happen to get this to work:

  1. Create a new class named ShakeDetector
  2. Define variables for sensor manager, shake detector and accelerometer
  3. Add/register the sensor manager and listener in the onCreate() method in your activity
  4. Configure onResume and onPause to activate/deactivate the accelerometer
  5. Edit the AndroidManifest.xml file to require the device to have an accelerometer

Continue reading

© 2020 Jason McReynolds

Theme by Anders NorénUp ↑