Android:从选项菜单打开自定义弹出对话框

发布于 2024-12-02 08:07:16 字数 1248 浏览 1 评论 0原文

是否可以有一个打开对话框窗口的选项菜单项? 这就是我所得到的:

public class main extends Activity {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int score;
    
    SharedPreferences stats = getSharedPreferences("TRHprefs", MODE_WORLD_READABLE);

    score = stats.getInt("score", 0);
    
    switch (item.getItemId()) {
        
        case R.id.score:
                            
            Context mContext = getApplicationContext();
            Dialog dialog = new Dialog(mContext);

            dialog.setContentView(R.layout.options_menu);
            dialog.setTitle("Hero Stats");
            
            TextView b10 = (TextView) dialog.findViewById(R.id.tolevel);
            b10.setText("Score: " + score);

            dialog.setCancelable(true);
            dialog.show();

                            break;
        case R.id.options:     
            //Options

                            break;
        case R.id.quit: 
            //Quit
                            break;
    }
    return true;
}
}

当我选择分数选项按钮时,应用程序强制关闭。有什么想法吗?

Is it possible to have an option menu item that opens a dialog window?
Here's what I've got:

public class main extends Activity {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int score;
    
    SharedPreferences stats = getSharedPreferences("TRHprefs", MODE_WORLD_READABLE);

    score = stats.getInt("score", 0);
    
    switch (item.getItemId()) {
        
        case R.id.score:
                            
            Context mContext = getApplicationContext();
            Dialog dialog = new Dialog(mContext);

            dialog.setContentView(R.layout.options_menu);
            dialog.setTitle("Hero Stats");
            
            TextView b10 = (TextView) dialog.findViewById(R.id.tolevel);
            b10.setText("Score: " + score);

            dialog.setCancelable(true);
            dialog.show();

                            break;
        case R.id.options:     
            //Options

                            break;
        case R.id.quit: 
            //Quit
                            break;
    }
    return true;
}
}

When I select the score options button, the app force closes. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只想待在家 2024-12-09 08:07:16

您应该包含 logcat 输出,以便更容易地确定到底出了什么问题,但盯着代码我的期望是:

  1. 布局文件 options_menu.xml 和行中存在错误TextView b10 = (TextView)dialog.findViewById(R.id.tolevel);b10 返回空值。如果发生这种情况,下一行将导致 NullPointerException 并且应用程序将强制关闭。
  2. Dialog dial = new Dialog(mContext); 失败,因为您传递的是 ApplicationContext 而不是 Activity。尝试使用 Dialogdialog = new Dialog(this);。

You should include the logcat output to make it easier to determine what exactly is going wrong, but staring at the code my expectation is that either:

  1. There is an error in the layout file options_menu.xml and the line TextView b10 = (TextView) dialog.findViewById(R.id.tolevel); is returning a null value for b10. If that happens the next line will cause a NullPointerException and the app will force close.
  2. Dialog dialog = new Dialog(mContext); is failing because you're passing in an ApplicationContext rather than an Activity. Try using Dialog dialog = new Dialog(this);.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文