AlertDialog问题

时间:2014-10-10 16:44:47

标签: android alertdialog builder

当我点击“测试”按钮(在菜单区域中创建)时,为什么我的程序崩溃..我在Nexus 5上运行我的程序(Android 4.4.4)

以下是代码:

    public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }

    if(id == R.id.menu_test){
        AlertDialog.Builder builder = new AlertDialog.Builder(null);
        builder.setMessage(R.string.dialog_m).setTitle(R.string.dialog_t);
        AlertDialog dialog = builder.create();
        dialog.show();
        return true;
    }

    return super.onOptionsItemSelected(item);
}

1 个答案:

答案 0 :(得分:2)

AlertDialog.Builder需要一个上下文。您目前正在传递null。将您的代码更改为:

AlertDialog.Builder builder = new AlertDialog.Builder(this);