我以前从未和SP合作过,所以我可能做错了什么,但我给了它最好的机会。我做了很多研究,这就是我提出的,但当我运行我的应用程序时,它只会立即崩溃。
我有更多的CheckBoxes,但这只是保持代码简短的一个例子:
final CheckBox cb1 = (CheckBox) findViewById(R.id.checkBox1);
cb1.setChecked(getFromSP("cb1"));
cb1.setOnCheckedChangeListener((OnCheckedChangeListener) this);
将CheckBox设置为“checked”时的代码:
cb1.setChecked(true);
saveInSp("cb1",isChecked);
SP代码的其余部分:
private boolean getFromSP(String key){
SharedPreferences preferences = getApplicationContext()
.getSharedPreferences("GW2Legendary", android.content.Context.MODE_PRIVATE);
return preferences.getBoolean(key, false);
}
private void saveInSp(String key,boolean value){
SharedPreferences preferences = getApplicationContext()
.getSharedPreferences("GW2Legendary", android.content.Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(key, value);
editor.commit();
}
正如我所说,我以前从未使用过共享偏好,所以我可能做错了。如果有人能解释这是如何工作以及我做错了什么,我会很感激。正如您所看到的,我正在尝试让我的应用程序记住我检查了哪些复选框
我还添加了整个java代码,如果它有帮助:
public class Bifrost extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bifrost);
final CheckBox cb1 = (CheckBox) findViewById(R.id.checkBox1);
cb1.setChecked(getFromSP("cb1"));
cb1.setOnCheckedChangeListener((OnCheckedChangeListener) this);
final CheckBox cb2 = (CheckBox) findViewById(R.id.checkBox2);
cb2.setChecked(getFromSP("cb2"));
cb2.setOnCheckedChangeListener((OnCheckedChangeListener) this);
final CheckBox cb3 = (CheckBox) findViewById(R.id.checkBox3);
cb3.setChecked(getFromSP("cb3"));
cb3.setOnCheckedChangeListener((OnCheckedChangeListener) this);
final CheckBox cb4 = (CheckBox) findViewById(R.id.checkBox4);
cb4.setChecked(getFromSP("cb4"));
cb4.setOnCheckedChangeListener((OnCheckedChangeListener) this);
final CheckBox cb5 = (CheckBox) findViewById(R.id.checkBox5);
cb5.setChecked(getFromSP("cb5"));
cb5.setOnCheckedChangeListener((OnCheckedChangeListener) this);
final CheckBox cb6 = (CheckBox) findViewById(R.id.checkBox6);
cb6.setChecked(getFromSP("cb6"));
cb6.setOnCheckedChangeListener((OnCheckedChangeListener) this);
final CheckBox cb7 = (CheckBox) findViewById(R.id.checkBox7);
cb7.setChecked(getFromSP("cb7"));
cb7.setOnCheckedChangeListener((OnCheckedChangeListener) this);
final CheckBox cb8 = (CheckBox) findViewById(R.id.checkBox8);
cb8.setChecked(getFromSP("cb8"));
cb8.setOnCheckedChangeListener((OnCheckedChangeListener) this);
final CheckBox cb9 = (CheckBox) findViewById(R.id.checkBox9);
cb9.setChecked(getFromSP("cb9"));
cb9.setOnCheckedChangeListener((OnCheckedChangeListener) this);
final CheckBox cb10 = (CheckBox) findViewById(R.id.checkBox10);
cb10.setChecked(getFromSP("cb10"));
cb10.setOnCheckedChangeListener((OnCheckedChangeListener) this);
final CheckBox cb11 = (CheckBox) findViewById(R.id.checkBox11);
cb11.setChecked(getFromSP("cb11"));
cb11.setOnCheckedChangeListener((OnCheckedChangeListener) this);
final CheckBox cb12 = (CheckBox) findViewById(R.id.checkBox12);
cb12.setChecked(getFromSP("cb12"));
cb12.setOnCheckedChangeListener((OnCheckedChangeListener) this);
final CheckBox cb13 = (CheckBox) findViewById(R.id.checkBox13);
cb13.setChecked(getFromSP("cb13"));
cb13.setOnCheckedChangeListener((OnCheckedChangeListener) this);
final CheckBox cb14 = (CheckBox) findViewById(R.id.checkBox14);
cb14.setChecked(getFromSP("cb14"));
cb14.setOnCheckedChangeListener((OnCheckedChangeListener) this);
final CheckBox cb15 = (CheckBox) findViewById(R.id.checkBox15);
cb15.setChecked(getFromSP("cb15"));
cb15.setOnCheckedChangeListener((OnCheckedChangeListener) this);
final CheckBox cb16 = (CheckBox) findViewById(R.id.checkBox16);
cb16.setChecked(getFromSP("cb16"));
cb16.setOnCheckedChangeListener((OnCheckedChangeListener) this);
final CheckBox cb17 = (CheckBox) findViewById(R.id.checkBox17);
cb17.setChecked(getFromSP("cb17"));
cb17.setOnCheckedChangeListener((OnCheckedChangeListener) this);
final CheckBox cb18 = (CheckBox) findViewById(R.id.checkBox18);
cb18.setChecked(getFromSP("cb18"));
cb18.setOnCheckedChangeListener((OnCheckedChangeListener) this);
final CheckBox cb19 = (CheckBox) findViewById(R.id.checkBox19);
cb19.setChecked(getFromSP("cb19"));
cb19.setOnCheckedChangeListener((OnCheckedChangeListener) this);
final CheckBox cb20 = (CheckBox) findViewById(R.id.checkBox20);
cb20.setChecked(getFromSP("cb20"));
cb20.setOnCheckedChangeListener((OnCheckedChangeListener) this);
final CheckBox cb21 = (CheckBox) findViewById(R.id.checkBox21);
cb21.setChecked(getFromSP("cb21"));
cb21.setOnCheckedChangeListener((OnCheckedChangeListener) this);
final CheckBox cb22 = (CheckBox) findViewById(R.id.checkBox22);
cb22.setChecked(getFromSP("cb22"));
cb22.setOnCheckedChangeListener((OnCheckedChangeListener) this);
final CheckBox cb23 = (CheckBox) findViewById(R.id.checkBox23);
cb23.setChecked(getFromSP("cb23"));
cb23.setOnCheckedChangeListener((OnCheckedChangeListener) this);
final CheckBox cb24 = (CheckBox) findViewById(R.id.checkBox24);
cb24.setChecked(getFromSP("cb24"));
cb24.setOnCheckedChangeListener((OnCheckedChangeListener) this);
final CheckBox cb25 = (CheckBox) findViewById(R.id.checkBox25);
cb25.setChecked(getFromSP("cb25"));
cb25.setOnCheckedChangeListener((OnCheckedChangeListener) this);
final CheckBox cb26 = (CheckBox) findViewById(R.id.checkBox26);
cb26.setChecked(getFromSP("cb26"));
cb26.setOnCheckedChangeListener((OnCheckedChangeListener) this);
final CheckBox cb27 = (CheckBox) findViewById(R.id.checkBox27);
cb27.setChecked(getFromSP("cb27"));
cb27.setOnCheckedChangeListener((OnCheckedChangeListener) this);
final CheckBox cb28 = (CheckBox) findViewById(R.id.checkBox28);
cb28.setChecked(getFromSP("cb28"));
cb28.setOnCheckedChangeListener((OnCheckedChangeListener) this);
final CheckBox cb29 = (CheckBox) findViewById(R.id.checkBox29);
cb29.setChecked(getFromSP("cb29"));
cb29.setOnCheckedChangeListener((OnCheckedChangeListener) this);
final CheckBox cb30 = (CheckBox) findViewById(R.id.checkBox30);
cb30.setChecked(getFromSP("cb30"));
cb30.setOnCheckedChangeListener((OnCheckedChangeListener) this);
final CheckBox cb31 = (CheckBox) findViewById(R.id.checkBox31);
cb31.setChecked(getFromSP("cb31"));
cb31.setOnCheckedChangeListener((OnCheckedChangeListener) this);
final CheckBox cb32 = (CheckBox) findViewById(R.id.checkBox32);
cb32.setChecked(getFromSP("cb32"));
cb32.setOnCheckedChangeListener((OnCheckedChangeListener) this);
final CheckBox cb33 = (CheckBox) findViewById(R.id.checkBox33);
cb33.setChecked(getFromSP("cb33"));
cb33.setOnCheckedChangeListener((OnCheckedChangeListener) this);
final CheckBox cb34 = (CheckBox) findViewById(R.id.checkBox34);
cb34.setChecked(getFromSP("cb34"));
cb34.setOnCheckedChangeListener((OnCheckedChangeListener) this);
final CheckBox cb35 = (CheckBox) findViewById(R.id.checkBox35);
cb35.setChecked(getFromSP("cb35"));
cb35.setOnCheckedChangeListener((OnCheckedChangeListener) this);
final CheckBox cb36 = (CheckBox) findViewById(R.id.checkBox36);
cb36.setChecked(getFromSP("cb36"));
cb36.setOnCheckedChangeListener((OnCheckedChangeListener) this);
final CheckBox cb37 = (CheckBox) findViewById(R.id.checkBox37);
cb37.setChecked(getFromSP("cb37"));
cb37.setOnCheckedChangeListener((OnCheckedChangeListener) this);
final CheckBox cb38 = (CheckBox) findViewById(R.id.checkBox38);
cb38.setChecked(getFromSP("cb38"));
cb38.setOnCheckedChangeListener((OnCheckedChangeListener) this);
final CheckBox cb39 = (CheckBox) findViewById(R.id.checkBox39);
cb39.setChecked(getFromSP("cb39"));
cb39.setOnCheckedChangeListener((OnCheckedChangeListener) this);
cb3.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( cb3.isChecked() )
{
cb4.setChecked(true);
cb5.setChecked(true);
cb6.setChecked(true);
cb7.setChecked(true);
}
}
});
cb8.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( cb8.isChecked() )
{
cb9.setChecked(true);
saveInSp("cb1",isChecked);
cb10.setChecked(true);
cb11.setChecked(true);
cb12.setChecked(true);
}
}
});
cb14.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( cb14.isChecked() )
{
cb15.setChecked(true);
cb16.setChecked(true);
cb17.setChecked(true);
cb18.setChecked(true);
}
}
});
cb21.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( cb21.isChecked() )
{
cb25.setChecked(true);
}
}
});
cb24.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( cb24.isChecked() )
{
cb28.setChecked(true);
}
}
});
cb3.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( cb3.isChecked() )
{
cb4.setChecked(true);
cb5.setChecked(true);
cb6.setChecked(true);
cb7.setChecked(true);
}
}
});
cb23.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( cb23.isChecked() )
{
cb24.setChecked(true);
cb29.setChecked(true);
cb30.setChecked(true);
cb31.setChecked(true);
}
}
});
cb32.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( cb32.isChecked() )
{
cb33.setChecked(true);
cb34.setChecked(true);
cb35.setChecked(true);
cb36.setChecked(true);
}
}
});
cb2.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( cb2.isChecked() )
{
cb3.setChecked(true);
cb8.setChecked(true);
cb13.setChecked(true);
cb14.setChecked(true);
}
}
});
cb19.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( cb19.isChecked() )
{
cb20.setChecked(true);
cb21.setChecked(true);
cb26.setChecked(true);
cb27.setChecked(true);
}
}
});
cb22.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( cb22.isChecked() )
{
cb23.setChecked(true);
cb32.setChecked(true);
cb37.setChecked(true);
cb38.setChecked(true);
}
}
});
cb1.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( cb1.isChecked() )
{
cb2.setChecked(true);
cb19.setChecked(true);
cb22.setChecked(true);
cb39.setChecked(true);
}
}
});
}
private boolean getFromSP(String key){
SharedPreferences preferences = getApplicationContext().getSharedPreferences("GW2Legendary", android.content.Context.MODE_PRIVATE);
return preferences.getBoolean(key, false);
}
private void saveInSp(String key,boolean value){
SharedPreferences preferences = getApplicationContext().getSharedPreferences("GW2Legendary", android.content.Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(key, value);
editor.commit();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.bifrost, menu);
return true;
}
}
再次,抱歉,如果我太复杂,但我只是一个初学者试图学习:)
答案 0 :(得分:0)
问题出在您的代码的这一行:
cb1.setOnCheckedChangeListener((OnCheckedChangeListener) this);
您的类不会从OnCheckedChangeListener继承,因此将Bitfrost强制转换为OnCheckedChangeListener会触发ClassCastException。
尝试为“cb1”分配OnCheckedChangeListener的新实例(跟随,匿名类和继承的方法)。
示例:
cb1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO your code
}
});