我在访问处理加载和保存的自定义类方法时遇到问题。 虽然它们属于同一类,但这些方法仍然有效。但是因为我决定重复使用save / load方法,所以我需要在单独的类中使用它们,但是当我尝试访问这些方法程序崩溃时,任何人都有一个解释为什么? 也许问题出在MODE_PRIVATE但我尝试更改它仍然会崩溃
package com.main.kanji_sama;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import com.main.customClass.sharedPreferences;
public class Hiragana extends Activity implements OnClickListener{
CheckBox cb1,cb2;
Button backButton;
sharedPreferences myFunction = new sharedPreferences();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hiragana);
cb1 = (CheckBox) findViewById(R.id.hiragana_xx_check);
cb1.setChecked(myFunction.loadSharedPref("cb1"));
cb2 = (CheckBox) findViewById(R.id.hiragana_kx_check);
cb2.setChecked(myFunction.loadSharedPref("cb2"));
backButton = (Button) findViewById(R.id.hiraganaconfbutton);
backButton.setOnClickListener(this);
}
@Override
public void onClick(View v){
startActivity(new Intent(Hiragana.this, Main.class));
}
public void onCheckboxClicked(View view) {
boolean checked = ((CheckBox) view).isChecked();
switch(view.getId()) {
case R.id.hiragana_xx_check:
myFunction.saveSharedPref("cb1",checked);
break;
case R.id.hiragana_kx_check:
myFunction.saveSharedPref("cb2", checked);
}
}
}
**This is my class**
public class sharedPreferences extends Activity {
public boolean loadSharedPref(String key){
SharedPreferences preferences = getApplicationContext().getSharedPreferences("Kanji-Sama", android.content.Context.MODE_PRIVATE);
return preferences.getBoolean(key, false);
}
public void saveSharedPref(String key,boolean value){
SharedPreferences preferences = getApplicationContext().getSharedPreferences("Kanji-Sama", android.content.Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(key, value);
editor.commit();
}
}
答案 0 :(得分:1)
您无需再使用其他课程,也无需延长Activity
。请更改您的代码。
public class Hiragana extends Activity implements OnClickListener {
CheckBox cb1, cb2;
Button backButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hiragana);
cb1 = (CheckBox) findViewById(R.id.hiragana_xx_check);
cb1.setChecked(myFunction.loadSharedPref("cb1"));
cb2 = (CheckBox) findViewById(R.id.hiragana_kx_check);
cb2.setChecked(myFunction.loadSharedPref("cb2"));
backButton = (Button) findViewById(R.id.hiraganaconfbutton);
backButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
startActivity(new Intent(Hiragana.this, Main.class));
}
public void onCheckboxClicked(View view) {
boolean checked = ((CheckBox) view).isChecked();
switch (view.getId()) {
case R.id.hiragana_xx_check:
saveSharedPref("cb1", checked);
break;
case R.id.hiragana_kx_check:
saveSharedPref("cb2", checked);
}
}
public boolean loadSharedPref(String key) {
SharedPreferences preferences = getApplicationContext()
.getSharedPreferences("Kanji-Sama",
android.content.Context.MODE_PRIVATE);
return preferences.getBoolean(key, false);
}
public void saveSharedPref(String key, boolean value) {
SharedPreferences preferences = getApplicationContext()
.getSharedPreferences("Kanji-Sama",
android.content.Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(key, value);
editor.commit();
}
}
你也需要为onCheckedChange
编写CheckBox
监听器。
答案 1 :(得分:0)
您的课程已扩展活动。
public class sharedPreferences extends Activity
尝试删除extends activity并添加类成员Context,Constructor传递Context(getApplicationContext()。
并且你在方法中改变你的类使用类成员。