onSaveInstanceState崩溃我的应用程序

时间:2013-08-02 05:37:07

标签: android crash lifecycle

为什么我的应用程序崩溃,当我离开我的活动?更具体一点:在我实施onSaveInstanceState()onRestoreInstanceState()

之前,一切正常

以下是代码:

 @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告诉我 08-02 05:23:09.791: E/AndroidRuntime(873): at net.frontend.androidapp.statusrequest.PurchaseOrderRequest.onSaveInstanceState(PurchaseOrderRequest.java:47)

引用的行是 outState.putString("d1",(((EditText) findViewById (R.id.request_detail1)).getText()).toString());

我有点困惑,只要调用onSaveInstanceState(),应用程序就会崩溃。

这是整个代码,如果我错误是由其他原因引起的

    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;
        }

    }

那么我做错了什么导致应用程序崩溃?

如果您需要任何我想错过的更多信息,请发表评论,我会添加它们。

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

id为“R.id.request_detail1”的视图是什么类型的对象?在onSaveInstanceState中,您将其转换为EditText,但在onRestoreInstanceState中,您将其转换为TextView。

如果是TextView,那么首次强制转换会导致异常。