Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8268003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:43:49+00:00 2026-06-08T05:43:49+00:00

Need your help!!! I have activity, where I get some data in JSON format

  • 0

Need your help!!!
I have activity, where I get some data in JSON format and put it ListView. Everything works fine. But I want to define what item was clicked in list with Setonitemclicklistener. I tried to use it in onPostExecuted() method of my inner Connection class,which extends AsyncTask, but I’ve got NullPoinerException

public class ConnectionActivity extends ListActivity{
JsonParser jsonParser =  new JsonParser(); 
private ListView listView;
JSONArray inbox = null;
  private ProgressDialog pDialog;
private static final String TAG_ID = "ID";
private static final String TAG_NAME = "Date";
private static final String TAG_DATE = "Name";
private static final String TAG_PRICE = "Price";
ArrayList<HashMap<String,String>> resultList;

public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);

 resultList = new ArrayList<HashMap<String,String>> ();
 new Connection().execute();
 new ListHandler().execute();

}
class Connection extends AsyncTask<String,String,String>
{

    @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(ConnectionActivity.this);
            pDialog.setMessage("Не базарь и жди пока загрузиться!!!!");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

    @Override
    protected String doInBackground(String... args) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();

        // getting JSON string from URL
        String json = jsonParser.makeHttpRequest();
        Log.d("Outbox JSON",json.toString());

        try{

            JSONArray jArray = new JSONArray(json);
            for (int i=0;i<jArray.length();i++) {
                    JSONObject c = jArray.getJSONObject(i);


                 // Storing each json item in variable
                    String id = c.getString(TAG_ID);
                    String name= c.getString(TAG_NAME);
                    String date = c.getString(TAG_DATE);
                    String price = c.getString(TAG_PRICE);

                    // creating new HashMap
                    HashMap<String, String> hashmap = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    hashmap.put(TAG_ID, id);
                    hashmap.put(TAG_NAME, name);
                    hashmap.put(TAG_DATE, date);
                    hashmap.put(TAG_PRICE, price);

                    // adding HashList to ArrayList
                    resultList.add(hashmap);

            }

        }
        catch(JSONException e){
            Log.e("log_tag", "Error parsing data "+e.toString());
        }
        return null;
    }
     protected void onPostExecute(String file_url) {
            // dismiss the dialog after getting all products
            pDialog.dismiss();
            // updating UI from Background Thread
            runOnUiThread(new Runnable() {
                public void run() {
                    /**
                     * Updating parsed JSON data into ListView
                     * */
                    ListAdapter adapter = new SimpleAdapter(
                            ConnectionActivity.this, resultList,
                            R.layout.connect_item, new String[] { TAG_ID, TAG_NAME, TAG_DATE, TAG_PRICE },
                            new int[] { R.id.order_id, R.id.order_date, R.id.order_name, R.id.order_price });
                    // updating listview
                    setListAdapter(adapter);
                    listView =  (ListView) findViewById(R.id.list);
                    listView.setOnItemClickListener(new ListClickListener());
                }
            });

        }
}

I think that problem is with using of one thread, so I created one more inner class in my ConnectionActivity like this^

 class ListHandler extends AsyncTask<Void,Void,Void>
     {
        protected void onPreExecute ()
         {


         }
        @Override
        protected Void  doInBackground(Void... arg0) {
            // TODO Auto-generated method stub
            return null;
        }

        protected void onPostExecute()
        {
            listView =  (ListView) findViewById(R.id.list);

            listView.setOnItemClickListener(new OnItemClickListener() {

           public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {
               Log.d("Click on the item","!!!!!!!!!!!!!!!!!!!!!");
               Toast toast = Toast.makeText(getApplicationContext(), 
                       "Пора покормить кота!", Toast.LENGTH_SHORT); 
                    toast.show(); 
           } 
        });


        }


     }

But it doesn’t work. I mean that I cannot get the Toast when I clicked on the item , but there’s no exception. I tried to handle setonitemclicklistener in void onResume() method of the Activity, but I’ve got NullPointerException. Also I handled this in OnPreExecute() method of the ListHandler class – same result…
Help please with it…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T05:43:50+00:00Added an answer on June 8, 2026 at 5:43 am

    where is setContentView(R.layout.yourxml) ? i think you forgot it in oncreate Method.

    So your listView is null and gives NPE.

    AND

    onPostExecute method have runonUiThread,Remove it no need it.Its already run in UI Thread.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some help here. Basically, I have an Activity. This uses the startActivityForResult()
I need your help. I have one Activity with two fragments: one fragment with
I need your help. I have more than 40000 proteins in fasta file format.
I need your help. Say I have a code inside a for statement similar
I need your help. I have a php script to send email to people
Hi i have a requirement i need your help for it:- I have a
I have one question about Yii framework, and I need your help. Basicly, I
Thanks in advance for your help. I have a need within an application to
Need your help to get the max of CAP_PRICE based on certain criteria in
I need your help in JAVA (with some sample code if possible) regarding to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.