In an activity, I have two Text Views. In context menu, I have an options to change text size of one of the text view. I tried something like this..
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.menutextSize:
final CharSequence[] items = {"Normal","Large","Larger"};
AlertDialog.Builder builder = new
AlertDialog.Builder(this);
builder.setTitle("Select TextSize");
builder.setSingleChoiceItems(items, -1,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item],
Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
int textSize = (int)mBodyText.getTextSize();
if (items[whichButton] == "Normal")
{
mTextv.setTextSize(12);
}
if (items[whichButton] == "Large")
{
mTextv.setTextSize(14);
}
if (items[whichButton] == "Larger")
{
mTextv.setTextSize(16);
}
}
});
builder.setNegativeButton("cancel", null);
builder.show();
return true;
}
t when I am clciking in the radiobutton it is showing “Force close ” messsage. How can I solve this?
Thank you..
Your app crashes because it tries to access an element with a negative index in the
itemsarray. It happens because of these lines:If you look carefully at DialogInterface.OnClickListener documentation you’ll notice that its
onClick()method accepts such constants asBUTTON_POSITIVE,BUTTON_NEUTRALandBUTTON_NEGATIVEwhich all are negative and not connected to the list items.