根据布局文件,仅从现有ImageViews / TextBox中保存字符串/图像的最佳方法是什么?我应该只检查我当前正在使用的布局(使用contentView变量)并基于那些现有TextBox的保存?
所以我有四个横向和横向布局:
所以问题是我在横向和纵向之间切换时会得到nullpointer异常我使用protected void onSaveInstanceState(Bundle outState)
和protected void onRestoreInstanceState(Bundle savedInstanceState)
。
这是因为我从ImageViews的文本框和图像中保存字符串,但并非每个布局文件都包含所有文本框,所以当我尝试从未存在的文本框中保存字符串时,我会得到nullpointer异常。
现在我在更改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>
答案 0 :(得分:0)
我建议你为不同的屏幕使用不同的活动。 如果您计划在同一个活动中包含所有布局,请将它们放在同一个xml文件中,否则在每个布局上使用不同的活动和布局。要跨活动进行通信和发送数据,您可以将信息放在Intent中,或者如果您需要更复杂的信息,请使用持久性内存(DB,共享偏好,其他)。