2014-01-31 56 views
1

我很困惑如何檢查活動菜單文件中的複選框,而不是其佈局文件。首先我得到了下面的代碼,但是findViewById(R.id.uniformScale);而不是findViewById(R.menu.dressing_room).findViewById(R.id.uniformScale);,但是拋出了一個nullPointerException,所以我添加了findViewById(R.menu.dressing_room)但這會導致相同的錯誤。我知道我必須引用菜單文件中的複選框,因爲它不在佈局文件中,但我不知道如何。或者我將它添加到佈局文件,但我不知道如何做到這一點。注意:它們可能是radioButtons,因爲checkableBehavior =「single」,但我已經嘗試將類型更改爲RadioButton並獲得拋出的相同異常。謝謝。檢查複選框nullPointerException

在該活動的菜單文件:

<item 
         android:title="Sticker Settings" 
         android:id="@+id/action_cancel1" 
         android:icon="@drawable/ic_action_settings"> 
         <menu> 
        <group android:checkableBehavior="single" 
         > 
         <item android:id="@+id/uniformScale" 
          android:title="Change Size" 
          android:checked="true" /> 
         <item android:id="@+id/rotation" 
          android:title="Rotate" 
          android:checked="false" /> 
        </group> 
        </menu> 
        </item> 

中的活動:

@Override 
     public boolean onCreateOptionsMenu(Menu menu) { 
      // Inflate the menu; this adds items to the action bar if it is present. 
      getMenuInflater().inflate(R.menu.mainActivity, menu); 
      checkCheckboxes(); 
      return true; 
     } 

    private void checkCheckboxes(){ 

      CheckBox checkBox1 = (CheckBox) findViewById(R.menu.dressing_room).findViewById(R.id.uniformScale); 
      if (checkBox1.isChecked()) { 
       photoSorter.setRotate(1); 
       photoSorter.setAnisotropic(2); 
      } 
      else{ 
       checkBox1.setChecked(true); 
       photoSorter.setRotate(2); 
       photoSorter.setAnisotropic(1); 
      } 
     } 

編輯試圖blackbelts回答:

private void checkCheckboxes(Menu menu){ 

      MenuItem checkBox1 = (MenuItem) menu.getItem(R.id.uniformScale); 
      if (checkBox1.isChecked()) { 
       photoSorter.setRotate(1); 
       photoSorter.setAnisotropic(2); 
      } 
      else{ 
       //checkBox1.setChecked(true); 
       photoSorter.setRotate(2); 
       photoSorter.setAnisotropic(1); 
      } 
     } 

@Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.mainActivity, menu); 
     checkCheckboxes(menu); 
     return true; 
    } 

回答

2

變化

private void checkCheckboxes() 

private void checkCheckboxes(Menu menu) 

,並使用menu.findItem(R.id.uniformScale)檢索元素

+0

感謝。我將如何獲得菜單?我試過這個'@Override \t public boolean onOptionsItemSelected(MenuItem item){ \t \t int itemId = item.getItemId(); \t \t菜單菜單=(菜單)findViewById(R.menu.dressing_room); \t \t checkCheckboxes(menu); \t \t開關(的itemId){...'和'私人無效checkCheckboxes(菜單菜單){ \t \t \t 單選按鈕\t = checkBox1(單選按鈕)menu.getItem(R.id.uniformScale); \t \t if(checkBox1.isChecked()){ \t \t \t photoSorter.setRotate(1); \t \t \t photoSorter.setAnisotropic(2); \t \t} \t \t否則{ \t \t \t //checkBox1.setChecked(true); \t \t \t photoSorter.setRotate(2); \t \t \t photoSorter.setAnisotropic(1); \t \t} \t}'同樣的錯誤 – user3164083

+0

你打電話checkCheckboxes裏面onCreateOptionsMenu。 onCreateOptionsMenu有一個菜單作爲參數 – Blackbelt

+0

謝謝。移動到那裏。得到以下例外:01-31 21:44:15.434:E/AndroidRuntime(14702):java.lang.IndexOutOfBoundsException。至少有一個例外:P – user3164083