简体   繁体   English

Android-处理屏幕方向更改对话框

[英]Android - Dealing with a Dialog on Screen Orientation change

I am overriding the onCreateDialog and onPrepareDialog methods or the Dialog class. 我重写onCreateDialog和onPrepareDialog方法或Dialog类。

I have followed the example from Reto Meier's Professional Android Application Development book, Chapter 5 to pull some XML data and then use a dialog to display the info. 我按照Reto Meier的Professional Android Application Development一书(第5章)中的示例提取一些XML数据,然后使用对话框来显示信息。

I have basically followed it exactly but changed the variables to suit my own XML schema as follows: 我基本上完全遵循了它,但是更改了变量以适合我自己的XML模式,如下所示:


@Override
public Dialog onCreateDialog(int id) {
  switch(id) {
    case (SETTINGS_DIALOG) :        
      LayoutInflater li = LayoutInflater.from(this);
      View settingsDetailsView = li.inflate(R.layout.details, null);

      AlertDialog.Builder settingsDialog = new AlertDialog.Builder(this);
      settingsDialog.setTitle("Provisioned Settings");         
      settingsDialog.setView(settingsDetailsView);
return settingsDialog.create();
  }
  return null;
}

@Override
public void onPrepareDialog(int id, Dialog dialog) {
  switch(id) {
    case (SETTINGS_DIALOG) :                  

String afpunText = "  ";

     if(setting.getAddForPublicUserNames() == 1){
      afpunText = "Yes";
     }
     else{
      afpunText = "No";
     }
      String Text = "Login Settings: " + "\n" 
                       + "Password: " + setting.getPassword()  + "\n" 
                       + "Server: " + setting.getServerAddress() + "\n";


      AlertDialog settingsDialog = (AlertDialog)dialog; 
settingsDialog.setTitle(setting.getUserName());

tv = (TextView)settingsDialog.findViewById(R.id.detailsTextView);
if (tv != null)
        tv.setText(Text);

      break;
  }
}

It works fine until I try changing the screen orientation, When I do this onPrepareDialog gets call but I get null pointer exceptions on all my variables. 在我尝试更改屏幕方向之前,它工作正常。当我执行此操作时,onPrepareDialog会被调用,但所有变量都会出现空指针异常。

The error still occurs even when I tell my activity to ignore screen orientation in the manifest. 即使我告诉我的活动忽略清单中的屏幕方向,该错误仍然会发生。

So I presume something has been left out of the example in the book do I need to override another method to save my variables in or something? 因此,我认为书中的示例遗漏了一些东西,我是否需要重写另一种方法来保存变量?

I have now added the following: 我现在添加了以下内容:


@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
  // Save UI state changes to the savedInstanceState.
  // This bundle will be passed to onCreate if the process is
  // killed and restarted.
  savedInstanceState.putString("Username", setting.getUserName());
  savedInstanceState.putString("Password", setting.getPassword());
  savedInstanceState.putString("Server", setting.getServerAddress());

  super.onSaveInstanceState(savedInstanceState);
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);
  settings.clear();
  // Restore UI state from the savedInstanceState.
  // This bundle has also been passed to onCreate.
  username = savedInstanceState.getString("Username");
  password = savedInstanceState.getString("Password");
  serveraddress = savedInstanceState.getString("Server");


Settings setting = new Settings(username, password, serveraddress);
addNewSettings(setting);

}

But I'm still getting the Null Pointer exception 但是我仍然收到Null Pointer异常

I don't have Reto's book so I can't check the code, but if there is a mistake in the book you can contact him easily on Twitter to discuss it: http://twitter.com/retomeier 我没有Reto的书,所以我无法检查代码,但是如果书中有错误,您可以在Twitter上轻松与他联系以进行讨论: http : //twitter.com/retomeier

Remember that if the screen orientation changes, your activity is recreated. 请记住,如果屏幕方向发生变化,则会重新创建您的活动。 That means that any variables you set the last time around, which are not static, will be lost after the activity is recreated under the new orientation. 这意味着在新的方向下重新创建活动后,您上次设置的所有变量(不是静态变量)都将丢失。

My guess is that you hit a NullPointerException here: 我的猜测是您在此处命中了NullPointerException:

if(setting.getAddForPublicUserNames() == 1){

If so, it is most likely that some user action changes the "setting" variable to something other than null - you need to make sure this is set again when the activity is recreated. 如果是这样,则很可能某些用户操作会将“设置”变量更改为非null的值-您需要确保在重新创建活动时再次将其设置。

The most standard method to store/retrieve the activity state between orientations is detailed here: Saving Android Activity state using Save Instance State 此处详细介绍了在方位之间存储/检索活动状态的最标准方法: 使用“保存实例状态”保存Android活动状态

When you change orientation, android restart your activity, you need to save the estate with 当您更改方向时,android重新启动您的活动,您需要使用

onSaveInstanceState

and recover with 并与恢复

savedInstanceState

If you don't want the activity to be recreated when orientation changes. 如果您不希望在方向更改时重新创建活动。 Insert the following line in the AndroidManifest.xml. 将以下行插入AndroidManifest.xml中。

android:configChanges="keyboardHidden|orientation

example: 例:

<activity android:name=".FABSLogin" android:label="@string/app_name" android:theme="@style/Theme.NoWindowTitle" android:noHistory="true" **android:configChanges="keyboardHidden|orientation**"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM