尝试在Android中调用ContextMenu时遇到问题。
Android Studio在一个float小部件中设置一个侦听器,它可以工作:
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
registerForContextMenu(fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
openContextMenu(fab);
}
});
但是当用户按下NavigationMenu的项目时尝试调用它,我不确定正确的声明方式:
NavigationMenuItemView navigationItemView = (NavigationMenuItemView) findViewById(R.id.nav_call);
然后使用:
openContextMenu(navigationItemView);
我有这个:
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
NavigationMenuItemView navigationItemView = (NavigationMenuItemView) findViewById(R.id.nav_call); //Declare view maybe like this?
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_home) {
//home
} else if (id == R.id.nav_call) {
//Here is my problem
openContextMenu(navigationItemView);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
activity_main_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_home"
android:icon="@drawable/ic_home_24dp"
android:title="Home" />
<item
android:id="@+id/nav_call"
android:icon="@drawable/ic_call_24dp"
android:title="Call" />
</group>
当然,它不起作用,因为当我按下按钮应用程序将崩溃并给我Null指针异常:
日志:
04-18 00:40:45.561 27548-27548 / com.android.test01 E / eglCodecCommon:**** ERROR unknown type 0xb8a05080(glSizeof,80) 04-18 00:40:45.593 27548-27548 / com.android.test01 D / AndroidRuntime:关闭VM 04-18 00:40:45.593 27548-27548 / com.android.test01 W / dalvikvm:threadid = 1:线程退出未捕获的异常(组= 0xa4d14b20) 04-18 00:40:45.593 27548-27548 / com.android.test01 E / AndroidRuntime:FATAL EXCEPTION:main 处理:com.android.test01,PID:27548 显示java.lang.NullPointerException 在android.app.Activity.openContextMenu(Activity.java:2933) 在com.android.test01.MainActivity.onNavigationItemSelected(MainActivity.java:109) 在android.support.design.widget.NavigationView $ 1.onMenuItemSelected(NavigationView.java:151) 在android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811) 在android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152) 在android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958) 在android.support.design.internal.NavigationMenuPresenter $ 1.onClick(NavigationMenuPresenter.java:318) 在android.view.View.performClick(View.java:4438) 在android.view.View $ PerformClick.run(View.java:18422) 在android.os.Handler.handleCallback(Handler.java:733) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:136) 在android.app.ActivityThread.main(ActivityThread.java:5001) at java.lang.reflect.Method.invokeNative(Native Method) 在java.lang.reflect.Method.invoke(Method.java:515) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:785) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 在dalvik.system.NativeStart.main(本地方法)
使用导航项调用ContextMenu的正确方法是什么?
感谢阅读!问候。
答案 0 :(得分:0)
好吧,我认为这是不可能的,正如另一位用户所说,这没有意义。
然后我使用了一个子菜单。
我刚刚编辑了菜单XML文件:
activity_main_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_home"
android:icon="@drawable/ic_home_24dp"
android:title="Home" />
<item
android:id="@+id/nav_call"
android:icon="@drawable/ic_call_24dp"
android:title="Call">
<menu>
<item
android:id="@+id/nav_call1"
android:icon="@drawable/ic_call_24dp"
android:title="Call to A" />
<item
android:id="@+id/nav_call2"
android:icon="@drawable/ic_call_24dp"
android:title="Call to B" />
<item
android:id="@+id/nav_call3"
android:icon="@drawable/ic_call_24dp"
android:title="Call to C" />
</menu>
</item>
</group>
并在Activity.java文件中进行验证:
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_home) {
//Home activity
} else if (id == R.id.nav_call1) {
//foo CallNumber(1);
} else if (id == R.id.nav_call2) {
//foo CallNumber(2);
} else if (id == R.id.nav_call3) {
//foo CallNumber(3);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
感谢阅读,问候!