2013-08-02 156 views
0

爲什麼我的應用程序崩潰,當我離開我的活動?更具體地講:一切工作正常,直到我實現onSaveInstanceState()onRestoreInstanceState()onSaveInstanceState崩潰我的應用程序

下面是代碼:

@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)

的refered線 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; 
     } 

    } 

引起那麼我做錯造成應用程序崩潰?

如果您需要任何進一步的信息,我錯過指出,請留下評論,我會添加它們。

謝謝你的幫助!

+0

嘿,只要嘗試把super.onSaveInstanceState(outState);作爲onSaveInstanceState的第一行。並在onRestoreInstanceState執行檢查,如果(savedInstanceState!= null){//做必需的東西} – SKK

+0

@Santhosh我曾嘗試過這個,應用程序仍然崩潰。即使如此,我將使用'if(savedInstanceState!= null){//做必要的事情}'部分。但據我現在,甚至沒有得到恢復部分:(但謝謝你:) –

回答

1

什麼類型的對象是ID爲「R.id.request_detail1」一個看法?在onSaveInstanceState中,您將它轉換爲EditText,但在onRestoreInstanceState中,您將它轉換爲TextView。

如果它是一個TextView,然後第一投將導致異常。

+0

哦,這讓我感到非常愚蠢......謝謝。現在應用程序不再崩潰了。但它仍然行不通...?任何暗示? (即使它超出了實際問題的範圍) –

+0

什麼不起作用?請更具體一些。 –

+0

哦,是的確定:如果我回到活動,request_detail和poRequest字段是空的。沒有錯誤發生。當我導航到兒童活動時,我在這些領域獲得了信息。當我回到此活動時,信息不會顯示。 –