원인 : java.lang.NullPointerException : null 개체 참조에서 가상 메서드 'void android.widget.CheckBox.setChecked (boolean)'호출 시도
이것이 왜 발생하는지 알 수없는 오류입니다.
다음은 내 Java 클래스입니다 .
public class PopUpInfoActivity extends Activity {
static final String PREFS = "preference_file";
@Override
public void onCreate(Bundle state){
super.onCreate(state);
CheckBox chk = (CheckBox) findViewById(R.id.dontshow_checkbox);
chk.setChecked(PreferenceManager.getDefaultSharedPreferences(this).getBoolean("value", false));
chk.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//store isChecked to Preferences
SharedPreferences settings = getSharedPreferences(PREFS,0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("isChecked", false);
PreferenceManager.getDefaultSharedPreferences(PopUpInfoActivity.this).edit().putBoolean("value", isChecked).apply();
}
});
}
}
내가 수입 한 android.widget.CheckBox
그리고 이것은 내 xml 파일입니다.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="200dp"
android:scaleType="centerInside"
android:src="@drawable/legenda" />
<CheckBox
android:id="@+id/dontshow_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/dont_show"
android:textAllCaps="true"
android:fontFamily="sans-serif"
android:textStyle="bold"
android:layout_gravity="center"/>
</LinearLayout>
이 오류를 수정하도록 도와 주시면 정말 감사하겠습니다.
정확 id
하지만 매우 중요한 한 줄이 누락되었습니다 setContentView
. 이 줄은 xml을 Activity의 뷰와 바인딩하고 .NET에 사용되는 뷰입니다 findViewById
. 다음 줄을 추가하십시오 onCreate
.
@Override
public void onCreate(Bundle state){
super.onCreate(state);
setContentView(R.layout.name_of_the_xml);
그리고 나머지 코드. 올바른 xml 이름을 제공해야합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다