문제

Here's my MAIN ACTIVITY

    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);
}

I have DateTimePicker.java class which has 1 button and 1 Textview. Clicking on button, my another class Calendar.java get populated in PopupWindow and this Popup window displays my class Calendar.java . I have created different layouts of my Calendar.java class for portrait and landscape mode. Here's DateTimePicker.java some snippet of code,

    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 contain DateTimePicker . Actually I wanted my Popup window to be opened up even when orientation gets changed at run time, so I have done it through setting flag openPopup = true; in openCalendar() method and if it is opened and orientation gets changed at run time, this flag will be saved in onSaveInstanceState() method. After orientation will change, it will be checked in onCreate() and popup will be opened up for respective orientation mode. I hope you got my point.

PROBLEM: Initially When I click on button in Portrait mode, popup window pops up for portrait layout. then without dismissing popup window, I change the orientation to landscape. And after changing, I can see my popup window as intact and appears on screen of landscape layout. Till now it works fine. But IF popup window is opened up in landscape mode and then I change the orientation to portrait, popup window of portrait layout didn't come up and I see FORCE CLOSE message:/ Please help since I am working behind it so long and getting no clue. I would be very grateful to you all. Thanks!

P.S.: Changing orientation means I am pressing ctrl+F11 and changing orientation of Emulator

도움이 되었습니까?

해결책

The emulator has an odd feature (some consider it a bug) in which changing from landscape to portrait in the emulator causes two configuration changes and two restarts of your activity. (One configuration change is the orientation and the other is an emulated change in the keyboard state.) The timing of the configuration changes frequently causes crashes like this. Try adding this attribute:

android:configChanges="keyboard|keyboardHidden"

to your <activity> tag in the manifest. See if that improves the situation.

다른 팁

Make sure you have your layout defined in layout-land folder and ensure onCreate is not called again and again. android:configChanges="keyboard|keyboardHidden" Put this in your manifest file so that the state is retained when you change orientation

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top