Note that there are some explanatory texts on larger screens.

plurals
  1. POSaving variables to onsavedinstance and determine whether or not they aren't null
    primarykey
    data
    text
    <p><strong>What would be the best way to only save strings/images from existing ImageViews/TextBoxes depending on the layout file? Should I just check what layout I'm currently using (using the contentView variable) and based on that save from those existing TextBoxes or ?</strong></p> <p>So I have 4 Layouts for both Landscape and Portrait:</p> <ol> <li>main.xml (Choose between select image from SD or from application itself)</li> <li>imagefromsd.xml (Corresponding layout for SD)</li> <li>imagefromapplication (Corresponding layout for application)</li> <li>finishandupload.xml (Where I display the result and give option to save/upload image)</li> </ol> <p>So the problem is I get nullpointer exceptions when switching between landscape and portrait I use <code>protected void onSaveInstanceState(Bundle outState)</code> and <code>protected void onRestoreInstanceState(Bundle savedInstanceState)</code>.</p> <p>This happens because I save strings from textboxes and images from ImageViews, but not every layout file has all textboxes, so when I try to save a string from an unexisting textbox I get nullpointer exceptions.</p> <p>Right now I'm saving the current contentView in a variable when I change the setContentView.</p> <pre><code> 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); } </code></pre> <p> </p> <pre><code>&lt;ImageView android:id="@+id/imgMemeFinished" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="50" android:contentDescription="@string/imgMemeFinishedDescription" /&gt; &lt;LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" &gt; &lt;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" /&gt; &lt;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" /&gt; &lt;/LinearLayout&gt; </code></pre> <p></p> <p> </p> <pre><code>&lt;ImageView android:id="@+id/imgMeme" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="50" android:contentDescription="@string/imgMemeDescription" /&gt; &lt;LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" &gt; &lt;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" /&gt; &lt;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" /&gt; &lt;/LinearLayout&gt; &lt;LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" &gt; &lt;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" /&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;/LinearLayout&gt; </code></pre> <p></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload