1

I'm trying to add ImageButton inside the Toolbar

In my Activity layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MyActivity">
<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/primary"
    android:elevation="4dp"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/Theme"
    android:transitionName="actionBar" >
    <ImageButton
        android:id="@+id/updateButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="handleUpdate"
        android:src="@drawable/ic_media_play"
        android:layout_gravity="end"/>
</android.support.v7.widget.Toolbar>
...
</RelativeLayout>

In MyActivity code:

public void handleUpdate(View view) {
    update();
}

But when I click updateButton application crashes with the following exception:

 java.lang.IllegalStateException: Could not find a method handleUpdate(View) in the activity class android.support.v7.internal.view.ContextThemeWrapper for onClick handler on view class android.widget.ImageButton with id 'updateButton'
        at android.view.View$1.onClick(View.java:3801)
        at android.view.View.performClick(View.java:4421)
        at android.view.View$PerformClick.run(View.java:17903)
        at android.os.Handler.handleCallback(Handler.java:730)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:213)
        at android.app.ActivityThread.main(ActivityThread.java:5225)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
        at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoSuchMethodException: handleUpdate

It crashes even if I change handleUpdate return type from void to boolean.

satorikomeiji
  • 469
  • 6
  • 16
  • I think its better to use menuitems with toolbar.. – Emil Jul 23 '15 at 07:39
  • Instead of doing like this, You can implement `OnClickListener` in Java Code. Take a handle of `ToolBar` view using `findViewById`, and from toolbar view take handle of `ImageView` and write `OnClickListener`. If your ImageView is sort of icon then I recommend you to use `MenuItem` – Kavin Prabhu Jul 23 '15 at 07:42
  • I understand that, however I'm still curious why is this happening. – satorikomeiji Jul 23 '15 at 07:45

2 Answers2

1

Remove the android:theme attribute of your Toolbar, and set it in styles.xml. Your onClick gets the wrong Context (android.support.v7.internal.view.ContextThemeWrapper is not your Activity). I advise you to use listeners in code, not set them thru XML.

Stackoverflow questions/answers explaining what is happening:

Community
  • 1
  • 1
Marko
  • 20,385
  • 13
  • 48
  • 64
0

try doing

updateButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                 //update
            }
});

when you initialize the toolbar

R. Adang
  • 593
  • 3
  • 13