getextra和putextra无法正常工作

时间:2015-06-11 09:34:12

标签: android json

我想在一个Activity中获取edittext值并将其发送到第二个Activity。在第二个Activity中,此值将添加到JSON URL以获取值。问题是当我在edittext中输入值后点击“继续”按钮而不是转到下一个Activity程序崩溃时。

第二项活动:

public class ServMan extends Activity {

    ArrayList<String> AD_C_List;
    ProgressDialog P_Dialog_TR;
    ArrayAdapter<String> ARR;
    AutoCompleteTextView AV;
    EditText ET;
    String yourname;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //setTheme(android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
        setContentView(R.layout.servman);

        EditText name = (EditText)findViewById(R.id.name);
        EditText mobile = (EditText)findViewById(R.id.mobile);
        EditText email = (EditText)findViewById(R.id.email);

        yourname = getIntent().getExtras().getString("yourname");
       /* Intent i = getIntent();
        yourname = i.getStringExtra("yourname"); */



        /*String URL = "http://103.8.127.248:1002/serv1/Service1.svc/getassignedstaff/" + yourname.toString();


        loading_data(URL);   */

        loading_data((new StringBuilder("http://103.8.127.248:1002/serv1/Service1.svc/getassignedstaff/")).append(yourname).toString());




        }

     private void loading_data(String url) {
        // TODO Auto-generated method stub      


        AD_C_List = new ArrayList<String>();        

        P_Dialog_TR = new ProgressDialog(ServMan.this);
        P_Dialog_TR.setMessage("Fetching Data...");
        P_Dialog_TR.setCancelable(true);
        P_Dialog_TR.show();

        RequestParams params = new RequestParams();

        AsyncHttpClient client = new AsyncHttpClient();

        client.get(url, params, new JsonHttpResponseHandler() { 


            public void onFailure(int statusCode, Throwable e,
                    JSONObject errorResponse) {
                // TODO Auto-generated method stub
                //super.onFailure(statusCode, e, errorResponse);
                //This will called on 4xx HTTP error
                Toast.makeText(getApplicationContext(), "No data", Toast.LENGTH_SHORT).show();

                System.out.println("Status Code ::: "+ statusCode);
                System.out.println("Error Response ::: "+ errorResponse); 
            }


        @Override
        public void onSuccess(int statusCode,
                org.apache.http.Header[] headers, final JSONObject response) {

            // TODO Auto-generated method stub
//          super.onSuccess(statusCode, headers, response);

            System.out.println("Success Response ::: "+response);


            P_Dialog_TR.dismiss();
                         try {


                                JSONObject jsonObject = response.getJSONObject("GetAssignedStaffResult");
                                String e_name = jsonObject.getString("EMAIL");
                                String m_name = jsonObject.getString("MOBILE");     
                                String u_name = jsonObject.getString("UserName");       

                                String final_txt = " Email: "+e_name+"\n Mobile :"+m_name+"\n Username :"+u_name;

                                System.out.println("JSON Object: "+jsonObject);

                                Log.d("Email : ",jsonObject.getString("EMAIL"));
                                Log.i("Mobile :",jsonObject.getString("MOBILE"));
                                Log.i("Username :",jsonObject.getString("UserName"));


                                for (int i = 0; i < AD_C_List.size(); i++) { 

                                String item = AD_C_List.get(i);
                                Log.i("Array List",item);
                                }
                                Toast.makeText(getApplicationContext(), u_name, Toast.LENGTH_SHORT).show();
                                populate_ET(final_txt);


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

                      }              

            });

     }

     public void populate_ET(String C_List) {

                ET.setText(C_List);
    }   

}

第一项活动:

public class BookID extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //setTheme(android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
        setContentView(R.layout.bookid);

       final EditText et = (EditText)findViewById(R.id.editText1);
        Button b = (Button)findViewById(R.id.button1);
        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                 if (et.getText().toString().matches(""))
                    {
                        Toast.makeText(getApplicationContext(), "Please enter Booking ID", Toast.LENGTH_SHORT).show();
                        return;
                    } else
                    {
                        Intent view = new Intent(BookID.this, ServMan.class);
                        view.putExtra("yourname", et.getText().toString());
                        startActivity(view);
                        return;
                    }
            }
        });  
    }   
}

1 个答案:

答案 0 :(得分:1)

你错过了这个

ET = (EditText)findViewById(R.id.your_edit_text_id);

你不能使用这个,这就是你在ET.setText(C_List);这行获得NullPointerException的原因

修改:

您在第二项活动

中错过了此项