Java 如果将SharedReferences放入其中,则代码不起作用

Java 如果将SharedReferences放入其中,则代码不起作用,java,android,boolean,sharedpreferences,Java,Android,Boolean,Sharedpreferences,为什么伙计们,我的代码有问题,如果我把SharedReferences放在我的代码上,我的代码就不起作用了 我将用下面的代码进行解释 这是菜单。class public class menu extends Activity { Button f1, f2; ImageView f2lock; @Override protected void onCreate(Bundle savedInstanceState) { super.onC

为什么伙计们,我的代码有问题,如果我把SharedReferences放在我的代码上,我的代码就不起作用了

我将用下面的代码进行解释

这是
菜单。class

public class menu extends Activity {

    Button f1, f2;
    ImageView f2lock;    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.famouslevel);
        f1 =(Button)findViewById(R.id.f1);      

        f1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){
                // TODO Auto-generated method stub
                Intent level1 = new Intent ();
                level1.setClassName ("com.example.game", "com.example.game.levelone");
                startActivityForResult (level1, 0);              
            }             
        });     
    }   

    public void onActivityResult (int requestCode, int resultCode, Intent level1){
        super.onActivityResult (requestCode, resultCode, level1); 
        f2=(Button)findViewById(R.id.f2);      
        f2lock=(ImageView)findViewById(R.id.f2lock);

        switch (resultCode) {
            case 2:  f2.setVisibility(View.VISIBLE);
                     f2lock.setVisibility(View.GONE);            
        }      

        f2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){
                // TODO Auto-generated method stub
                Intent level2 = new Intent ();
                level2.setClassName ("com.example.game", "com.example.game.leveltwo");
                startActivityForResult (level2, 0);              
            }             
        });       
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.splashscreen, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
如果我使用这个代码

        switch (resultCode) {
        case 2:  f2.setVisibility(View.VISIBLE);
                 f2lock.setVisibility(View.GONE);            
        }   
代码运行得很好,菜单中的f2按钮。xml显示为可见,f2lock
消失,但如果没有
SharedReferences
,它当然不会保存

因此,如果我更改代码并像这样放置
SharedReferences

switch (resultCode) {
    case 2:    
        SharedPreferences preferences = getSharedPreferences("preferences", MODE_PRIVATE);            
        SharedPreferences.Editor editor = preferences.edit(); 
        editor.putBoolean("f2", levelTwoUnlocked);
        editor.commit();

        if(levelTwoUnlocked){
            f2.setVisibility(View.VISIBLE);
            f2lock.setVisibility(View.GONE);            
        }
        else {
            f2.setVisibility(View.GONE);
            f2lock.setVisibility(View.VISIBLE);
        }      
}
menu.xml
中的f2按钮没有打开
可见
,它仍然
消失
。代码无法使f2按钮
可见
和f2lock
消失

有人能帮我处理这个代码吗

更新

我再次更改了代码

switch (resultCode) {
        case 2:    
            SharedPreferences preferences = getSharedPreferences("preferences", MODE_PRIVATE); 
            //to make f2 VISIBLE and f2lock GONE
            boolean levelTwoUnlocked = preferences.getBoolean("f2", true);      
            SharedPreferences.Editor editor = preferences.edit(); 
            editor.putBoolean("f2", levelTwoUnlocked);
            editor.commit();

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

仍然有同样的问题,f2不会设置可见性(View.VISIBLE)

事实上我不太明白您试图做什么,但错误是您从未切换布尔值
leveltwoolked
。因此,不必介意在
中输入多少次,如果
,则路线将始终相同(我打赌
leveltwoolocked=false
,因为这是默认的Java
布尔值):


我已经优先更新了上面的代码看看Jordi你试过我的答案了吗?按钮显示和隐藏?现在的问题是什么?
if(levelTwoUnlocked){
    f2.setVisibility(View.VISIBLE);
    f2lock.setVisibility(View.GONE);            
    levelTwoUnlocked = false;
} else {
    f2.setVisibility(View.GONE);
    f2lock.setVisibility(View.VISIBLE);
    levelTwoUnlocked = true;
}