如何在选中框时进行全屏活动

时间:2014-08-21 05:41:01

标签: java android fullscreen

我试图在我的应用中进行设置,因此当用户选中一个框而不是应用程序全屏时,但每次我运行该活动时都会强制关闭:

CheckBox FullScreen;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.settings);

    if (FullScreen.isChecked()) {
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }
}

我如何实现理想的行为?
这是我的logcat错误:

08-20 23:09:23.328: E/AndroidRuntime(30491): FATAL EXCEPTION: main
08-20 23:09:23.328: E/AndroidRuntime(30491): Process: com.jason.payday, PID: 30491
08-20 23:09:23.328: E/AndroidRuntime(30491): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jason.payday/com.jason.payday.settings}: java.lang.NullPointerException
08-20 23:09:23.328: E/AndroidRuntime(30491):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2334)
08-20 23:09:23.328: E/AndroidRuntime(30491):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392)
08-20 23:09:23.328: E/AndroidRuntime(30491):    at android.app.ActivityThread.access$900(ActivityThread.java:169)
08-20 23:09:23.328: E/AndroidRuntime(30491):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
08-20 23:09:23.328: E/AndroidRuntime(30491):    at android.os.Handler.dispatchMessage(Handler.java:102)
08-20 23:09:23.328: E/AndroidRuntime(30491):    at android.os.Looper.loop(Looper.java:146)
08-20 23:09:23.328: E/AndroidRuntime(30491):    at android.app.ActivityThread.main(ActivityThread.java:5487)
08-20 23:09:23.328: E/AndroidRuntime(30491):    at java.lang.reflect.Method.invokeNative(Native Method)
08-20 23:09:23.328: E/AndroidRuntime(30491):    at java.lang.reflect.Method.invoke(Method.java:515)
08-20 23:09:23.328: E/AndroidRuntime(30491):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
08-20 23:09:23.328: E/AndroidRuntime(30491):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
08-20 23:09:23.328: E/AndroidRuntime(30491):    at dalvik.system.NativeStart.main(Native Method)
08-20 23:09:23.328: E/AndroidRuntime(30491): Caused by: java.lang.NullPointerException
08-20 23:09:23.328: E/AndroidRuntime(30491):    at com.jason.payday.settings.onCreate(settings.java:42)
08-20 23:09:23.328: E/AndroidRuntime(30491):    at android.app.Activity.performCreate(Activity.java:5451)
08-20 23:09:23.328: E/AndroidRuntime(30491):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
08-20 23:09:23.328: E/AndroidRuntime(30491):    at           android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
08-20 23:09:23.328: E/AndroidRuntime(30491):    
... 11 more

5 个答案:

答案 0 :(得分:1)

你有两个问题:

  1. 如何制作全屏活动?
  2. 为什么我的应用程序会因为nullpointer而崩溃?
  3. 以下是

    CheckBox FullScreen;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setcontentview(R.layout.settings);
        // initialize the checkbox, else nullpointer in if-condition (q2)
        FullScreen = (CheckBox)findViewById(R.id.myFullscreenCheckbox);
        if (FullScreen.isChecked()) {
            // make app fullscreen (q1)
            this.getWindow().setFlags(
                        WindowManager.LayoutParams.FLAG_FULLSCREEN,
                        WindowManager.LayoutParams.FLAG_FULLSCREEN
            );
            setcontentview(R.layout.settings);
        }
    }
    

    但是再一次,这个代码有点无用,因为复选框总是初始化为true或false,具体取决于你的xml。您应该使用SharedPrefernces来保持设置并在应用启动后加载它们。快速教程。

    获取全屏设置:

    boolean fullscreen = 
        getSharedPreferences("settings", MODE_PRIVATE).getBoolean("fullscreen", false);
    

    保存全屏设置:

    boolean fullscreen = mycheckbox.isChecked();
    getSharedPreferences("settings", MODE_PRIVATE).edit().putBoolean("fullscreen", fullscreen).commit();
    

答案 1 :(得分:0)

致电setContentView(R.layout.settings);

之后

this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

答案 2 :(得分:0)

您只需在

之后致电setcontentview(Your layout);
   this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

现在不会出现崩溃。如果您仍然发现任何问题,请与我们联系。 另外,我建议您选择保存首选项中的复选框选项。

http://www.sanfoundry.com/java-android-program-demonstrate-preference-screen/

答案 3 :(得分:0)

当您的复选框状态变为 true 时,请输入此代码

buttonFullscreen
            .setOnCheckedChangeListener(new OnCheckedChangeListener() {
                    @Override
                public void onCheckedChanged(CompoundButton arg0,
                        boolean b) {
                    // TODO Auto-generated method stub
                    if(b)
                    getWindow().setFlags(
                            WindowManager.LayoutParams.FLAG_FULLSCREEN,
                            WindowManager.LayoutParams.FLAG_FULLSCREEN);

                }

            });

答案 4 :(得分:0)

请分享强制关闭日志。看看你的代码,我认为force close与Full Screen标志无关。我怀疑这是由于其他原因。如果您使用的是Ubuntu,则可以通过以下命令获取强制关闭日志,

adb logcat | grep "AndroidRuntime"

关于解决方案,请勿在OnCreate中设置FullScreen标志。 如果要在按钮上单击,请设置onclicklistener。如果要在CheckBox状态更改列表器上执行此操作,请设置checkstate change listener。在事件中添加以下代码

if (fullScreen == true) {
                    getWindow().addFlags(
                            WindowManager.LayoutParams.FLAG_FULLSCREEN);
                } else {
                    getWindow().clearFlags(
                            WindowManager.LayoutParams.FLAG_FULLSCREEN);
                }