2014-01-31 3 views
1

레이아웃 파일이 아닌 활동 메뉴 파일에있는 체크 상자를 확인하는 방법과 혼동합니다. 먼저 findViewById(R.menu.dressing_room).findViewById(R.id.uniformScale); 대신 findViewById(R.id.uniformScale); 코드를 가지고 있지만 nullPointerException을 던져서 findViewById(R.menu.dressing_room)을 추가했지만 같은 오류가 발생합니다. 레이아웃 파일에 없기 때문에 메뉴 파일의 확인란을 참조해야한다는 것을 알고 있지만 어떻게해야할지 모르겠습니다. 또는 레이아웃 파일에 추가 할 수 있지만 그 방법을 잘 모르겠습니다. 참고 : checkableBehavior = "single"이기 때문에 radioButton 일 수 있지만 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> 

활동 내 : 노력 blackbelts 대답

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

편집 : 활동에 대한 메뉴 파일 내에서

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) 

하고 요소를

+0

감사를 검색 할 menu.findItem(R.id.uniformScale)를 사용합니다. 메뉴는 어떻게 얻을 수 있습니까? 나는 이것을 시도했다. @Override \t public boolean onOptionsItemSelected (MenuItem item) { \t \t int itemId = item.getItemId(); \t \t 메뉴 메뉴 = (메뉴) findViewById (R.menu.dressing_room); \t \t checkCheckboxes (메뉴); \t \t 스위치 (해당 itemId) {... '와'개인 무효 checkCheckboxes (메뉴 메뉴) { \t \t \t \t 의 RadioButton CheckBox1을 = (의 RadioButton) 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

onCreateOptionsMenu에서 checkCheckboxes를 호출하려고합니다. onCreateOptionsMenu에는 매개 변수로 Menu가 있습니다 – Blackbelt

+0

고마워요. 거기로 옮겼습니다. 이 예외가 있습니다 : 01-31 21 : 44 : 15.434 : E/AndroidRuntime (14702) : java.lang.IndexOutOfBoundsException. 그것의 다른 예외를 atleast : P – user3164083

관련 문제