Question

So I am building an app that just performs AsyncTask to grab images from a website, and then I am trying to use JSON to grab text. So far I have 2 successful tutorials completed both working apps. I am now trying to merge the two so I get both. I got an error and I think it's an easy fix, but I am not quite sure how to resolve. It's messing with the "R." and I remember I was told not to mess with that.

MainActivity.java

package com.jms.loadimagewithasynctask;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends ListActivity {

 /* JSON TEXT BELOW - i also changed (from) extends Activity*/
private Context context;
private static String url = "http://docs.blackberry.com/sampledata.json";

private static final String TAG_VTYPE = "vehicleType";
private static final String TAG_VCOLOR = "vehicleColor";
private static final String TAG_FUEL = "fuel";
private static final String TAG_TREAD = "treadType";
private static final String TAG_OPERATOR = "approvedOperators";
private static final String TAG_NAME = "name";
private static final String TAG_POINTS = "experiencePoints";

ArrayList<HashMap<String, String>> jsonlist = new ArrayList<HashMap<String, String>>();

ListView lv ;
 /* JSON TEXT Above*/
private String[] imageURLArray = new String[]{
        "http://iam.colum.edu/students/jordan.max/poker.png",
        "http://farm4.staticflickr.com/3777/9049174610_bf51be8a07_s.jpg",
        "http://farm8.staticflickr.com/7324/9046946887_d96a28376c_s.jpg",
        "http://farm3.staticflickr.com/2828/9046946983_923887b17d_s.jpg",
        "http://farm4.staticflickr.com/3810/9046947167_3a51fffa0b_s.jpg",
        "http://farm4.staticflickr.com/3773/9049175264_b0ea30fa75_s.jpg",
        "http://farm4.staticflickr.com/3781/9046945893_f27db35c7e_s.jpg",
        "http://farm6.staticflickr.com/5344/9049177018_4621cb63db_s.jpg",
        "http://farm8.staticflickr.com/7307/9046947621_67e0394f7b_s.jpg",
        "http://farm6.staticflickr.com/5457/9046948185_3be564ac10_s.jpg",
        "http://farm4.staticflickr.com/3752/9046946459_a41fbfe614_s.jpg",
        "http://farm8.staticflickr.com/7403/9046946715_85f13b91e5_s.jpg",
        "http://farm8.staticflickr.com/7315/9046944633_881f24c4fa_s.jpg",
        "http://farm4.staticflickr.com/3777/9049174610_bf51be8a07_s.jpg",
        "http://farm8.staticflickr.com/7324/9046946887_d96a28376c_s.jpg",
        "http://farm3.staticflickr.com/2828/9046946983_923887b17d_s.jpg",
        "http://farm4.staticflickr.com/3810/9046947167_3a51fffa0b_s.jpg",
        "http://farm4.staticflickr.com/3773/9049175264_b0ea30fa75_s.jpg",
        "http://farm4.staticflickr.com/3781/9046945893_f27db35c7e_s.jpg",
        "http://farm6.staticflickr.com/5344/9049177018_4621cb63db_s.jpg",
        "http://farm8.staticflickr.com/7307/9046947621_67e0394f7b_s.jpg",
        "http://farm6.staticflickr.com/5457/9046948185_3be564ac10_s.jpg",
        "http://farm4.staticflickr.com/3752/9046946459_a41fbfe614_s.jpg",
        "http://farm8.staticflickr.com/7403/9046946715_85f13b91e5_s.jpg"};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ListView listView = (ListView)this.findViewById(R.id.listView);
    ImageAdapter imageAdapter = new ImageAdapter(this, R.layout.imageitem, imageURLArray);
    listView.setAdapter(imageAdapter);

    new ProgressTask(MainActivity.this).execute(); //JSON TEXT//JSON TEXT//JSON TEXT//JSON TEXT
}
/* JSON CLASSES BELOW */
private class ProgressTask extends AsyncTask<String, Void, Boolean> {
    private ProgressDialog dialog;

    private ListActivity activity;

    // private List<Message> messages;
    public ProgressTask(ListActivity activity) {
        this.activity = activity;
        context = activity;
        dialog = new ProgressDialog(context);
    }

    /** progress dialog to show user that the backup is processing. */

    /** application context. */
    private Context context;

    protected void onPreExecute() {
        this.dialog.setMessage("Progress start");
        this.dialog.show();
    }

    @Override
    protected void onPostExecute(final Boolean success) {
        if (dialog.isShowing()) {
            dialog.dismiss();
        }
        ListAdapter adapter = new SimpleAdapter(context, jsonlist,
                R.layout.list_item, new String[] { TAG_VTYPE, TAG_VCOLOR,
                        TAG_FUEL, TAG_TREAD }, new int[] {
                        R.id.vehicleType, R.id.vehicleColor, R.id.fuel,
                        R.id.treadType });

        setListAdapter(adapter);

        // selecting single ListView item
         lv = getListView();






    }

    protected Boolean doInBackground(final String... args) {

        JSONParser jParser = new JSONParser();

        // getting JSON string from URL
        JSONArray json = jParser.getJSONFromUrl(url);

        for (int i = 0; i < json.length(); i++) {

            try {
                JSONObject c = json.getJSONObject(i);
                String vtype = c.getString(TAG_VTYPE);

                String vcolor = c.getString(TAG_VCOLOR);
                String vfuel = c.getString(TAG_FUEL);
                String vtread = c.getString(TAG_TREAD);

                HashMap<String, String> map = new HashMap<String, String>();

                // adding each child node to HashMap key => value
                map.put(TAG_VTYPE, vtype);
                map.put(TAG_VCOLOR, vcolor);
                map.put(TAG_FUEL, vfuel);
                map.put(TAG_TREAD, vtread);
                jsonlist.add(map);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        return null;

    }

}
/* JSON CLASSES ABOVE */
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

JSONParser.java

package com.jms.loadimagewithasynctask;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
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.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class JSONParser {

static InputStream is = null;
static JSONArray jarray = null;
static String json = "";

// constructor
public JSONParser() {

}

public JSONArray getJSONFromUrl(String url) {

       StringBuilder builder = new StringBuilder();
        HttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        try {
          HttpResponse response = client.execute(httpGet);
          StatusLine statusLine = response.getStatusLine();
          int statusCode = statusLine.getStatusCode();
          if (statusCode == 200) {
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
            String line;
            while ((line = reader.readLine()) != null) {
              builder.append(line);
            }
          } else {
            Log.e("==>", "Failed to download file");
          }
        } catch (ClientProtocolException e) {
          e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        }

    // try parse the string to a JSON object
    try {
        jarray = new JSONArray( builder.toString());
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jarray;

}
}

(I do have an ImageAdaptor class as well, but obviously I doubt that has anything to do with my problem seeing as it was working before I put my JSON stuff in there.)

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">  
<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <!-- Name Label -->
    <TextView
        android:id="@+id/vehicleType"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#43bd00"
        android:textSize="16sp"
        android:textStyle="bold"
        android:paddingTop="6dip"
        android:paddingBottom="2dip" />
    <!-- Description label -->
    <TextView
        android:id="@+id/vehicleColor"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#acacac"
        android:paddingBottom="2dip">
    </TextView>

    <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <!-- Cost Label -->
    <TextView
          android:id="@+id/fuel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#5d5d5d"
        android:gravity="left"
        android:textStyle="bold"
        android:text="Mobile: " >
    </TextView>
    <!-- Price Label -->
    <TextView
        android:id="@+id/treadType"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#acacac" 
        android:textStyle="bold"
        android:gravity="left">
    </TextView>
    </LinearLayout>
</LinearLayout>

</LinearLayout>

main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<ListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />
</RelativeLayout>

(Again, I do have another .xml file for my Async stuff but that has nothing to do with my error, I will post it if need be though.)

And my error:

FATAL EXCEPTION: main
java.lang.RunTimeException: Unable to start activity
ComponentInfo(com.jms.loadimagewithasynctask/com.jms.loadimagewithasynctask.MainActivity}:     java.lang.RuntimeException: Your content must have a List View whose id attribute is android.R.id.list

UPDATED Errors

02-23 22:54:55.032: E/AndroidRuntime(534): FATAL EXCEPTION: main
02-23 22:54:55.032: E/AndroidRuntime(534): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jms.loadimagewithasynctask/com.jms.loadimagewithasynctask.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
02-23 22:54:55.032: E/AndroidRuntime(534):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-23 22:54:55.032: E/AndroidRuntime(534):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-23 22:54:55.032: E/AndroidRuntime(534):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-23 22:54:55.032: E/AndroidRuntime(534):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-23 22:54:55.032: E/AndroidRuntime(534):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-23 22:54:55.032: E/AndroidRuntime(534):  at android.os.Looper.loop(Looper.java:123)
02-23 22:54:55.032: E/AndroidRuntime(534):  at android.app.ActivityThread.main(ActivityThread.java:3683)
02-23 22:54:55.032: E/AndroidRuntime(534):  at java.lang.reflect.Method.invokeNative(Native Method)
02-23 22:54:55.032: E/AndroidRuntime(534):  at java.lang.reflect.Method.invoke(Method.java:507)
02-23 22:54:55.032: E/AndroidRuntime(534):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-23 22:54:55.032: E/AndroidRuntime(534):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-23 22:54:55.032: E/AndroidRuntime(534):  at dalvik.system.NativeStart.main(Native Method)
02-23 22:54:55.032: E/AndroidRuntime(534): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
02-23 22:54:55.032: E/AndroidRuntime(534):  at android.app.ListActivity.onContentChanged(ListActivity.java:243)
02-23 22:54:55.032: E/AndroidRuntime(534):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:210)
02-23 22:54:55.032: E/AndroidRuntime(534):  at android.app.Activity.setContentView(Activity.java:1657)
02-23 22:54:55.032: E/AndroidRuntime(534):  at com.jms.loadimagewithasynctask.MainActivity.onCreate(MainActivity.java:67)
02-23 22:54:55.032: E/AndroidRuntime(534):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-23 22:54:55.032: E/AndroidRuntime(534):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-23 22:54:55.032: E/AndroidRuntime(534):  ... 11 more
Was it helpful?

Solution

Change

android:id="@+id/listView"

to

android:id="@android:id/list"

because you are extending ListActivity so you must have ListView id @android:id/list

OTHER TIPS

You must have

 <ListView android:id="@android:id/list"

In your main.xml

ListActivity by default consists of a single, full-screen list in the center of the screen.

So if you have setContentView(R.layout.main); then you need to have a listview with the id as mentioned above.

http://developer.android.com/reference/android/app/ListActivity.html

you need to change your listview

from this

  <ListView
  android:id="@+id/listView"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  />

to this....

<ListView
  android:id="@android:id/list"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  />

because you are extending listactivity...

As suggested by other, there is a problem with your listview implementation.

Why don't you check out Exhaustive ListView with simple examples

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top