0

I'm trying to receive json from an URL. After I got the object I'll parse it and load it on a listview in my App. I'm using Adapter to create the list view and running my jsonTask on my mainActivity then pass the result to adapter. It always throws null pointer exception. Is there a better way to do that? I'm new to android and json task runs as async how can I make my UI load to wait for the result of jsonTask.Here is my code;

Main Activity;

public class TvActivity extends Activity {
    ListView lv;
    ArrayList<TV> tvList;

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;
        new JSONTask().execute();
        lv = (ListView) findViewById(R.id.listView);
        lv.setAdapter(new Adapter(this, tvList));

    }
}

public class JSONTask extends AsyncTask<String, String, JSONArray> {
        ProgressDialog pDialog;

        @Override
        protected JSONArray doInBackground(String... params) {
            return Utility.getJSONArray(Constants.TV_JSON_URL);
        }

        @Override
        protected void onPostExecute(JSONArray result) {
            pDialog.dismiss();

            for (int i = 0; i < result.length(); i++) {
                try {
                    JSONObject row = result.getJSONObject(i);
                    String title = row.getString(Constants.TV_TITLE);
                    String code = row.getString(Constants.TV_CODE);
                    String image_url = row.getString(Constants.TV_IMAGE_URL);
                    tvList.add(new TV(title, image_url, code));

                } catch (JSONException e) {
                    e.printStackTrace();

                }

            }
        }
    }

My Adapter;

public class TvAdapter extends BaseAdapter {

    String[] result;
    Context context;
    ArrayList<TV> tvArrayList;
    int[] imageId;
    private static LayoutInflater inflater = null;

    public TvAdapter(TvActivity mainActivity, ArrayList<TV> tvList) {
        // TODO Auto-generated constructor stub
        //result = prgmNameList;
        context = mainActivity;
        tvArrayList = tvList;
        //imageId = prgmImages;
        inflater = (LayoutInflater) context.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return tvArrayList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return tvArrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public class Holder {
        TextView tv;
        ImageView img;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        TV tvItem = (TV) getItem(position);

        Holder holder = new Holder();
        View rowView;
        rowView = inflater.inflate(R.layout.tv_list, null);
        holder.tv = (TextView) rowView.findViewById(R.id.textView1);
        holder.img = (ImageView) rowView.findViewById(R.id.imageView1);
        holder.tv.setText(tvItem.getTitle());
       // holder.img.setImageResource(imageId[position]);
        rowView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(context, "You Clicked " + tvItem[position], Toast.LENGTH_LONG).show();
            }
        });
        return rowView;
    }

}
2
  • 1
    It is difficult to know your problem without the stacktrace Commented Jul 29, 2016 at 19:27
  • It is solved. Thanks for every comment&answer. Commented Jul 29, 2016 at 19:49

3 Answers 3

1

There is no problem with the AsyncTask. Initialize this ArrayList<TV> tvList before using it here lv.setAdapter(new Adapter(this, tvList))

tvList = new ArrayList<>();
lv.setAdapter(new Adapter(this, tvList));
Sign up to request clarification or add additional context in comments.

6 Comments

I initialized value but I need to fill it with the result of async task. Now it throws error because of progress.dialog
@FatihAktepe Well... ProgressDialog pDialog; ... It is null!
You again didn't initialize the progress dialog also. :)
I initialized everything. I get my data from url but view doesn't refresh itself even though I call notifyDataSetChanged().
I've been using JS for a long time. It is challenging to change your development language to Java. Thanks for answer, it also helped.
|
0

You may try like below:

new JSONTask().execute();
lv = (ListView) findViewById(R.id.listView);
tvList = new ArrayList<TV>();
lv.setAdapter(new Adapter(this, tvList));

Then

@Override
protected void onPreExecute() {
    super.onPreExecute();
    pDialog = new ProgressDialog(context);
    pDialog.setCancelable(true);
    pDialog.setMessage("Loading...");
    pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    pDialog.setProgress(0);
    pDialog.show();
}

add adapter.notifyDataSetChanged(); below of your onPostExecute method

1 Comment

Welcome. Happy Coding :)
0

The app is crashing as tvList is null. Initialize it before passing it to the Adapter constructor. Also notify the adapter to refresh itself once the Async task returns values, using notifyDataSetChanged that can be called on the adapter. eg. adapter.notifyDataSetChanged().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.