当我离开 Activity 时,为什么我的应用程序崩溃?更具体地说:在我实现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;
        }

    }

那么,我做错了什么导致该应用崩溃了?

如果您需要进一步的信息(我想指出),请发表评论,我将其添加。

感谢您的帮助!

最佳答案

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

如果它是TextView,则第一次强制转换将导致异常。

10-08 13:56