アクティビティを終了すると、アプリがクラッシュするのはなぜですか? より具体的に言うと、実装するまではすべて正常に機能し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;
}
}
では、アプリをクラッシュさせるために何が間違っていたのでしょうか?
指摘し忘れた情報がさらに必要な場合は、コメントを残してください。追加します。
ご協力ありがとうございました!