问题
I'm trying to get a dialog out of a listView. I want to let the user see a dialog with 3 font sizes so he can change the font size of the app. To get to this dialog the user has to tap on a button in a listView. I made it like this:
package com.reekapps.simplenote;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.app.Dialog;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Settings extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
getActionBar().setTitle("Settings");
String[] listItems = {"Colour", "Font Size",};
ListView lv = (ListView) findViewById(R.id.settings_list);
lv.setAdapter(new ArrayAdapter<String>
(this, android.R.layout.simple_list_item_1, listItems));
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View
view, int position, long id)
{
String[] listItems = {"Colour", "Font Size",};
if(listItems[position].equals("Font Size"))
{
AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
builder.setTitle("Choose Font Size");
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}
});
// TODO Auto-generated method stub
}
}
It doesn't show any errors but it crashes when I click on Font Size (the button to get to the dialog).
Thank you for your time!
回答1:
As Raghunandan already commented above the main problem was for context, Update your click listener for listview like this
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String[] listItems = { "Colour", "Font Size", };
if (listItems[position].equals("Font Size")) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
Settings.this );
// set title
alertDialogBuilder.setTitle("Choose Font Size");
// set dialog message
alertDialogBuilder
.setMessage("Click yes to exit!")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
}
});
回答2:
Use Activity Context
Replace
AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
By
AlertDialog.Builder builder = new AlertDialog.Builder(Settings.this);
No need for the below in onItemClick
String[] listItems = {"Colour", "Font Size",};
Use the below instead
String name = (String) parent.getItemAtPosition(position); //get clicked item
if(name.equals("Font Size"))
{
回答3:
You must create builder first. Please replace your onItemClick with:
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String[] listItems = { "Colour", "Font Size", };
if (listItems[position].equals("Font Size")) {
AlertDialog builder = new AlertDialog.Builder(
Settings.this).create();
builder.setTitle("Choose Font Size");
builder.show();
}
}
});
来源:https://stackoverflow.com/questions/19456660/creating-a-dialog-after-onclick-in-listview