我无法从onPostExecute价值传递到另一个活动?(I can't pass valu

2019-10-20 16:19发布

我有娄代码,我需要的值传递onPostExecute到另一个intent ,但它让我crash 。 下面是Logins.class ,我需要的值传递onPostExecuteMainActivity.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)

Answer 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
}
}


Answer 2:

去掉

Bundle bundle = getIntent().getExtras();

onClick方法和使用它onCreate方法

bundle = getIntent().getExtras();

由于getIntent()是活动的情况下没有按钮的onClick的一部分。 而在你的MainActivity为使用捆绑作为一个全球性

Bundle bundle = null;


Answer 3:

为什么进不去的意图额外onCreate()与尝试捕捉block..something这样的:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
try{
    String uId = getIntent().getStringExtra("EXTRA_SESSION_ID"); 

}catch(Exception e){
    e.printStachStrace();
}


文章来源: I can't pass value from onPostExecute to another activity?