2011-09-18 9 views
0

アクティビティでは、2つのテキストビューがあります。コンテキストメニューでは、テキストビューの1つのテキストサイズを変更するオプションがあります。私はこのような何かを試してみました。..コンテキストメニューで動的に変更されるtextSizeを持つAndroid AlertDialog

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

トン、私はmesssage「クローズを強制」を示すされるラジオボタンでclcikingています。これをどうすれば解決できますか? ありがとうございました..

答えて

1

items配列の負のインデックスを持つ要素にアクセスしようとすると、アプリケーションがクラッシュします。それは、これらの行で発生します。

if (items[whichButton] == "...") 

あなたはDialogInterface.OnClickListenerドキュメントを注意深く見ている場合は、そのonClick()方法は、すべてが否定され、リスト項目に接続されていないBUTTON_POSITIVEBUTTON_NEUTRALBUTTON_NEGATIVEなどの定数を受け入れることに気づくでしょう。