方向更改时将字符串保留在登录对话框窗口中。的Android

时间:2017-02-18 21:17:01

标签: android string dialog onchange screen-orientation

我正在尝试在方向更改时保留用户在登录对话框窗口中输入的内容,但我始终收到此错误消息:

  

尝试调用虚拟方法'android.view.View   空对象上的android.widget.RelativeLayout.findViewById(int)'   参考

有代码:

公共类ReservationActivity扩展了AppCompatActivity {

ImageView uDeM_Logo;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_reservation);

    uDeM_Logo = (ImageView)findViewById(R.id.UdeM_Logo);

    dimensions();
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    RelativeLayout login = (RelativeLayout)findViewById(R.id.loginLayout);

    EditText userField = (EditText) login.findViewById(R.id.userEditText);
    EditText passField = (EditText) login.findViewById(R.id.passEditText);
    String user = userField.getText().toString();
    String pass = passField.getText().toString();

    outState.putString("User", user);
    outState.putString("Pass", pass);
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);

    RelativeLayout login = (RelativeLayout) findViewById(R.id.loginLayout);

    EditText userField = (EditText) login.findViewById(R.id.userEditText);
    EditText passField = (EditText) login.findViewById(R.id.passEditText);
    String user = savedInstanceState.getString("User");
    String pass = savedInstanceState.getString("Pass");

    userField.setText(user);
    passField.setText(pass);
}

public void onConfigurationChanged(Configuration nouvOrient) {
    if(nouvOrient.orientation == Configuration.ORIENTATION_LANDSCAPE ||
    nouvOrient.orientation == Configuration.ORIENTATION_PORTRAIT)
        dimensions();
}

public void dimensions() {
    Display display = getWindowManager().getDefaultDisplay();
    Point grandeur = new Point();
    display.getSize(grandeur);
    double hauteur = grandeur.y, dim = hauteur * 0.2492;
    int dimsInt = (int) dim;

    ViewGroup.LayoutParams parametres = uDeM_Logo.getLayoutParams();
    parametres.width = dimsInt;
    parametres.height = dimsInt;

    uDeM_Logo.setLayoutParams(parametres);


}

public void loginDialog(View log){
    final Dialog login = new Dialog(this);
    login.setContentView(R.layout.login_dialog);

    Button btnLogin = (Button)login.findViewById(R.id.dialogLoginBtn);
    Button btnCancel = (Button)login.findViewById(R.id.dialogCancelBtn);

    btnLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(ReservationActivity.this,
                    "Login Sucessfull", Toast.LENGTH_SHORT).show();
        }
    });

    btnCancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            login.dismiss();
        }
    });

    login.show();
}

}

enter image description here

2 个答案:

答案 0 :(得分:0)

请勿在{{1​​}}

中执行此操作
onRestoreInstanceState()

但是,将这些 RelativeLayout login = (RelativeLayout) findViewById(R.id.loginLayout); EditText userField = (EditText) login.findViewById(R.id.userEditText); EditText passField = (EditText) login.findViewById(R.id.passEditText); loginuserField变为Activity类中的字段变量,如下所示:

passField

同时将以上3个代码行从private RelativeLayout login; private EditText userField, passField; 移至onSaveInstanceState(),但修改它们以使用您的字段变量,如下所示:

onCreate()

我建议您恢复 login = (RelativeLayout) findViewById(R.id.loginLayout); userField = (EditText) login.findViewById(R.id.userEditText); passField = (EditText) login.findViewById(R.id.passEditText); 而不是onCreate()的状态,如下所示:

onRestoreInstanceState()

原因是因为// Check whether we're recreating a previously destroyed instance if (savedInstanceState != null) { // Restore value of members from saved state String user = savedInstanceState.getString("User"); String pass = savedInstanceState.getString("Pass"); userField.setText(user); passField.setText(pass); } else { // Probably initialize members with default values for a new instance } 之后调用了onRestoreInstanceState(),而onStart()之前调用了onCreate()

答案 1 :(得分:0)

您似乎无需在方向更改上重新创建活动。您只需在manifest

中为您的活动设置此配置即可
android:configChanges="keyboardHidden|orientation