2011-09-18 4 views
0

활동에서 두 개의 텍스트보기가 있습니다. 상황에 맞는 메뉴에서 텍스트보기 중 하나의 텍스트 크기를 변경할 수있는 옵션이 있습니다. 나는 이런 식으로 뭔가를 시도 ..컨텍스트 메뉴에서 동적으로 변경되는 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;  
      } 

t을 나는 님에게 메일 "가까운 강제"표시되어있는 라디오 버튼에 clciking하고 때. 이 문제를 어떻게 해결할 수 있습니까? 감사합니다 ..

답변

1

items 배열의 음수 인덱스가있는 요소에 액세스하려고하므로 응용 프로그램이 충돌합니다. 이 때문에이 라인의 발생 :

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

주의 깊게 살펴 DialogInterface.OnClickListener 문서에서 당신은 onClick() 방법은 BUTTON_POSITIVE, BUTTON_NEUTRAL 및 모든 부정과 목록 항목에 연결되지 않은 BUTTON_NEGATIVE 같은 상수를 받아들이는 것을 알 수 있습니다합니다.

관련 문제