3

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>
Jakoss
  • 4,647
  • 2
  • 26
  • 40
  • 1
    Looks like `setTitle()` was passed `null`, or you tried to `setTitle()` on an `Object` that was `null`. – MeetTitan Feb 26 '15 at 09:36
  • Well, yes, but why is that happening and what can i do to prevent it, that's my issue here – Jakoss Feb 26 '15 at 10:18
  • Is the error thrown on findviewbyid? If so I suspect you may have implemented the XML incorrectly. If the error is not thrown on that line, check if findviewbyid did not return null. – MeetTitan Feb 26 '15 at 10:24
  • Error is thrown after clicking on menu button on toolbar – Jakoss Feb 26 '15 at 10:33
  • On another phone i got an information that TextView isn't created. But still i have no idea why is that happening – Jakoss Feb 27 '15 at 08:33
  • I believe you're not implementing the XML correctly, as I said. It's trying to inflate something that is not there, hence the NPE. I, unfortunately, am not familiar with `Toolbar` or the support version, so I cannot help without a little research of my own. At least I hope I've identified a portion of your project to take a look at for bugs. Please post R.id.toolbar and either I'll take a look at it. – MeetTitan Feb 27 '15 at 08:37
  • I added toolbar xml definition, but still while creating menu i'm not using any xml in particular, it's system job to create menu items – Jakoss Feb 27 '15 at 11:19

2 Answers2

5

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!

Thpramos
  • 441
  • 4
  • 15
  • Exact same problem keeps occurring for me - clean and rebuild fixes the problem. – Jamie Apr 22 '15 at 08:05
  • 1
    Yes, it helped. But i'm pretty sure it's because of some xamarin update (i tried to clean project earlier) – Jakoss Apr 28 '15 at 14:20
3

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?

  1. Click on Gradle pane, found in the top-right corner of Android Studio
  2. Navigate to your project,then Tasks -> build ->cleanBuildCache
  3. Right click cleanBuildCache, run it.
  4. After this completes be sure to Run -> "Run App" to avoid running the cleanBuildCache script over and over again.
Alanna
  • 31
  • 2