根据布局文件,仅从现有 ImageViews/TextBoxes 中保存字符串/图像的最佳方法是什么?我应该只检查我当前正在使用的布局(使用 contentView 变量)并基于从那些现有 TextBoxes 中保存的内容吗?
所以我有 4 种横向和纵向布局:
- main.xml(从 SD 或应用程序本身选择图像)
- imagefromsd.xml(SD的对应布局)
- imagefromapplication(应用程序的对应布局)
- finishandupload.xml(我在其中显示结果并提供保存/上传图像的选项)
所以问题是在我使用protected void onSaveInstanceState(Bundle outState)
和protected void onRestoreInstanceState(Bundle savedInstanceState)
.
发生这种情况是因为我保存了来自文本框的字符串和来自 ImageViews 的图像,但并非每个布局文件都有所有文本框,所以当我尝试从不存在的文本框保存字符串时,我得到空指针异常。
现在,当我更改 setContentView 时,我将当前的 contentView 保存在一个变量中。
public void ShowLoadAPP(View v){ //Leave menu and show view for images from application
setContentView(R.layout.imagefromapplication);
contentView = R.layout.imagefromapplication;
LoadDrawables(); //Load list with images and show the first image
}
public void ShowLoadSD(View v){ //Leave menu and show view for images from sd
setContentView(R.layout.imagefromsd);
contentView = R.layout.imagefromsd;
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onRestoreInstanceState(savedInstanceState);
setContentView(savedInstanceState.getInt("MyView"));
//Lines where application crashes because of nullpointer where the retrieved string doesn't exist.
((ImageView)findViewById(R.id.imgMeme)).setImageResource(savedInstanceState.getInt("MyImage"));
((EditText)findViewById(R.id.txtImageTop)).setText(savedInstanceState.getString("MyTopString"));
((EditText)findViewById(R.id.txtImageButtom)).setText(savedInstanceState.getString("MyButtomString"));
((ImageView)findViewById(R.id.imgMemeFinished)).setImageBitmap((Bitmap) savedInstanceState.getParcelable("MyBitmap"));
}
@Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
contentView = contentView == null ? R.layout.menu : contentView;
selectedImage = selectedImage == null ? R.drawable.are_you_fucking_kidding : selectedImage;
stringTop = ((EditText)findViewById(R.id.txtImageTop)).getText().toString() == null ? "" : ((EditText)findViewById(R.id.txtImageTop)).getText().toString();
stringButtom = ((EditText)findViewById(R.id.txtImageButtom)).getText().toString() == null ? "" : ((EditText)findViewById(R.id.txtImageButtom)).getText().toString();
combinedImage = combinedImage;
outState.putInt("MyView", contentView);
outState.putInt("MyImage", selectedImage);
outState.putString("MyTopString", stringTop);
outState.putString("MyButtomString", stringButtom);
outState.putParcelable("MyBitmap", combinedImage);
super.onSaveInstanceState(outState);
}
<ImageView
android:id="@+id/imgMemeFinished"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="50"
android:contentDescription="@string/imgMemeFinishedDescription" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/btnSave"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:onClick="SaveImage"
android:text="@string/btnSave" />
<Button
android:id="@+id/btnUpload"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:onClick="UploadImage"
android:text="@string/btnUpload" />
</LinearLayout>
<ImageView
android:id="@+id/imgMeme"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="50"
android:contentDescription="@string/imgMemeDescription" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="@+id/txtImageTop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:ems="10"
android:inputType="text"
android:text="@string/txtImageTop" />
<EditText
android:id="@+id/txtImageButtom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:ems="10"
android:inputType="text"
android:text="@string/txtImageButtom" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/btnPreviousImage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:onClick="PreviousImage"
android:text="@string/btnPreviousImage" />
<Button
android:id="@+id/btnNextImage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:onClick="NextImage"
android:text="@string/btnNextImage" />
<Button
android:id="@+id/btnFinish"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:onClick="ShowFinish"
android:text="@string/btnFinish" />
</LinearLayout>