I'm trying to create an Android app and I've only limited experience with Java so this is my first real-world app. What I'm trying to do is create a listview and the end result I am looking for is similar to: +========+ |SCROLLVIEW | +========+ |Listview line 1| |Listview line 2| |Listview line 1| |Listview line 2| |Listview line 1| |Listview line 2| +========+ I'm struggling even getting the listview working though with a nullpointer exception. I've noted it down as I'm a little clueless as to where to go from here so if anyone could help I'd be grateful. package info.androidhive.slidingmenu; import android.app.Fragment; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ListView; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.StatusLine; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class PhotosFragment extends Fragment { public PhotosFragment(){} @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_photos, container, false); new RequestTask().execute("http://www.myjsonurl.co.uk/app/index.php?Type=8&catid=7&userid=4"); return rootView; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); Log.v("created", "onActivityCreated()"); setHasOptionsMenu(true); } private static final String TAG = MainActivity.class.getSimpleName(); private static List<ListViewItem> mItems; // ListView items list JSONObject obj; JSONArray stories; public class RequestTask extends AsyncTask<String, String, String> { @Override protected String doInBackground(String... uri) { HttpClient httpclient = new DefaultHttpClient(); HttpResponse response; String responseString = null; try { response = httpclient.execute(new HttpGet(uri[0])); StatusLine statusLine = response.getStatusLine(); if (statusLine.getStatusCode() == HttpStatus.SC_OK) { ByteArrayOutputStream out = new ByteArrayOutputStream(); response.getEntity().writeTo(out); out.close(); responseString = out.toString(); //Do anything with response.. Log.d(TAG, responseString); } else { //Closes the connection. response.getEntity().getContent().close(); throw new IOException(statusLine.getReasonPhrase()); } } catch (ClientProtocolException e) { //TODO Handle problems.. Log.d(TAG, String.valueOf(e)); } catch (IOException e) { //TODO Handle problems.. Log.d(TAG, String.valueOf(e)); } return responseString; } @Override protected void onPostExecute(String result) { super.onPostExecute(result); Log.e(TAG, result); try { obj = new JSONObject(result); stories = obj.getJSONArray("stories"); // initialize the items list mItems = new ArrayList<ListViewItem>(); for (int i = 0; i < stories.length(); i++) { JSONObject storyObj = stories.getJSONObject(i); if (!storyObj.has("Advert")) { newsStories newsStories = new newsStories(); newsStories.setTitle(storyObj.getString("subject")); newsStories.setBody(storyObj.getString("body")); mItems.add(new ListViewItem(newsStories.getTitle(), newsStories.getBody(), "")); } else { newsStories newsStories = new newsStories(); newsStories.setThumbnailUrl(storyObj.getString("Advert")); mItems.add(new ListViewItem("", "", newsStories.getThumbnailUrl())); } // initialize and set the list adapter //setListAdapter(new ListViewDemoAdapter(getActivity(), mItems)); } } catch (JSONException e) { e.printStackTrace(); } //mProgressBar = (ProgressBar) getView().findViewById(R.id.progressBar); ListView mListView = (ListView) getView().findViewById(R.id.listView2); //Here is the issue //mTvEmpty = (TextView) getView().findViewById(R.id.textView); Log.d("mListView: ", String.valueOf(mListView)); ArrayAdapter<ListViewItem> adapter = new ArrayAdapter<ListViewItem>(getActivity(), android.R.layout.simple_list_item_1, mItems); Log.d("Adapter: ", String.valueOf(adapter)); // load your data to your mListView mListView.setAdapter(adapter); //NULL POINTER } public class newsStories { private String name, thumbnailUrl, body; public String getTitle() { return name; } public void setTitle(String name) { this.name = name; } public String getBody() { return body; } public void setBody(String body) { this.body = body.substring(0, 150); } public String getThumbnailUrl() { return thumbnailUrl; } public void setThumbnailUrl(String thumbnailUrl) { this.thumbnailUrl = thumbnailUrl; } } } public class ListViewItem { public final String title; // the text for the ListView item title public final String description; // the text for the ListView item description public final String adUrl; //public final Drawable image; public ListViewItem(String title, String description, String Advert_Url) { if (Advert_Url != null && !Advert_Url.isEmpty()) { String Advert = "http://www.myurl.co.uk/images/advert.png?width=700&height=200"; this.title = ""; this.description = ""; this.adUrl = Advert; } else { this.title = title; this.description = description; this.adUrl = ""; } } } } Code (markup):
Before using findViewById the element needs to be in Activity (R.id.listView2) in this case. If you google "getView().findViewById" you can find many examples of that. I don't have my Java stack setup so I can't test this out.
The problem is that I'm doing it Async so if I put it in the activity it tries to set the listview before the Async task has finished.