0

我以前从未与 SP 合作过,所以我可能做错了什么,但我尽了最大努力。我做了很多研究,这就是我想出的,但是当我运行我的应用程序时,它会立即崩溃。

我有更多的复选框,但这只是保持代码简短的一个示例:

final CheckBox cb1 = (CheckBox) findViewById(R.id.checkBox1);
cb1.setChecked(getFromSP("cb1"));
cb1.setOnCheckedChangeListener((OnCheckedChangeListener) this);

当我将 CheckBox 设置为“已选中”时的代码:

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

}

再次,对不起,如果我太复杂了,但我只是一个试图学习的初学者:)

4

1 回答 1

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       
    }
});
于 2013-06-11T21:39:12.693 回答