gpt4 book ai didi

android - onSaveInstanceState 使我的应用程序崩溃

转载 作者:行者123 更新时间:2023-12-03 16:53:59 26 4
gpt4 key购买 nike

当我离开我的 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,那么先强制转换会引发异常。

关于android - onSaveInstanceState 使我的应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18009455/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com