Maurice
Maurice

Reputation: 280

AlertDialog issue

Why my program is crashing when I click on the "Test" button (created in the menu area).. I run my program on my Nexus 5 (Android 4.4.4)

Here is the code:

    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);
}

Upvotes: 0

Views: 106

Answers (1)

Dan Harms
Dan Harms

Reputation: 4840

AlertDialog.Builder needs a context. You are currently passing in null. Change your code to this:

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

Upvotes: 2

Related Questions