简体   繁体   中英

calling a method in an android application

I'm trying to call a method in an android application when I press a button but the application crushes when the method is called.

          BtnName.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            get_name_value();
        }
    }); 

public void get_name_value(){

    int success;
    String name = "paul";
    try {
        List<NameValuePair> params1 = new ArrayList<NameValuePair>();
        params1.add(new BasicNameValuePair("name", name));

        // getting User details by making HTTP request
        // Note that User details url will use GET request
        JSONObject json = jsonParser.makeHttpRequest(
                url_product_detials, "GET", params1);

        // check your log for json response
        Log.d("Single User Details", json.toString());

        // json success tag
        success = json.getInt(TAG_SUCCESS);
        if (success == 1) {
            // successfully received User details
            JSONArray UserObj = json
                    .getJSONArray(TAG_PRODUCT); // JSON Array

            // get first User object from JSON Array
            final JSONObject product = UserObj.getJSONObject(0);
            runOnUiThread(new Runnable() {
                public void run() {
                    // User with this pid found
                    // Edit Text
                        txtName = (EditText) findViewById(R.id.accid);
                    // display User data in EditText
                    try {
                        txtName.setText(product.getString(TAG_VALUE));
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            });

        }else{
            // User with pid not found
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
} 

the following are some of the errors showing.

01-04 09:11:59.665: E/AndroidRuntime(579): FATAL EXCEPTION: main
01-04 09:11:59.665: E/AndroidRuntime(579): java.lang.NullPointerException
01-04 09:11:59.665: E/AndroidRuntime(579):  at com.pule.ConCurrency.Concurency_main$5.run(Concurency_main.java:320)
01-04 09:11:59.665: E/AndroidRuntime(579):  at android.app.Activity.runOnUiThread(Activity.java:3717)
01-04 09:11:59.665: E/AndroidRuntime(579):  at com.pule.ConCurrency.Concurency_main.get_name_value(Concurency_main.java:313)

First problem is that you try to do network operation on UI thread. You should put this part:

    // getting User details by making HTTP request
    // Note that User details url will use GET request
    JSONObject json = jsonParser.makeHttpRequest(
            url_product_detials, "GET", params1);

inside AsyncTask.doInBackground, and the later code which updates your UI inside AsyncTask.onPostExecute.

Here is a good tutorial using makeHttpRequest for you:

http://techlovejump.com/android-json-parser-from-url/

as you can see this method is executed from inside of AsyncTask.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM