我为editText.setText(buttonText);
行获取了NullPointerException,因此看起来我的editText
在方向更改后被销毁。我知道活动生命周期,但我不确定在哪里放置editText
的声明。
然而,buttonText似乎已正确填充。
我的课程:
package com.example.orientation;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import java.io.OutputStream;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager;
import android.widget.EditText;
@SuppressLint("NewApi")
public class MainActivity extends FragmentActivity {
EditText editText;
String buttonText = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("StateInfo", "onCreate");
editText = (EditText) findViewById(R.id.txtField1);
if (savedInstanceState != null) {
editText.setText(buttonText);
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putString("buttonId", editText.getText().toString());
super.onSaveInstanceState(outState);
};
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
buttonText = savedInstanceState.getString("buttonId");
//editText.setText(buttonText);
};
}
XML:
<EditText
android:id="@+id/txtField1"
android:inputType="text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
编辑:排序!我有另一个横向视图布局,在那个XML上我没有EditText元素。