This question already has an answer here:
- Creating a Dialog after an onClick event 4 answers
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!
Use Activity Context
Replace
By
No need for the below in
onItemClick
Use the below instead
As Raghunandan already commented above the main problem was for context, Update your click listener for listview like this
You must create builder first. Please replace your onItemClick with: