2011-10-29 11 views
1

안녕하세요 .xml 형식의 체크 박스 값을 저장하고 싶습니다. 내 주요 XML에 대한 된 SharedPreferences를 사용하지만 내가 메시지 먹을수록 내가 컴파일 할 수 없습니다 :체크 박스 값 저장

Cannot invoke isChecked() on the primitive type int 

누군가가 제발 도와 줄 수 있습니까? 코드는 어떻게 보이게해야합니까?

편집 코드 :

@Override 
public void onPause() { 
    super.onPause(); 
    final CheckBox checkBox = (CheckBox) findViewById(R.id.nieuwbel); 
    save(checkBox.isChecked()); 
} 

private CheckBox checkBox = null; 

@Override 
public void onResume() { 
    super.onResume(); 
    checkBox = (CheckBox) findViewById(R.id.nieuwbel); 
    checkBox.setChecked(load()); <----- still exception line 464 
} 
private void save(final boolean isChecked) { 
    SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE); 
    SharedPreferences.Editor editor = sharedPreferences.edit(); 
    editor.putBoolean("check", isChecked); 
    editor.commit(); 
} 

private boolean load() { 
    SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE); 
    return sharedPreferences.getBoolean("check", false); 
} 

예외 코드 로그 캣 :

10-29 23:35:31.556: E/AndroidRuntime(3292): FATAL EXCEPTION: main 
10-29 23:35:31.556: E/AndroidRuntime(3292): java.lang.RuntimeException: Unable to resume activity {com.sencide/com.sencide.AndroidLogin}: java.lang.NullPointerException 
10-29 23:35:31.556: E/AndroidRuntime(3292):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2241) 
10-29 23:35:31.556: E/AndroidRuntime(3292):  at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2256) 
10-29 23:35:31.556: E/AndroidRuntime(3292):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1789) 
10-29 23:35:31.556: E/AndroidRuntime(3292):  at android.app.ActivityThread.access$1500(ActivityThread.java:123) 
10-29 23:35:31.556: E/AndroidRuntime(3292):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939) 
10-29 23:35:31.556: E/AndroidRuntime(3292):  at android.os.Handler.dispatchMessage(Handler.java:99) 
10-29 23:35:31.556: E/AndroidRuntime(3292):  at android.os.Looper.loop(Looper.java:130) 
10-29 23:35:31.556: E/AndroidRuntime(3292):  at android.app.ActivityThread.main(ActivityThread.java:3835) 
10-29 23:35:31.556: E/AndroidRuntime(3292):  at java.lang.reflect.Method.invokeNative(Native Method) 
10-29 23:35:31.556: E/AndroidRuntime(3292):  at java.lang.reflect.Method.invoke(Method.java:507) 
10-29 23:35:31.556: E/AndroidRuntime(3292):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847) 
10-29 23:35:31.556: E/AndroidRuntime(3292):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605) 
10-29 23:35:31.556: E/AndroidRuntime(3292):  at dalvik.system.NativeStart.main(Native Method) 
10-29 23:35:31.556: E/AndroidRuntime(3292): Caused by: java.lang.NullPointerException 
10-29 23:35:31.556: E/AndroidRuntime(3292):  at com.sencide.AndroidLogin.onResume(AndroidLogin.java:463) 
10-29 23:35:31.556: E/AndroidRuntime(3292):  at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1150) 
10-29 23:35:31.556: E/AndroidRuntime(3292):  at android.app.Activity.performResume(Activity.java:3832) 
10-29 23:35:31.556: E/AndroidRuntime(3292):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2231) 
10-29 23:35:31.556: E/AndroidRuntime(3292):  ... 12 more 

편집 2 : 거의 다른 사용자와 함께 문제를 해결하지만 난 응용 프로그램을 시작하고 아무것도하지 않고 닫습니다 (쇼 메뉴 등) 나는이 줄에 nullexception을 얻을 것이다. 일시 :

putBoolean(PREF_BOOL, nieuwbel.isChecked()); 

코드가 너무 지금까지 :

public static final String PREFS_NAME = "SharedPrefsDemoPreferences"; 
public static final String PREF_BOOL = "PrefBool"; 

private SharedPreferences mPrefs; 
private CheckBox nieuwbel; 

public boolean myBoxState = false; 
.. 
mPrefs = getSharedPreferences(PREFS_NAME, 0); 
    nieuwbel = (CheckBox)findViewById(R.id.nieuwbel); 

는 메뉴를 통해 레이아웃 XML을 호출 :

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle item selection 
    switch (item.getItemId()) { 
    case R.id.menu_beltegoed: // Laat Beltegoed.xml zien en checkboxen worden geinitialiseerd 
     MyBeltegoed dialog = new MyBeltegoed (this, new OnReadyListenerBeltegoed()); 
     dialog.setTitle("Optie beltegoed/toon:"); 
     dialog.show(); 
     nieuwbel = (CheckBox)dialog.findViewById(R.id.nieuwbel); 
     nieuwbel.setChecked(myBoxState); 
     nieuwbel.setOnCheckedChangeListener(new OnCheckedChangeListener(){ 

      @Override 
      public void onCheckedChanged(CompoundButton buttonView, 
        boolean isChecked) { 
       myBoxState = nieuwbel.isChecked(); 

      }}); 
     return true; 
    case R.id.menu_sluit: 
     //showHelp(); 
     return true; 
    default: 
     return super.onOptionsItemSelected(item); 
    } 
} 

onResume과 된 OnSave :

@Override 
protected void onResume() { 
    mPrefs = getSharedPreferences(PREFS_NAME, 0); 
    if(mPrefs!=null) 
     myBoxState=mPrefs.getBoolean(PREF_BOOL, false); 
    super.onResume(); 
} 


@Override  
protected void onPause() { 
    Editor e = mPrefs.edit(); 
    e.putBoolean(PREF_BOOL, nieuwbel.isChecked()); //<-- NullPointer when i start and close the app, without doing some handlings.    
    e.commit();   
    Toast.makeText(this, "Settings Saved.", Toast.LENGTH_SHORT).show();   
    super.onPause();  
} 

편집 3 : 당신은에 액세스 할 수 없습니다

@Override  
protected void onPause() { 
    MyBeltegoed dialog = new MyBeltegoed (this, new OnReadyListenerBeltegoed()); 
    nieuwbel = (CheckBox)dialog.findViewById(R.id.nieuwbel); 
    Editor e = mPrefs.edit(); 
    e.putBoolean(PREF_BOOL, nieuwbel.isChecked()); <-- nullpointer    
    e.commit();   
    Toast.makeText(this, "Settings Saved.", Toast.LENGTH_SHORT).show();   
    super.onPause();  
} 
+0

나는 두 선언이 체크 박스 거기에 방황, 그 중 하나는 최종! 왜 OnPause에서 다시 체크 박스를 선언해야합니까? –

+0

안녕하세요 왈 리드, 나는 레이아웃 xml 파일을 체크 박스와 함께 게시했는데 왜 작동하지 않는지 설명해 주시겠습니까? – Lars

+0

NullPointerExceptions은 알아낼 수있는 예외 중 가장 쉬운 방법입니다. 무언가가 null이라는 것을 의미합니다 (선언되었지만 인스턴스화되지 않았 음을 의미 할 수 있음). null 인 Nieubwel 일 가능성이 큽니다. 일부 초기 Android 튜토리얼 (http://developer.android.com/resources/tutorials/notepad/index.html)을 읽어보십시오. – Jack

답변

2

R.id.nieuwbel.is를 사용하여 확인란의 값 선택 함(). 방법은 다음과 같습니다.

final CheckBox checkBox = (CheckBox) findViewById(R.id.nieuwbel); 
     if (checkBox.isChecked()) { 
      checkBox.setChecked(false); 
     } 

this 링크를 참조하십시오.

@Override 
public void onPause() { 
    super.onPause(); 
    final CheckBox checkBox = (CheckBox) findViewById(R.id.nieuwbel); 
    save(checkBox.isChecked()); 
} 

와 onResume는 다음과 같이 보일 것이다 :

그래서 onPause는 다음과 같이 보일 것이다

@Override 
public void onResume() { 
    super.onResume(); 
    final CheckBox checkBox = (CheckBox) findViewById(R.id.nieuwbel); 
    checkBox.setChecked(load()); 
} 
+0

안녕하세요 잭, 답변을위한 Thx, 내가 수정 사항을 적용한다면, 나는 강제 종료 (forceclose)를 받게 될 것입니다. 예외 수정 코드를 참조하십시오. – Lars

+0

logcat에서 예외 로그를 ​​게시하십시오. – Jack

+0

Hi Jack, Logcat 게시 – Lars

1

R.id. * 귀하의 의견에 불과 ID 년대이다.

실제 뷰를 얻으려면, 호출 활동에

findViewById(R.id.nieubwel); 

을 (onPause와 onResume은 괜찮습니다).

findViewById를 일반적인 뷰를 돌려줍니다, 그래서 당신은 그것을 확인란을 캐스팅해야합니다

(CheckBox)findViewById(R.id.nieubwel);