必须在添加内容异常之前调用requestFeature()

时间:2015-02-16 12:50:50

标签: java android layout

我见过很多人都在问这个问题,而且答案通常都是这样的:“在此之前做到这一点

  

的setContentView(R.layout.test_layout);

  

在super.onCreate(savedInstanceState);

之前执行

我已经尝试过这两种方法,但是先把它放进去     的setContentView(R.layout.test_layout); 给出例外情况,如前所述:     super.onCreate(savedInstanceState); 没有例外,但它也不起作用......

我通过推送全屏工作:

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

super.onCreate(savedInstanceState);

我已经看到很多强制noTitleScreen和全屏的方法,但是它们似乎都没有用。 (大多数其他方式没有错误,他们似乎没有做任何事情)像改变布局主题......由于某种原因没有做任何事情。

我正在使用Android Studio。

任何帮助都将不胜感激。

这是整个活动。我觉得有必要通过说这是我第一次用java开发来保护自己,所以如果我在这里有任何明显的错误,请原谅我:3 - >

package the1n07.bt_rc;

import android.content.Intent;
import android.gesture.Gesture;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.NumberPicker;


public class ControlActivity extends ActionBarActivity {

    Button speedPlusButton, speedMinusButton, forwardButton, backwardButton, leftButton, rightButton;
    EditText speedText;

    public String speed;
    public Integer speedInt;
    public boolean forward = false, backward = false, left = false, right = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
//have tried putting the line here
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
//and here
        super.onCreate(savedInstanceState);
//and here
        setContentView(R.layout.test_layout);
//and even here just in case, though I'm pretty sure it's not supposed to go here

        Main();

    }

    public void Main(){
        speedPlusButton = (Button) findViewById(R.id.speedPlusButton);
        speedMinusButton = (Button) findViewById(R.id.speedMinusButton);
        speedText = (EditText) findViewById(R.id.speedText);
        forwardButton = (Button) findViewById(R.id.forwardButton);
        backwardButton = (Button) findViewById(R.id.backwardButton);
        leftButton = (Button) findViewById(R.id.leftButton);
        rightButton = (Button) findViewById(R.id.rightButton);



        speedPlusButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                speed = speedText.getText().toString();
                speedInt = Integer.parseInt(speed);

                if(speedInt != 5) {
                    speedInt = speedInt + 1;
                }
                speed = Integer.toString(speedInt);
                speedText.setText(speed);
            }
        });

        speedMinusButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                speed = speedText.getText().toString();
                speedInt = Integer.parseInt(speed);

                if(speedInt != 1) {
                    speedInt = speedInt - 1;
                }
                speed = Integer.toString(speedInt);
                speedText.setText(speed);
            }
        });

        forwardButton.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                switch (event.getAction())
                {
                    case MotionEvent.ACTION_DOWN:
                        forward = true;
                        forwardButton.setBackgroundColor(0xFF7D7D7D);
                        break;
                    case MotionEvent.ACTION_UP:
                        forward = false;
                        forwardButton.setBackgroundColor(0xff222222);
                        break;
                    case MotionEvent.ACTION_CANCEL:
                        forwardButton.setBackgroundColor(0xff222222);
                        forward = false;
                }

                return false;
            }
        });

        backwardButton.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                switch (event.getAction())
                {
                    case MotionEvent.ACTION_DOWN:
                        backward = true;
                        backwardButton.setBackgroundColor(0xFFB9B9B9);
                        break;
                    case MotionEvent.ACTION_UP:
                        backward = false;
                        backwardButton.setBackgroundColor(0xffffffff);
                        break;
                    case MotionEvent.ACTION_CANCEL:
                        backwardButton.setBackgroundColor(0xffffffff);
                        backward = false;
                }

                return false;
            }
        });

        leftButton.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                switch (event.getAction())
                {
                    case MotionEvent.ACTION_DOWN:
                        left = true;
                        leftButton.setBackgroundColor(0xFF7D7D7D);
                        break;
                    case MotionEvent.ACTION_UP:
                        left = false;
                        leftButton.setBackgroundColor(0xff222222);
                        break;
                    case MotionEvent.ACTION_CANCEL:
                        leftButton.setBackgroundColor(0xff222222);
                        left = false;
                }

                return false;
            }
        });

        rightButton.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                switch (event.getAction())
                {
                    case MotionEvent.ACTION_DOWN:
                        right = true;
                        rightButton.setBackgroundColor(0xFFB9B9B9);
                        break;
                    case MotionEvent.ACTION_UP:
                        right = false;
                        rightButton.setBackgroundColor(0xffffffff);
                        break;
                    case MotionEvent.ACTION_CANCEL:
                        rightButton.setBackgroundColor(0xffffffff);
                        right = false;
                }

                return false;
            }
        });
    }

}

1 个答案:

答案 0 :(得分:0)

这样做:

@Override
public void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainmenu);
}