Android - 处理屏幕上的对话方向更改 [英] Android - Dealing with a Dialog on Screen Orientation change

查看:147
本文介绍了Android - 处理屏幕上的对话方向更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我覆盖了onCreateDialog和onPrepareDialog方法或Dialog类。

I am overriding the onCreateDialog and onPrepareDialog methods or the Dialog class.

我遵循了Reto Meier的专业Android应用程序开发书第5章中的一些例子, XML数据,然后使用一个对话框来显示信息。

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.

我已经基本上按照它完全改变了变量,以适合我自己的XML模式,如下所示:

I have basically followed it exactly but changed the variables to suit my own XML schema as follows:

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






它工作正常,直到我尝试改变屏幕方向,当我这样做onPrepareDialog获取调用,但我得到所有变量的空指针异常。


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.

即使在我告诉我的活动,以忽略清单中的屏幕方向。

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

推荐答案

我没有Reto的书,所以我无法检查代码,但如果书中有错误,您可以在Twitter上轻松与他联系,进行讨论: http://twitter.com/ retomeier

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

请记住,如果屏幕方向更改,则会重新创建您的活动。这意味着,在新方向重新创建活动后,您最后设置的任何变量(不是静态的)将丢失。

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.

我的猜测是,你在这里点击NullPointerException:

My guess is that you hit a NullPointerException here:

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

如果是这样,很可能某些用户操作将设置变量更改为null以外的内容 - 您需要确保这是在活动重新创建时再次设置。

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.

这里详细介绍了存储/检索方向之间活动状态的最标准方法:保存Android Activity状态

The most standard method to store/retrieve the activity state between orientations is detailed here: Saving Android Activity state

这篇关于Android - 处理屏幕上的对话方向更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆