这是我的主要活动
public static boolean popupStatus=false;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if (savedInstanceState != null){
popupStatus = savedInstanceState.getBoolean("Open");
}
setContentView(R.layout.main);
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putBoolean("Open", DateTimePicker.openPopup);
super.onSaveInstanceState(savedInstanceState);
}
我的DateTimePicker.java
课程有 1 个按钮和 1 个 Textview。单击按钮,我的另一个类Calendar.java
被填充到 PopupWindow 中,这个弹出窗口显示我的类Calendar.java
。我Calendar.java
为纵向和横向模式创建了不同的班级布局。这是DateTimePicker.java
一些代码片段,
public static boolean openPopup = false;
textView = new TextView(this.getContext());
this.addView(textView, layoutParams);
button = new Button(this.getContext());
button.setText("C");
this.addView(button, layoutParams1);
button.setOnClickListener(this);
if(Main.popupStatus){
button.performClick();
}
public void onClick(View v) {
if(Main.popupStatus){
new Handler().postDelayed(new Runnable() {
public void run() {
openCalendar();
}
}, 100);
}
else{
openCalendar();
}
private void openCalendar() {
Calendar calendar = new Calendar(this.getContext());
if(portrait.equals(orientation)){
pw = new PopupWindow(calendarLayout, 245, 284, true);
}
else{
pw = new PopupWindow(calendarLayout, 295, 240, true);
}
pw.setOutsideTouchable(false);
pw.showAtLocation(this, Gravity.NO_GRAVITY, 10, 80);
openPopup = true;
}
public void closeCalendar(){
pw.dismiss();
openPopup = false;
}
Main.XML 包含DateTimePicker
. 实际上我希望我的弹出窗口即使在运行时改变方向也能打开,所以我通过openPopup = true;
在openCalendar()
方法中设置标志来完成它,如果它打开并且在运行时改变方向,这个标志将保存在onSaveInstanceState()
方法中。方向改变后,将被检入onCreate()
并打开相应方向模式的弹出窗口。我希望你明白我的意思。
问题:最初,当我在纵向模式下单击按钮时,会弹出纵向布局的弹出窗口。然后在不关闭弹出窗口的情况下,我将方向更改为横向。更改后,我可以看到我的弹出窗口完好无损,并出现在横向布局的屏幕上。到目前为止,它工作正常。但是如果弹出窗口在横向模式下打开,然后我将方向更改为纵向,纵向布局的弹出窗口没有出现,我看到 FORCE CLOSE 消息:/ 请帮助,因为我在它后面工作了这么长时间并且没有线索。我会非常感谢你们。谢谢!
PS:改变方向意味着我正在按 ctrl+F11 并改变模拟器的方向