Search code examples
androidcrashlifecycle

onSaveInstanceState crashes my app


why does my app crash, when I leave my activity? To be more specific: Everything worked fine until I implemented onSaveInstanceState() and onRestoreInstanceState()

Here is the code:

 @Override
 protected void onSaveInstanceState (Bundle outState){


        outState.putString("d1",(((EditText) findViewById (R.id.request_detail1)).getText()).toString());
        outState.putString("d2",(((EditText) findViewById (R.id.request_detail2)).getText()).toString());
        outState.putString("d3",(((EditText) findViewById (R.id.request_detail3)).getText()).toString());
        outState.putString("d4",(((EditText) findViewById (R.id.request_detail4)).getText()).toString());
        outState.putString("inputNr", (((EditText) findViewById (R.id.poRequest)).getText()).toString());
        super.onSaveInstanceState(outState);
    }



@Override
   protected void onRestoreInstanceState (Bundle savedInstanceState){
            super.onRestoreInstanceState(savedInstanceState);
             ((TextView) findViewById (R.id.request_detail1)).setText(savedInstanceState.getString("d1"));
             ((TextView) findViewById (R.id.request_detail2)).setText(savedInstanceState.getString("d2"));
             ((TextView) findViewById (R.id.request_detail3)).setText(savedInstanceState.getString("d3"));
             ((TextView) findViewById (R.id.request_detail4)).setText(savedInstanceState.getString("d4"));
             ((EditText) findViewById (R.id.poRequest)).setText(savedInstanceState.getString("inputNr"));

        }

LogCat tells me 08-02 05:23:09.791: E/AndroidRuntime(873): at net.frontend.androidapp.statusrequest.PurchaseOrderRequest.onSaveInstanceState(PurchaseOrderRequest.java:47)

the refered line is outState.putString("d1",(((EditText) findViewById (R.id.request_detail1)).getText()).toString());

I am a little bit confused, as soon as the onSaveInstanceState() is called, the app crashes.

and here is the whole code, in case I the error is caused by something else

    package net.frontend.androidapp.statusrequest;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class PurchaseOrderRequest extends Activity {

private static String preqnr="";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.purchase_order_request_activity);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            // Show the Up button in the action bar.
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
@Override
    protected void onSaveInstanceState (Bundle outState){


        outState.putString("d1",(((EditText) findViewById (R.id.request_detail1)).getText()).toString());
        outState.putString("d2",(((EditText) findViewById (R.id.request_detail2)).getText()).toString());
        outState.putString("d3",(((EditText) findViewById (R.id.request_detail3)).getText()).toString());
        outState.putString("d4",(((EditText) findViewById (R.id.request_detail4)).getText()).toString());
        outState.putString("inputNr", (((EditText) findViewById (R.id.poRequest)).getText()).toString());
        super.onSaveInstanceState(outState);
    }
    @Override
    protected void onRestoreInstanceState (Bundle savedInstanceState){
        super.onRestoreInstanceState(savedInstanceState);
         ((TextView) findViewById (R.id.request_detail1)).setText(savedInstanceState.getString("d1"));
         ((TextView) findViewById (R.id.request_detail2)).setText(savedInstanceState.getString("d2"));
         ((TextView) findViewById (R.id.request_detail3)).setText(savedInstanceState.getString("d3"));
         ((TextView) findViewById (R.id.request_detail4)).setText(savedInstanceState.getString("d4"));
         ((EditText) findViewById (R.id.poRequest)).setText(savedInstanceState.getString("inputNr"));

    }
    public void showDetail(View view){

        Intent intent = new Intent(this, OrderDetails.class);
        startActivity(intent);
    }
    public void checkStatus (View view) {

         EditText edit = (EditText) findViewById (R.id.poRequest);

             String s= edit.getText().toString();
             if (s.equals("")){
                 // give error message
             }
             else{

                 long lineNr=Long.parseLong(s);

                new BackgroundWorker().execute(lineNr); 
             }

    }


    public class BackgroundWorker extends AsyncTask<Long, Void, String[]> {

                     private String METHOD_NAME = "parser" ; 
                 private String NAMESPACE = "http://statusrequest.androidapp.webservice.backend.net";
                 private String SOAP_ACTION = NAMESPACE + METHOD_NAME;  
                 private static final String URL = "http://10.35.105.31:8080/SAPInterfaceWebservice/services/XMLTransfromer?wsdl";
                 ProgressDialog pd = new ProgressDialog(PurchaseOrderRequest.this);

                    @Override
                    protected String[] doInBackground(Long... lineNr) {
                        String[] failure = new String [1];
                        long lineNumber = lineNr[0];
                        try
                     {
                     SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
                     request.addProperty("lineNr", lineNumber); 
                     publishProgress();
                     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                     envelope.dotNet = true;
                     envelope.setOutputSoapObject(request);
                     HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
                     androidHttpTransport.call(SOAP_ACTION,envelope);
                     Object result = envelope.getResponse();
                     String hString = result.toString();
                     String[] details = hString.split(",");

                     preqnr=details[0].substring(1);
                     return details;
                     } catch (Exception E) {
                     E.printStackTrace();
                     failure[0]="ERROR: "    + E.getClass().getName() + " : " + E.getMessage(); 
                     }
                    return failure; 
                    }      

                    @Override
                    protected void onPostExecute(String[] result) {

                         ((TextView) findViewById (R.id.request_detail1)).setText("PO_Number: " +result[0].substring(1));
                         ((TextView) findViewById (R.id.request_detail2)).setText("Result: " +result[1]);
                         ((TextView) findViewById (R.id.request_detail3)).setText("Result: " +result[2]);
                         ((TextView) findViewById (R.id.request_detail4)).setText("Result: " +result[3]);
                         pd.dismiss();    
                    }

                    @Override
                    protected void onPreExecute() {

                        pd.setMessage("Sending...");
                        pd.show();

                    }

                    @Override
                    protected void onProgressUpdate(Void... values) {


                        }

                }
    public static String getPreqnr() {
        return preqnr;
        }

    }

So what did I do wrong to cause the app to crash?

If you need any further information, which i missed to point out, please leave a comment and I'll add them.

Thank you for your help!


Solution

  • What type of object is a view with id "R.id.request_detail1" ? In onSaveInstanceState you are casting it to EditText, but in onRestoreInstanceState you are casting it to TextView.

    If it is a TextView, then first cast will cause an exception.