1


I am trying the new Toolbar widget in Android 5.0. So I set the toolbar's navigation icon and tried to use it. But when I tap on the back button on the Toolbar I get the following error:

    FATAL EXCEPTION: main
java.lang.IllegalArgumentException: Invalid payload item type
at android.util.EventLog.writeEvent(Native Method)
at android.app.Activity.onMenuItemSelected(Activity.java:2628)
at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:350)
at android.support.v7.app.ActionBarActivity.onMenuItemSelected(ActionBarActivity.java:155)
at android.support.v7.app.ActionBarActivityDelegate$1.onMenuItemSelected(ActionBarActivityDelegate.java:74)
at android.support.v7.widget.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:44)
at android.support.v7.internal.widget.ToolbarWidgetWrapper$1.onClick(ToolbarWidgetWrapper.java:190)
at android.view.View.performClick(View.java:4211)
at android.view.View$PerformClick.run(View.java:17267)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4898)
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:1006)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
at dalvik.system.NativeStart.main(Native Method)

Moreover, if I use phone back button I get no error. It just quits the Settings Activity.
This is my Settings Activity:

public class ActivitySettings extends ActionBarActivity {

    private Toolbar toolbar;
    private DrawerLayout drawerLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        initToolBar();

        // Display the fragment as the main content.
        getFragmentManager().beginTransaction()
                .replace(R.id.container, new FragmentSettings()).commit();
    }

    private void initToolBar() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);

        if (toolbar != null) {
            // toolbar.setNavigationIcon(R.drawable.ic_drawer);
            toolbar.setTitle(Html.fromHtml("<font color=\"white\">"
                    + getString(R.string.settings_header_title) + "</font>"));              
            setSupportActionBar(toolbar);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }
}

And my AndroidManifest:

        <activity
            android:name=".ActivitySettings"
            android:label="@string/settings_header_title"
            android:parentActivityName=".ActivityMain" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".ActivityMain" />
        </activity>

I tried cleaning the solution, removing mafifest parent activity name and setting the click listener to the toolbar

toolbar.setNavigationOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    finish();

                }
            });

but it gave the same error.

Thanks in advance!
Device: Samsung Galaxy S3
Android: 4.1.2
Screenshot

Savelii Zagurskii
  • 997
  • 1
  • 9
  • 9

3 Answers3

2

The problem was in the

toolbar.setTitle(Html.fromHtml("<font color=\"white\">sample text</font>"));

Somehow, Android does not allow setting title with HTML strings. (it allows but when you click the back button it crashes) I tried to add CDATA but it didn't work.

Html.fromHtml("<![CDATA[<font color=\"white\">sample text</font>]]>");

As I had set the title in my AndroidManifest.xml

android:label="@string/settings_header_title"

I just had to remove the line with setting the title.

More info here and here.

Community
  • 1
  • 1
Savelii Zagurskii
  • 997
  • 1
  • 9
  • 9
1

I have the navigation working with the following implementation.

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
        case android.R.id.home:
              //implement action 
         }
}
UDI
  • 1,602
  • 1
  • 14
  • 13
0

The problem is actually that you have a non-String title set on the Toolbar at the time you're calling setSupportActionBar. It will end up in ToolbarWidgetWrapper, where it will be used when you click the navigation menu. You can use any CharSequence (e.g. Spannable) after calling setSuppportActionBar.

David Burström
  • 1,592
  • 14
  • 14