Android的 - 对屏幕方向变化处理的对话框对话框、屏幕、方向、Android

2023-09-06 03:00:31 作者:习惯不曾习惯的习惯

我重写了onCreateDialog和prepareDialog方法或对话框类。

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

我跟从雷托•迈耶的专业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;
  }
}

它正常工作,直到我试图改变屏幕的方向,当我做这在prepareDialog得到呼叫,但我得到我所有的变量空指针异常。

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.

所以我presume事情已经离开了书中的例子,我需要重写其他方法保存在什么我的变量?

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

推荐答案

我没有雷托的书,所以我不能检查code,但如果书中的错误,你可以很容易地与他联系。在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){

如果是这样,这是最有可能是某个用户操作改变设置可变比空其他的东西 - 你需要确保这是重新设置活动时重新创建

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.

存储/检索方向之间的活动状态的最标准的方法,在这里详细介绍:http://stackoverflow.com/questions/151777/how-do-i-save-an-android-applications-state

The most standard method to store/retrieve the activity state between orientations is detailed here: http://stackoverflow.com/questions/151777/how-do-i-save-an-android-applications-state

 
精彩推荐