Android使用AlertDialog实现弹出菜单

简介: 本文分享了在Android开发中使用AlertDialog实现弹出菜单的方法,并通过代码示例和错误处理,展示了如何避免因资源ID找不到导致的crash问题。

最近又开始捣鼓APP,许多api , class都忘记怎么用了,楼下使用AlertDialog实现个弹出菜单,结果直接crash,查了半天,终于即将,记录一下……

1 实现代码

  AlertDialog.Builder mBuilder = new AlertDialog.Builder(mContext);//will be crash: android.content.res.Resources$NotFoundException: Resource ID #0x0
        mBuilder = new AlertDialog.Builder(this);
        //填充列表数据
        final String[] items = new String[]{
   
   "选项1", "选项2", "选项3"};
        mBuilder.setTitle("选择一项"); // 设置标题
        mBuilder.setItems(items, new DialogInterface.OnClickListener() {
   
   
            @Override
            public void onClick(DialogInterface dialog, int which) {
   
   
                Toast.makeText(mContext, "clicked:"+ items[which], Toast.LENGTH_SHORT).show();
            }
        });

        mBtnApp6.setOnClickListener(new View.OnClickListener() {
   
   
            @Override
            public void onClick(View v) {
   
   
               // 创建并显示对话框
                mBuilder.create().show();//界面上mBtnApp6按钮点击后执行dialog显示

            }

        });

2 实现效果

在这里插入图片描述

3 增加数据项的效果

在这里插入图片描述

4 遇到的错误

  • android.content.res.Resources$NotFoundException: Resource ID #0x0
  • 原因:new AlertDialog.Builder(mContext) , crash
  •      或 new AlertDialog.Builder(getApplicationContext()) , crash
    
  • 解决办法:new AlertDialog.Builder(this), OK

--------- beginning of crash
04-18 17:08:40.940  6011  6011 E AndroidRuntime: FATAL EXCEPTION: main
04-18 17:08:40.940  6011  6011 E AndroidRuntime: Process: com.cictech.xlauncher, PID: 6011
04-18 17:08:40.940  6011  6011 E AndroidRuntime: android.content.res.Resources$NotFoundException: Resource ID #0x0
04-18 17:08:40.940  6011  6011 E AndroidRuntime:        at android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:237)
04-18 17:08:40.940  6011  6011 E AndroidRuntime:        at android.content.res.Resources.loadXmlResourceParser(Resources.java:2400)
04-18 17:08:40.940  6011  6011 E AndroidRuntime:        at android.content.res.Resources.getLayout(Resources.java:1252)
04-18 17:08:40.940  6011  6011 E AndroidRuntime:        at android.view.LayoutInflater.inflate(LayoutInflater.java:530)
04-18 17:08:40.940  6011  6011 E AndroidRuntime:        at android.view.LayoutInflater.inflate(LayoutInflater.java:479)
04-18 17:08:40.940  6011  6011 E AndroidRuntime:        at androidx.appcompat.app.AlertController$AlertParams.createListView(AlertController.java:989)
04-18 17:08:40.940  6011  6011 E AndroidRuntime:        at androidx.appcompat.app.AlertController$AlertParams.apply(AlertController.java:965)
04-18 17:08:40.940  6011  6011 E AndroidRuntime:        at androidx.appcompat.app.AlertDialog$Builder.create(AlertDialog.java:984)
04-18 17:08:40.940  6011  6011 E AndroidRuntime:        at com.cictech.xlauncher.MainActivity$7.onClick(MainActivity.java:188)
04-18 17:08:40.940  6011  6011 E AndroidRuntime:        at android.view.View.performClick(View.java:7448)
04-18 17:08:40.940  6011  6011 E AndroidRuntime:        at android.view.View.performClickInternal(View.java:7425)
04-18 17:08:40.940  6011  6011 E AndroidRuntime:        at android.view.View.onKeyUp(View.java:15060)
04-18 17:08:40.940  6011  6011 E AndroidRuntime:        at android.view.KeyEvent.dispatch(KeyEvent.java:2892)
04-18 17:08:40.940  6011  6011 E AndroidRuntime:        at android.view.View.dispatchKeyEvent(View.java:14247)
04-18 17:08:40.940  6011  6011 E AndroidRuntime:        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1954)
04-18 17:08:40.940  6011  6011 E AndroidRuntime:        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1959)
04-18 17:08:40.940  6011  6011 E AndroidRuntime:        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1959)
04-18 17:08:40.940  6011  6011 E AndroidRuntime:        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1959)
04-18 17:08:40.940  6011  6011 E AndroidRuntime:        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1959)
04-18 17:08:40.940  6011  6011 E AndroidRuntime:        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1959)
04-18 17:08:40.940  6011  6011 E AndroidRuntime:        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1959)
04-18 17:08:40.940  6011  6011 E AndroidRuntime:        at com.android.internal.policy.DecorView.superDispatchKeyEvent(DecorView.java:476)
04-18 17:08:40.940  6011  6011 E AndroidRuntime:        at com.android.internal.policy.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1861)
04-18 17:08:40.940  6011  6011 E AndroidRuntime:        at android.app.Activity.dispatchKeyEvent(Activity.java:4085)
04-18 17:08:40.940  6011  6011 E AndroidRuntime:        at androidx.core.app.ComponentActivity.superDispatchKeyEvent(ComponentActivity.java:126)
04-18 17:08:40.940  6011  6011 E AndroidRuntime:        at androidx.core.view.KeyEventDispatcher.dispatchKeyEvent(KeyEventDispatcher.java:86)
04-18 17:08:40.940  6011  6011 E AndroidRuntime:        at androidx.core.app.ComponentActivity.dispatchKeyEvent(ComponentActivity.java:144)
04-18 17:08:40.940  6011  6011 E AndroidRuntime:        at androidx.appcompat.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:604)
04-18 17:08:40.940  6011  6011 E AndroidRuntime:        at androidx.appcompat.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:60)
04-18 17:08:40.940  6011  6011 E AndroidRuntime:        at androidx.appcompat.app.AppCompatDelegateImpl$AppCompatWindowCallback.dispatchKeyEvent(AppCompatDelegateImpl.java:3413)
04-18 17:08:40.940  6011  6011 E AndroidRuntime:        at com.android.internal.policy.DecorView.dispatchKeyEvent(DecorView.java:390)
04-18 17:08:40.940  6011  6011 E AndroidRuntime:        at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:5977)
04-18 17:08:40.940  6011  6011 E AndroidRuntime:        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5845)
04-18 17:08:40.940  6011  6011 E AndroidRuntime:        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5340)
04-18 17:08:40.940  6011  6011 E AndroidRuntime:        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5397)
04-18 17:08:40.940  6011  6011 E AndroidRuntime:        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5363)
04-18 17:08:40.940  6011  6011 E AndroidRuntime:        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:5515)
04-18 17:08:40.940  6011  6011 E AndroidRuntime:        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5371)
04-18 17:08:40.940  6011  6011 E AndroidRuntime:        at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:5572)
04-18 17:08:40.940  6011  6011 E AndroidRuntime:        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5344)
04-18 17:08:40.940  6011  6011 E AndroidRuntime:        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5397)
04-18 17:08:40.940  6011  6011 E AndroidRuntime:        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5363)
04-18 17:08:40.940  6011  6011 E AndroidRuntime:        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5371)
04-18 17:08:40.940  6011  6011 E AndroidRuntime:        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5344)
04-18 17:08:40.940  6011  6011 E AndroidRuntime:        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5397)
04-18 17:08:40.940  6011  6011 E AndroidRuntime:        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5363)
04-18 17:08:40.940  6011  6011 E AndroidRuntime:        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:5548)
04-18 17:08:40.940  6011  6011 E AndroidRuntime:        at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:5706)
04-18 17:08:40.940  6011  6011 E AndroidRuntime:        at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:3179)
04-18 17:08:40.940  6011  6011 E AndroidRuntime:        at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:2721)
04-18 17:08:40.943  6011  6011 E AndroidRuntime:        at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:2712)
04-18 17:08:40.943  6011  6011 E AndroidRuntime:        at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:3156)
04-18 17:08:40.943  6011  6011 E AndroidRuntime:        at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:143)
04-18 17:08:40.943  6011  6011 E AndroidRuntime:        at android.os.MessageQueue.nativePollOnce(Native Method)
04-18 17:08:40.943  6011  6011 E AndroidRuntime:        at android.os.MessageQueue.next(MessageQueue.java:335)
04-18 17:08:40.943  6011  6011 E AndroidRuntime:        at android.os.Looper.loop(Looper.java:183)
04-18 17:08:40.943  6011  6011 E AndroidRuntime:        at android.app.ActivityThread.main(ActivityThread.java:7695)
04-18 17:08:40.943  6011  6011 E AndroidRuntime:        at java.lang.reflect.Method.invoke(Native Method)
04-18 17:08:40.943  6011  6011 E AndroidRuntime:        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
04-18 17:08:40.943  6011  6011 E AndroidRuntime:        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
04-18 17:08:40.947  2569  2813 D AudioFlinger: mixer(0xabae2080) throttle end: throttle time(45)
04-18 17:08:40.964  2774  3534 W ActivityTaskManager:   Force finishing activity com.cictech.xlauncher/.MainActivity
相关文章
|
4月前
|
XML API Android开发
码农之重学安卓:利用androidx.preference 快速创建一、二级设置菜单(demo)
本文介绍了如何使用androidx.preference库快速创建具有一级和二级菜单的Android设置界面的步骤和示例代码。
130 1
码农之重学安卓:利用androidx.preference 快速创建一、二级设置菜单(demo)
|
6月前
|
XML Java Android开发
34. 【Android教程】菜单:Menu
34. 【Android教程】菜单:Menu
124 2
|
6月前
|
开发工具 Android开发 开发者
Android Studio中两个让初学者崩溃菜单
Android Studio中两个让初学者崩溃菜单
56 0
|
7月前
|
XML Android开发 数据格式
android 12 添加菜单
android 12 添加菜单
45 0
|
6月前
|
Android开发 UED
|
6月前
|
API Android开发
32. 【Android教程】对话框:AlertDialog
32. 【Android教程】对话框:AlertDialog
94 2
|
5月前
|
API Android开发 UED
Android中使用AlertDialog创建对话框
Android中使用AlertDialog创建对话框
|
7月前
|
Java Android开发
Android 长按电源键弹出的GlobalActions菜单
Android 长按电源键弹出的GlobalActions菜单
173 1
|
7月前
|
Java Android开发
Android 长按桌面显示菜单的代码
Android 长按桌面显示菜单的代码
45 0
|
Android开发
Android 中选项菜单(Option menu)的用法
Android 中选项菜单(Option menu)的用法
199 0