Android共享首选项全屏不工作

时间:2012-05-25 23:41:25

标签: android passwords locking sharedpreferences

我已经在xml中创建了我的登录表单,我的共享首选项确实有效,但是当我向java类添加全屏时,应用程序崩溃了。这是我的代码,任何帮助都会感激不尽。

getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);

        /*
         * Check if we successfully logged in before. 
         * If we did, redirect to home page
         */
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        if (settings.getString("logged", "").toString().equals("logged")) {
            Intent intent = new Intent(Password.this, Video.class);
            startActivity(intent);
        }

        Button b = (Button) findViewById(R.id.loginbutton);
        b.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                EditText username = (EditText) findViewById(R.id.username);
                EditText password = (EditText) findViewById(R.id.password);

                if(username.getText().toString().length() > 0 && password.getText().toString().length() > 0 ) {
                    //------------------------------------Username below -------------------------------------Password below ---//
                    if(username.getText().toString().equals("username") && password.getText().toString().equals("password")) {

                        /*
                         * So login information is correct, 
                         * we will save the Preference data
                         * and redirect to next class / home  
                         */
                        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
                        SharedPreferences.Editor editor = settings.edit();
                        editor.putString("logged", "logged");
                        editor.commit();

                        Intent intent = new Intent(Password.this, Video.class);
                        startActivity(intent);
                    }
                }
            }
        });
    }
}

2 个答案:

答案 0 :(得分:0)

您的问题与SharedPreferences无关。您的代码存在的问题是,在设置了该窗口的内容后,您正在调用requestFeature 。如果您查看requestFeature的文档,则表示必须在调用setContentView()之前调用它(直接或间接)。

http://developer.android.com/reference/android/view/Window.html#requestFeature%28int%29

  

public boolean requestFeature(int featureId)

     

启用扩展屏幕功能。必须在setContentView()之前调用它。可以根据需要多次调用,只要它在setContentView()之前。如果未调用,则不会提供扩展功能。请求后,您无法关闭该功能。您可以使用FEATURE_CUSTOM_TITLE来使用其他标题功能。

答案 1 :(得分:0)

requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

在oncreate()方法中的setcontentView()之前使用此代码。