回転時に TextViews と EditTexts の値を保持するアプリを開発しようとしています。EditText が選択されていないときは、ソフト キーボードも非表示にしたいと思います。ただし、何らかの理由で、results
が表示されている場合hideKeyboard()
、複数の回転後にメソッドがクラッシュします。
犯人である可能性のある私の方法のいくつかを次に示します。
/**
* Detects if text boxes are not selected to hide keyboard
* @param view - main view
*/
private void setupUI(View view) {
if(!(view instanceof EditText)) {
view.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
hideKeyboard();
return false;
}
});
}
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
View innerView = ((ViewGroup) view).getChildAt(i);
setupUI(innerView);
}
}
}
/**
* Hides software keyboard
*/
private void hideKeyboard() {
InputMethodManager inputMethodManager = (InputMethodManager) this.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
}
/**
* Saves instance state on rotation
* @param outState - current state before rotation
*/
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putString("loabox", loaBox.getText().toString());
outState.putString("lwlbox", lwlBox.getText().toString());
outState.putString("beambox", beamBox.getText().toString());
outState.putString("displacementbox", displacementBox.getText().toString());
outState.putString("sabox", saBox.getText().toString());
if (results != null && !isEmpty(mhsText) && !isEmpty(dlText) && !isEmpty(sadText) && !isEmpty(capsizeText) && !isEmpty(comfortText)) {
outState.putInt("results", results.getVisibility());
outState.putString("mhs", mhsText.getText().toString());
outState.putString("dl", dlText.getText().toString());
outState.putString("sad", sadText.getText().toString());
outState.putString("capsize", capsizeText.getText().toString());
outState.putString("comfort", comfortText.getText().toString());
}
super.onSaveInstanceState(outState);
}
/**
* Restores previous instance state after rotation
* @param savedInstanceState - saved previous state before rotation
*/
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
if (savedInstanceState != null) {
loaBox.setText(savedInstanceState.getString("loabox"));
lwlBox.setText(savedInstanceState.getString("lwlbox"));
beamBox.setText(savedInstanceState.getString("beambox"));
displacementBox.setText(savedInstanceState.getString("displacementbox"));
saBox.setText(savedInstanceState.getString("sabox"));
mhsText.setText(savedInstanceState.getString("mhs"));
dlText.setText(savedInstanceState.getString("dl"));
sadText.setText(savedInstanceState.getString("sad"));
capsizeText.setText(savedInstanceState.getString("capsize"));
comfortText.setText(savedInstanceState.getString("comfort"));
results.setVisibility(savedInstanceState.getInt("results", results.getVisibility()));
}
}
setupUI()
メソッドは、メソッドの最後で呼び出されますonCreate()
。
ここで何が問題なのか誰か知っていますか?