根據佈局文件,僅從現有ImageView/TextBoxes保存字符串/圖像的最佳方法是什麼?我應該只檢查一下我當前使用的佈局(使用contentView變量)並基於那些現有文本框的保存或?將變量保存爲onsavedinstance並確定它們是否爲空
所以我有4個佈局。風景和肖像:
- main.xml中
- imagefromsd.xml(對應佈局SD) (從SD或應用程序本身選擇圖像之間進行選擇)
- imagefromapplication(對應的應用程序佈局)
- finishandupload.xml(我在哪裏顯示結果,並給予選項,保存/上傳圖片)
所以問題是我在使用protected void onSaveInstanceState(Bundle outState)
和protected void onRestoreInstanceState(Bundle savedInstanceState)
在橫向和縱向之間切換時會出現空指針異常。
發生這種情況是因爲我從文本框和圖像保存來自ImageViews的字符串,但並非每個佈局文件都具有所有文本框,所以當我嘗試從未出現的文本框中保存字符串時,會出現空指針異常。
現在我正在將當前contentView保存在變量中,當我更改setContentView時。
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>
用intents而不是1個活動重寫了應用程序,現在很好。希望我沒有必要,但哦:P – 2013-04-08 23:57:12