我有以下代码,我需要将onPostExecute
的值传递给intent
的另一个,但它会得到crash
。以下Logins.class
我需要将onPostExecute
的值传递给MainActivity.class
:
public class Logins extends Activity {
String NAMESPACE = "http://tempuri.org/";
String METHOD_NAME = "Login";
String SOAP_ACTION = "http://tempuri.org/Login";
String URL = "http://test.com/MobileWebService/Related.asmx";
public String Webresponse = "IS NULL ?";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyLogin task = new MyLogin();
task.execute(new String[] { URL });
}
public String[] test(String user,String pass)
{
String[] stvalue = {user,pass};
new MyLogin().execute(stvalue);
return stvalue;
}
private class MyLogin extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... urls) {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("username", urls[0]);
request.addProperty("pass", urls[1]);
request.addProperty("device", "0");
request.addProperty("security", 128110);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setAddAdornments(false);
envelope.implicitTypes = true;
envelope.setOutputSoapObject(request);
HttpTransportSE conn = new HttpTransportSE(URL);
conn.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
try {
conn.call(SOAP_ACTION, envelope);
SoapPrimitive response = (SoapPrimitive) envelope
.getResponse();
Webresponse = response.toString();
Log.i("LOG", Webresponse);
}
catch (Exception e) {
e.printStackTrace();
Log.i("xxx", e.toString());
}
return Webresponse;
}
@Override
protected void onPostExecute(String result) {
// someMethod(result);
Intent intent = new Intent(Logins.this, MainActivity.class);
intent.putExtra("EXTRA_SESSION_ID", result);
startActivity(intent);
}
}
}
这是我的Activity.class
:
public class MainActivity extends Activity {
EditText UserName;
EditText PassWord;
Button Login;
String mUserName;
String mPassWord;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
UserName = (EditText) findViewById(R.id.edtUserName);
PassWord = (EditText) findViewById(R.id.edtPasswprd);
Login = (Button) findViewById(R.id.btnLogin);
Login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
mUserName = UserName.getText().toString().trim();
mPassWord = PassWord.getText().toString().trim();
if (mUserName.length() > 0 && mPassWord.length() > 0) {
Logins lt = new Logins();
lt.test(mUserName, mPassWord);
---I need get value from here--->Bundle bundle = getIntent().getExtras();---BUT CRASH----
String uId = bundle.getString("EXTRA_SESSION_ID");
Log.i("OK", uId);
} else {
Toast.makeText(
G.context,
G.context.getResources().getString(
R.string.btn_LoginError), Toast.LENGTH_LONG)
.show();
}
}
});
}
}
我的LogCat
是:
AndroidRuntime
at com.test.MainActivity$1.onClick(MainActivity.java:53)
答案 0 :(得分:2)
删除
Bundle bundle = getIntent().getExtras();
来自onClick
方法,并在onCreate
方法中将其用作
bundle = getIntent().getExtras();
因为getIntent()
是Activity的上下文而不是按钮onClick的一部分。并在您的 MainActivity 中将bundle用作全局
Bundle bundle = null;
答案 1 :(得分:2)
为什么不通过try catch块在onCreate()
中获取额外的意图......如下:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try{
String uId = getIntent().getStringExtra("EXTRA_SESSION_ID");
}catch(Exception e){
e.printStachStrace();
}
答案 2 :(得分:1)
您应该使用interface
上的AsyncTask
解决此问题:
的AsyncTask:
public class MyAsyncTask extends AsyncTask<Void, Void, Response> {
public interface GetMyTaskListener {
public void onGetMyTaskComplete(Response response);
}
Request request;
GetMyTaskListener listener;
public MyAsyncTask(Request request,
GetMyTaskListener listener) {
this.request = request;
this.listener = listener;
}
protected void onPostExecute(Response result) {
listener.onGetMyTaskComplete(result);
}
@Override
protected Response doInBackground(Void... params) {
...
return response;
}
}
的活动:
public class MyActivity extends Activity implements GetMyTaskListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
...
Request req = new Request();
new MyAsyncTask(req, this).execute();
}
@Override
public void onGetMyTaskComplete(
Response response) {
//TODO use the information from the AsyncTask
}
}