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

时间:2013-11-04 06:20:23

标签: android alarm

我想创建一个Android应用程序来触发警报。我有一个名为btnStart的按钮。当用户单击按钮时,在3秒内,它应该转到AlarmReceiverActivity.java并开始播放声音。

btnStart onclick方法看起来像

btnStart.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            int i = 3;
            Intent intent = new Intent (MapPage.this, AlarmReceiverActivity.class);
                PendingIntent pending =
                        PendingIntent.getActivity(MapPage.this, 2, intent, 
                                            PendingIntent.FLAG_CANCEL_CURRENT);

            AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
                            am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+(i * 1000), pending);

            if (toast != null){
                toast.cancel();
            }

            toast = Toast.makeText(getApplicationContext(), "Alarm for activity is set in : "+ i +" second", toast.LENGTH_SHORT);
            toast.show();                       
            }
        });

并且AlarmReceiverActivity.java看起来像这样

public class AlarmReceiverActivity extends Activity{
private MediaPlayer mMediaPlayer;
private PowerManager.WakeLock mWakeLock;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.alarm);

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Wake Log");
    mWakeLock.acquire();
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN |
            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
            WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
            WindowManager.LayoutParams.FLAG_FULLSCREEN |
            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
            WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

    Button stopAlarm = (Button) findViewById(R.id.stopAlarm);
    stopAlarm.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            mMediaPlayer.stop();
            finish();
        }
    });
    playSound (this, getAlarmUri());
}

为了简短地提出这个简短的问题,有一些方法我不在上面说明。当我点击按钮时,我得到了错误。它说

java.lang.RunTimeException: Unable to start activity ComponentInfo{com.example.splashscreen/com.example.splashscreen.AlarmReceiverActivity}: android.util.AndroidRuntimeException: requestFeature() must be called before adding content

有关如何解决此问题的任何想法?提前谢谢

2 个答案:

答案 0 :(得分:2)

把这个:

this.requestWindowFeature(Window.FEATURE_NO_TITLE);

之前:

setContentView(R.layout.alarm);

来自the docs on requestWindowFeature(强调补充):

  

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

答案 1 :(得分:2)

像这样更改订单..

this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN |
            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
            WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
            WindowManager.LayoutParams.FLAG_FULLSCREEN |
            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
            WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
super.onCreate(savedInstanceState);
setContentView(R.layout.alarm);