Reputation: 5235
I need to programmatically create menu (exactly the same as tabs list) to power a navigation in my app. So i did code like that
public override bool OnCreateOptionsMenu (IMenu menu)
{
base.OnCreateOptionsMenu (menu);
for (int i = 0; i < fragmentsTabs.Count; i++) {
menu.Add(0,100 + i,i,new Java.Lang.String(fragmentsTabs.GetItem(i).Key));
}
return true;
}
But when i click on menu button i get an exception like that:
[AndroidRuntime] java.lang.NullPointerException
[AndroidRuntime] at android.support.v7.internal.view.menu.ListMenuItemView.setTitle(ListMenuItemView.java:117)
[AndroidRuntime] at android.support.v7.internal.view.menu.ListMenuItemView.initialize(ListMenuItemView.java:104)
[AndroidRuntime] at android.support.v7.internal.view.menu.MenuPopupHelper$MenuAdapter.getView(MenuPopupHelper.java:377)
[AndroidRuntime] at android.support.v7.internal.view.menu.MenuPopupHelper.measureContentWidth(MenuPopupHelper.java:219)
[AndroidRuntime] at android.support.v7.internal.view.menu.MenuPopupHelper.tryShow(MenuPopupHelper.java:153)
[AndroidRuntime] at android.support.v7.widget.ActionMenuPresenter$OpenOverflowRunnable.run(ActionMenuPresenter.java:752)
[AndroidRuntime] at android.os.Handler.handleCallback(Handler.java:733)
[AndroidRuntime] at android.os.Handler.dispatchMessage(Handler.java:95)
[AndroidRuntime] at android.os.Looper.loop(Looper.java:136)
[AndroidRuntime] at android.app.ActivityThread.main(ActivityThread.java:5017)
[AndroidRuntime] at java.lang.reflect.Method.invokeNative(Native Method)
[AndroidRuntime] at java.lang.reflect.Method.invoke(Method.java:515)
[AndroidRuntime] at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
[AndroidRuntime] at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
[AndroidRuntime] at dalvik.system.NativeStart.main(Native Method)
[AndroidRuntime] at android.support.v7.internal.view.menu.MenuPopupHel08.448 W/ActivityManager( 575): Force finishing activity pl.media30.android.parlament/aplikacjaparlamentandroid.PersonDetailsActivity
I'm using Toolbar from Support library:
Toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
SetSupportActionBar(Toolbar);
SupportActionBar.SetHomeButtonEnabled(true);
SupportActionBar.SetDisplayHomeAsUpEnabled(true);
On another phone i get more information about NullPointerException:
[AndroidRuntime] java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
[AndroidRuntime] at android.support.v7.internal.view.menu.ListMenuItemView.setTitle(ListMenuItemView.java:117)
[AndroidRuntime] at android.support.v7.internal.view.menu.ListMenuItemView.initialize(ListMenuItemView.java:104)
[AndroidRuntime] at android.support.v7.internal.view.menu.MenuPopupHelper$MenuAdapter.getView(MenuPopupHelper.java:377)
[AndroidRuntime] at android.support.v7.internal.view.menu.MenuPopupHelper.measureContentWidth(MenuPopupHelper.java:219)
[AndroidRuntime] at android.support.v7.internal.view.menu.MenuPopupHelper.tryShow(MenuPopupHelper.java:153)
[AndroidRuntime] at android.support.v7.widget.ActionMenuPresenter$OpenOverflowRunnable.run(ActionMenuPresenter.java:752)
[AndroidRuntime] at android.os.Handler.handleCallback(Handler.java:739)
[AndroidRuntime] at android.os.Handler.dispatchMessage(Handler.java:95)
[AndroidRuntime] at android.os.Looper.loop(Looper.java:135)
[AndroidRuntime] at android.app.ActivityThread.main(ActivityThread.java:5221)
[AndroidRuntime] at java.lang.reflect.Method.invoke(Native Method)
[AndroidRuntime] at java.lang.reflect.Method.invoke(Method.java:372)
[AndroidRuntime] at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
[AndroidRuntime] at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Anybody have any idea why the TextView isn't created? What can be an issue. I have a lot of fragments in this activity, but non of them is using menu. If i create single icon, for example search, everything is working. Icon is properly created and onclick is well handled. Problem appears only when menu items are not showed as action
@EDIT
My axml for toolbar is:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
android:scaleType="fitEnd"
android:src="@drawable/side" />
</android.support.v7.widget.Toolbar>
Upvotes: 3
Views: 776
Reputation: 31
Had exact same issue. For me the solution was not to "Build -> Clean" the project. This didn't work!
What did work was Surbhit Rao answer to another stack overflow post: How to project clean in android studio?
Upvotes: 3
Reputation: 441
I was having the same trouble for about 2 hours. Then after trying everything I decided to "Clean All" the projects. After I did that everything worked like a charm. One other thing that I changed in the project and might help was replacing all normal Fragment imports with the Support.v4 ones.
Hope it helps!
Upvotes: 5