简体   繁体   English

SharedPreferences布尔值的一些问题

[英]Some problems with SharedPreferences Boolean

I had three class, menu.class, level1.class, level2.class. 我有三个类,menu.class,level1.class,level2.class。

I had the following main.xml data 我有以下main.xml数据

<Button
    android:id="@+id/f1"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginRight="10dp"
    android:background="@drawable/button1" />

<ImageView
    android:id="@+id/f2lock"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:src="@drawable/levellocked" />

<Button
    android:id="@+id/f2"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:background="@drawable/button2"
    android:visibility="gone" />

I has the following main.class data 我有以下main.class数据

f1 =(Button)findViewById(R.id.f1);      
f1.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v){
        Intent i =new Intent(getApplicationContext(), level1.class);
        startActivity(i);            
    }             
}); 
f2=(Button)findViewById(R.id.f2)
f2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v){
        Intent i =new Intent(getApplicationContext(), level2.class);
        startActivity(i);            
    }             
});   

The condition of f2 button is GONE, so in level1.class f2按钮的条件为GONE,因此在level1.class

if(answer.equalsIgnoreCase("8"))
    SharedPreferences preferences = getSharedPreferences("preferences", MODE_PRIVATE);
boolean levelTwoUnlocked = preferences.getBoolean("f2");

if(levelTwoUnlocked){
    f2.setVisibility(View.VISIBLE);
    f2lock.setVisibility(View.GONE);
}
    else {
    f2.setVisibility(View.GONE);
    f2lock.setVisibility(View.VISIBLE);
} 

It's mean f2 button setVisibility(View.VISIBLE) but I got this error in boolean levelTwoUnlocked = preferences.getBoolean("f2"); 这是f2按钮setVisibility(View.VISIBLE)的意思,但是我在boolean levelTwoUnlocked = preferences.getBoolean("f2");遇到此错误boolean levelTwoUnlocked = preferences.getBoolean("f2");

The method getBoolean(String, boolean) in the type SharedPreferences is not applicable for the arguments (String) 类型SharedPreferences中的方法getBoolean(String,boolean)不适用于参数(String)

UPDATED. 更新。

i had changed the code like this 我已经改变了这样的代码

if(answer.equalsIgnoreCase("8"))
    SharedPreferences preferences = getSharedPreferences("preferences", MODE_PRIVATE);
    boolean levelTwoUnlocked = preferences.getBoolean("f2", false);

if(levelTwoUnlocked){
    f2.setVisibility(View.VISIBLE);
    f2lock.setVisibility(View.GONE);
}
    else {
    f2.setVisibility(View.GONE);
    f2lock.setVisibility(View.VISIBLE);
} 

but the game was force close, did the placement of the code in the wrong class? 但是游戏被迫关闭,代码放置在错误的类中吗? because i put the code above in level1.class not in menu.class 因为我把代码放在了level1.class而不是menu.class

This is the android documentation. 这是android文档。

public abstract boolean getBoolean (String key, boolean defValue)

Retrieve a boolean value from the preferences. 从首选项中检索布尔值。

Parameters 参量

key: The name of the preference to retrieve. 键:要检索的首选项的名称。

defValue: Value to return if this preference does not exist. defValue:如果此首选项不存在,则返回的值。

Returns 退货

Returns the preference value if it exists, or defValue. 返回首选项值(如果存在)或defValue。 Throws ClassCastException if there is a preference with this name that is not a boolean. 如果此名称的首选项不是布尔值,则抛出ClassCastException。

Now your problem: 现在你的问题:

It clearly says you need to pass a default value too. 显然,您还需要传递一个默认值。

Change your call like this 像这样改变你的电话

boolean levelTwoUnlocked = preferences.getBoolean("f2", false); // or true according to your default value

PS: Please don't think I am being rude, but you should look for the documentation at least once when error clearly say method definition is not matching or in any other such case. PS:请不要以为我很粗鲁,但是当错误明确指出方法定义不匹配或在任何其他此类情况下,您应该至少查找一次文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM