I’m pretty new to Android programming so any help would be great 🙂
I’m trying to get some dummy data remotely, parse the json response and display it within a listview.
This is the xml layout file i’m trying to use with the list activity:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/list"
/>
</LinearLayout>
This is my http search activity:-
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
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 android.app.ListActivity;
import android.net.ParseException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class searchResultActivity extends ListActivity {
ListView listview;
String result = null;
InputStream is = null;
StringBuilder sb = null;
String url;
String fd_name;
ArrayList<String> listItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
/*setContentView(R.layout.test);
listview = (ListView)findViewById(R.id.list);*/
url = "http://pjchambers.comuf.com/index.php";
new getData().execute();
}
public class getData extends AsyncTask<String, Void, ArrayList<String>> {
protected ArrayList<String> doInBackground(String...urls) {
// TODO Auto-generated method stub
listItems = new ArrayList<String>();
try {
// Set of http statements setting up the connections to the php
// and database
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection" + e.toString());
}
// convert response to string
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, "iso-8859-1"), 8);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line = "0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
// paring data
try {
JSONArray jArray = new JSONArray(result);
JSONObject json_data = null;
for (int i = 0; i < jArray.length(); i++) {
json_data = jArray.getJSONObject(i);
fd_name = json_data.getString("Name");
listItems.add(fd_name);
}
} catch (JSONException e1) {
Toast.makeText(getBaseContext(), "No Results Found",
Toast.LENGTH_LONG).show();
} catch (ParseException e1) {
e1.printStackTrace();
}
Log.d("debug", "- " + listItems.toString());
return listItems;
}
protected void doPostExecute() {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
searchResultActivity.this, android.R.layout.simple_list_item_1, R.id.list, listItems);
setListAdapter(adapter);
}
}
}
I’ve spent hours trying different methods and approaches, using a few different online tutorials but they all end in the same result. As it stands the app works but the displays are displayed; however if i declare the layout in the onCreate methods the app crashes. any help or advice would be must appreciated, a fresh set of eyes might notice the issue(s) straight away
This is the logcat after I uncommented the two statements
05-22 12:12:20.234: D/debug(631): - [The Arena, Empire]
05-22 12:12:42.004: I/dalvikvm(691): threadid=3: reacting to signal 3
05-22 12:12:42.244: I/dalvikvm(691): Wrote stack traces to '/data/anr/traces.txt'
05-22 12:12:42.404: D/gralloc_goldfish(691): Emulator without GPU emulation detected.
05-22 12:12:48.415: I/dalvikvm(691): threadid=3: reacting to signal 3
05-22 12:12:48.424: I/dalvikvm(691): Wrote stack traces to '/data/anr/traces.txt'
05-22 12:12:50.595: D/AndroidRuntime(691): Shutting down VM
05-22 12:12:50.595: W/dalvikvm(691): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
05-22 12:12:50.614: E/AndroidRuntime(691): FATAL EXCEPTION: main
05-22 12:12:50.614: E/AndroidRuntime(691): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.project.barcrawl/com.project.barcrawl.searchResultActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
05-22 12:12:50.614: E/AndroidRuntime(691): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
05-22 12:12:50.614: E/AndroidRuntime(691): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-22 12:12:50.614: E/AndroidRuntime(691): at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-22 12:12:50.614: E/AndroidRuntime(691): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-22 12:12:50.614: E/AndroidRuntime(691): at android.os.Handler.dispatchMessage(Handler.java:99)
05-22 12:12:50.614: E/AndroidRuntime(691): at android.os.Looper.loop(Looper.java:137)
05-22 12:12:50.614: E/AndroidRuntime(691): at android.app.ActivityThread.main(ActivityThread.java:4424)
05-22 12:12:50.614: E/AndroidRuntime(691): at java.lang.reflect.Method.invokeNative(Native Method)
05-22 12:12:50.614: E/AndroidRuntime(691): at java.lang.reflect.Method.invoke(Method.java:511)
05-22 12:12:50.614: E/AndroidRuntime(691): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-22 12:12:50.614: E/AndroidRuntime(691): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-22 12:12:50.614: E/AndroidRuntime(691): at dalvik.system.NativeStart.main(Native Method)
05-22 12:12:50.614: E/AndroidRuntime(691): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
05-22 12:12:50.614: E/AndroidRuntime(691): at android.app.ListActivity.onContentChanged(ListActivity.java:243)
05-22 12:12:50.614: E/AndroidRuntime(691): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:254)
05-22 12:12:50.614: E/AndroidRuntime(691): at android.app.Activity.setContentView(Activity.java:1835)
05-22 12:12:50.614: E/AndroidRuntime(691): at com.project.barcrawl.searchResultActivity.onCreate(searchResultActivity.java:41)
05-22 12:12:50.614: E/AndroidRuntime(691): at android.app.Activity.performCreate(Activity.java:4465)
05-22 12:12:50.614: E/AndroidRuntime(691): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-22 12:12:50.614: E/AndroidRuntime(691): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
05-22 12:12:50.614: E/AndroidRuntime(691): ... 11 more
05-22 12:12:51.004: I/dalvikvm(691): threadid=3: reacting to signal 3
05-22 12:12:51.024: I/dalvikvm(691): Wrote stack traces to '/data/anr/traces.txt'
Your id in the XML should
id="@android:id/list"then it should work. In your code a new id is created as part of “your” R.class which is different fromandroid.R.id.list. Therefore the list view cannot be found and consequently the exception is thrown.Also the third parameter of the
ArrayAdapterconstructor is supposed to be the id of the text resource to fill with the data. You are passingR.id.listwhich is wrong. For the resource defined byandroid.R.id.simple_list_item_1it should beandroid.R.id.text1if I’m not mistaken.