从一个Activity传递到另一个Activity的值在Android中以Null的形式到达

时间:2014-11-11 05:04:45

标签: android android-activity

通过这段代码,我试图将一个Activity的值发送到另一个Activity。但是我在另一个Actvity中得到一个空值。在doinbackground方法中,我得到了Status和Reason的值但在onPostexecute中我没有得到值。知道为什么吗?

public class Serchphonenumber extends Activity implements OnClickListener {
    EditText phonenumber;
    ImageView backbutton;
    Button search;
    String _url;
    String Username;
    String Password;
    String Circlearea;
    String Phonenumber;
    JSONfunctions _jparser = new JSONfunctions();
    ProgressDialog pDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.act_serchphonenumber);
        backbutton = (ImageView) findViewById(R.id.searchstatus_imgBack);
        search = (Button) findViewById(R.id.searchstatus_btnSearch);
        phonenumber = (EditText) findViewById(R.id.searchstatus_editMobile);
        Intent intent = getIntent();
        Username = intent.getStringExtra("Username");
        Password = intent.getStringExtra("Password");
        Circlearea = intent.getStringExtra("Circlearea");
        search.setOnClickListener(this);
        backbutton.setOnClickListener(this);
        search.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        if (v.getId() == R.id.searchstatus_btnSearch) {

            if (phonenumber.toString().length() == 0) {
                Toast.makeText(getApplicationContext(),
                        "Entere Your Phone Number", 10000).show();
            }

            new a().execute();

        }

        if (v.getId() == R.id.imageView1) {
            {
                finish();
            }

        }
    }

    class a extends AsyncTask<String, String, String> {
        public String Status;
        public String Reason;

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();

            pDialog = new ProgressDialog(Serchphonenumber.this);
            pDialog.setTitle("Serching");
            pDialog.setMessage("Please wait...");
            pDialog.setCanceledOnTouchOutside(false);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub

            Phonenumber = phonenumber.getText().toString().trim();

            _url = "http://182.71.212.107:8080/api/values/status?userId="
                    + Username + "&" + "password=" + Password + "&" + "circle="
                    + Circlearea + "&" + "mobile=" + Phonenumber;
            Log.d("value,************", _url);

            //
            try {

                String json = HttpHitter.ExecuteData(_url);
                JSONArray _jarray = new JSONArray(json);
                // _jparser.getJSONfromURL(_url);
                JSONObject _jobject = _jarray.getJSONObject(0);

                Status = _jobject.getString("Status");
                Reason = _jobject.getString("Reason");
                Log.e("Response+++", Status);
                Log.e("Reason+++", Reason);

                // JSONObject jsn = _jobject.getJSONObject("responseData");
                //

                // runOnUiThread(afterParsing);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            pDialog.dismiss();

            //
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);

            if (Status.equalsIgnoreCase("ACCEPTED")) {
                Intent intent = new Intent(Serchphonenumber.this,
                        Serchresult.class);

                intent.putExtra("Status", Status);
                intent.putExtra("Reason", Reason);
                startActivity(intent);
            }

            else {
                Toast.makeText(getApplicationContext(), "Worng", 1000).show();
            }
        }

    }

}

2 个答案:

答案 0 :(得分:0)

您可以使用Intent,它是在intent中的活动之间发送的消息,您可以将排序数据放置为String,int等。

活动1中的

Intent intent = new Intent(activity1.this, activity2.class);
intent.putExtra("message", message);
startActivity(intent);

<强>活性2

Intent intent = getIntent();
String message = bundle.getString("message");

答案 1 :(得分:0)

onCreate() methed on activity class

中使用此功能
String Status= getIntent().getExtras().getString("Status");
String Reason= getIntent().getExtras().getString("Reason");