我已经看到了一些如何实现这一目标的例子,但我仍然无法使其发挥作用。
我遵循了this示例,TextView
在OnCreate
中为空,当我旋转屏幕时。以下是我实施它的方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_image_viewer);
displayFileName = (TextView) findViewById(R.id.displayFileName);
//Getting the fileName
Bundle extras = getIntent().getExtras();
uri = extras.getString("uriAsString");
Uri myUri = Uri.parse(uri);
filename = uri.substring(uri.lastIndexOf("/") + 1);
nameWitoutExtention = filename.substring(0, filename.lastIndexOf('.'));
//Checking if Text was stored
if (savedInstanceState != null) {
CharSequence savedText = savedInstanceState.getCharSequence(KEY_TEXT_VALUE);
displayFileName.setText(savedText);
}
}
@Override
protected void onSaveInstanceState (Bundle outState) {
super.onSaveInstanceState(outState);
outState.putCharSequence(KEY_TEXT_VALUE, displayFileName.getText());
}
我想要达到的目的是将nameWitoutExtention
设置为displayFileName
,并在设备旋转时让文字保留。
我在文本上有一个空指针,这就是我尝试这个的原因,对我做错的任何帮助?
修改
这就是我目前的实施情况:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_image_viewer);
displayFileName = (TextView) findViewById(R.id.displayFileName);
if(savedInstanceState != null){
CharSequence savedText = savedInstanceState.getCharSequence(KEY_TEXT_VALUE);
//When displaying this toast the correct filename gets displayed
//Toast.makeText(getApplicationContext(), savedText, Toast.LENGTH_LONG).show();
displayFileName.setText(savedText+"");
}
else{
//Getting the fileName
Bundle extrsas = getIntent().getExtras();
uri = extrsas.getString("selectedStudentVid");
Uri mUri = Uri.parse(uri);
filename = uri.substring(uri.lastIndexOf("/") + 1);
nameWitoutExtention = filename.substring(0, filename.lastIndexOf('.'));
displayFileName.setText(nameWitoutExtention);
}
@Override
protected void onSaveInstanceState (Bundle outState) {
super.onSaveInstanceState(outState);
outState.putCharSequence(KEY_TEXT_VALUE, displayFileName.getText());
}
我也按照@PorasBhardwaj
的建议尝试了onRestoreInstanceState()
我得到的例外是:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
答案 0 :(得分:2)
尝试使用onRestoreInstanceState()
回调方法获取已保存的数据。
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onRestoreInstanceState(savedInstanceState);
CharSequence savedText = savedInstanceState.getCharSequence(KEY_TEXT_VALUE);
displayFileName.setText(savedText);
}
根据官方文件。
大多数实现只会使用onCreate(Bundle)来恢复它们 状态,但有时候在这里做完之后很方便 初始化已完成或允许子类决定是否 使用您的默认实现。
因此,为了获得最佳实践,请将您的视图层次结构保留在onCreate中,并在onRestoreInstanceState中恢复之前的状态。
答案 1 :(得分:1)
尝试这样做:
//Checking if Text was stored
if(savedInstanceState != null){
CharSequence savedText =
savedInstanceState.getCharSequence(KEY_TEXT_VALUE);
displayFileName.setText(savedText + "");
}
else{
//Getting the fileName
Bundle extras = getIntent().getExtras();
uri = extras.getString("uriAsString");
Uri myUri = Uri.parse(uri);
filename = uri.substring(uri.lastIndexOf("/") + 1);
nameWitoutExtention = filename.substring(0, filename.lastIndexOf('.'));
}
你做错了什么。每当方向改变时,它再次从额外值获得值,在旋转改变之后为null,并且它将相同的值传递给它。这就是你获得例外的原因。 试试这个希望它会对你有帮助。