如果摆在那里共享preferences的code的无法正常工作 [英] The code was not function if SharedPreferences put in there

查看:109
本文介绍了如果摆在那里共享preferences的code的无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

海兰家伙,我有一个问题,我的code,如果我把共享preferences我的code无法正常工作

Hy guys i have a problem with my code, if i put SharedPreferences my code not function

我会用我的下code解释

i will explain with my following code

这是 menu.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);
    }

如果我用这个code

if i use this code

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

在code的运行完美,F2键 menu.xml文件是显示为可见和f2lock GONE 但当然没有共享preferences 不会保存。

The code was running perfect, the f2 button in menu.xml is show up to VISIBLE and f2lock GONE but of course without SharedPreferences it won't save.

所以,如果我改变code,并把共享preferences 是这样的:

So if I change the code and put SharedPreferences like this:

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

在F2键 menu.xml文件不转可见,它仍然是 GONE 。 YHE code不起作用,使F2键可见和f2lock GONE

The f2 button in menu.xml does not turn VISIBLE, it is still GONE. Yhe code does not function to make f2 button VISIBLE and f2lock GONE.

谁能帮我这个code?

Can anyone help me with this code?

更新时间:

我又改变了我的code

i have changed my code again

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不会setVisibility(View.VISIBLE)

still have the same problem, f2 won't setVisibility(View.VISIBLE)

推荐答案

其实我不明白,那么你正在试图做什么,但错误是你永远不切换您的布尔 levelTwoUnlocked 。所以不介意你在如果,该航线将始终是相同的(我敢打赌,多少次输入 levelTwoUnlocked = FALSE ,因为是默认的Java 布尔值):

Actually I don't understand well what you're trying to do, but mistake is you never toggle your boolean levelTwoUnlocked. So don't mind how many times you enter in the if, the route will be always the same (I bet levelTwoUnlocked=false because is default Java boolean value):

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

这篇关于如果摆在那里共享preferences的code的无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆