RefreshActionItem
RefreshActionItem copied to clipboard
RefreshActionItem object in activity is null
Hi Manuel Peinado,
I'm trying to use this library, I followed your instructions to add this library in my project but I'm experiencing an error in onCreateOptionsMenu method. This is the body of my onCreateOptionsMenu method:
getSupportMenuInflater().inflate(R.menu.activity_home, menu);
MenuItem item = menu.findItem(R.id.refresh_menu);
if (item != null) {
mRefreshActionItem = (RefreshActionItem) item.getActionView();
mRefreshActionItem.setMenuItem(item);
mRefreshActionItem.setMax(100);
mRefreshActionItem.setRefreshActionListener(this);
loadData();
}
// return super.onCreateOptionsMenu(menu);
return true;
and I get the NullPointerException in the following line:
mRefreshActionItem.setMenuItem(item);.
As you can see, only the mRefreshActionItem object can be null. My question is: why do I get this exception???
Can you post your menu xml, i.e. activity_home.xml?
Are you setting the actionViewClass in the menu xml?
android:actionViewClass="com.manuelpeinado.refreshactionitem.RefreshActionItem"
This is my activity_home.xml menu
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/add_menu"
android:icon="@drawable/button_add"
android:showAsAction="ifRoom"
android:title="@string/add_menu"/>
<item
android:id="@+id/refresh_menu"
android:actionViewClass="com.manuelpeinado.refreshactionitem.RefreshActionItem"
android:showAsAction="always"
android:title="@string/refresh_menu"/>
</menu>
There is one more thing: I've tried my application on both API Level 10 and API Level 17 of Android, and it has worked fine on only API Level 10, on API Level 17 it has thrown the exception mentioned above.
How weird. I'll see if I can look into it tomorrow. Thanks for reporting.
Sorry but I can't seem to reproduce your problem.
For Android API Level 17 I use the Android emulator, maybe this is the reason of my problem. I will try my app on a device soon and I will tell you the results.
I Have same issue here !
E/AndroidRuntime(19851): java.lang.NullPointerException E/AndroidRuntime(19851): at com.possebom.json.MainActivity.onCreateOptionsMenu(MainActivity.java:27) E/AndroidRuntime(19851): at com.actionbarsherlock.app.SherlockActivity.onCreatePanelMenu(SherlockActivity.java:184) E/AndroidRuntime(19851): at com.actionbarsherlock.ActionBarSherlock.callbackCreateOptionsMenu(ActionBarSherlock.java:559)
@coxande I was able to reproduce your logcat error by deliberately misspelling:
com.manuelpeinado.refreshactionitem.RefreshActionItem
in the menu xml. Have you checked you have this line exactly:
android:actionViewClass="com.manuelpeinado.refreshactionitem.RefreshActionItem"
? It might not be this but it is best to check.
Have you tested your code on different versions of Android like devilmac?
I have found de problem !
E/AndroidRuntime( 1900): android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x2/d=0x7f01000f a=2} E/AndroidRuntime( 1900): at android.content.res.Resources.loadDrawable(Resources.java:1927) E/AndroidRuntime( 1900): at android.content.res.TypedArray.getDrawable(TypedArray.java:601) E/AndroidRuntime( 1900): at com.manuelpeinado.refreshactionitem.RefreshActionItem.(RefreshActionItem.java:121)
Just edit file : com.manuelpeinado.refreshactionitem.RefreshActionItem and comment these two lines:
case R.styleable.RefreshActionItem_refreshActionItemBackground: // Drawable drawable = a.getDrawable(attr); // mRefreshButton.setBackgroundDrawable(drawable); break;
To get error in logcat I gave made this change in code :
@Override public boolean onCreateOptionsMenu(Menu menu) { getSupportMenuInflater().inflate(R.menu.main, menu); MenuItem item = menu.findItem(R.id.refresh_button); // mRefreshActionItem = (RefreshActionItem)item.getActionView(); mRefreshActionItem = new RefreshActionItem(getApplicationContext()); item.setActionView(mRefreshActionItem); mRefreshActionItem.setMenuItem(item); mRefreshActionItem.setProgressIndicatorType(ProgressIndicatorType.INDETERMINATE); mRefreshActionItem.setRefreshActionListener(this); return true; }
@coxande Thanks for helping out. It's not entirely clear to me, though, whether there is something I can change in the library to make your life easier with regard to this issue. If there is, please let me know.
I had the same exact error as @coxande in a Galaxy Nexus running 4.2.2. My ABS is v4.3.1. May be it has something to do with the error. Anyway, his work-around is working fine for me. Thanks for sharing!
Hi Manuel Peinado,
finally I got the same error (see start issue) on a device with Android 4.3. I hope you fix this problem soon.
same problem here