简体   繁体   中英

Android - ActionBar item - onClickListener

I'm trying to implement onClickListener for the item which is a submenu of the ActionBar. Whatever I'm trying to do the result is the same - "Unfortunatelly, application has stopped." However there are no errors during compilation. All seems to be ok, but it is't. What goes wrong here? Thanks for help.

This is my code:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        View view = (View) menu.findItem(R.id.delete).getActionView();
        view.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Execute when actionbar's item is touched
            }
        });
        return true;
    }

And here is the main.xml file where ActionBar and its item is created

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:showAsAction="always"
        android:title="@string/action_settings">
        <menu>
            <item 
                android:id="@+id/delete" 
                android:title="@string/delete" 
                android:showAsAction="always" 
                android:orderInCategory="200"/>
        </menu>
    </item>
</menu>

仅当您具有自定义操作视图(使用setActionView )时, getActionView()才会返回有效对象(不为null )。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM