-2

when I run my emulator it just force close here is the logcat shows......

07-16 03:12:30.536: D/AndroidRuntime(386): Shutting down VM
07-16 03:12:30.536: W/dalvikvm(386): threadid=1: thread exiting with uncaught exception             (group=0x4001d800)
07-16 03:12:30.556: E/AndroidRuntime(386): FATAL EXCEPTION: main
07-16 03:12:30.556: E/AndroidRuntime(386): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.database_demo/com.database_demo.Database_demo}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
07-16 03:12:30.556: E/AndroidRuntime(386):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-16 03:12:30.556: E/AndroidRuntime(386):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-16 03:12:30.556: E/AndroidRuntime(386):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-16 03:12:30.556: E/AndroidRuntime(386):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-16 03:12:30.556: E/AndroidRuntime(386):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-16 03:12:30.556: E/AndroidRuntime(386):  at android.os.Looper.loop(Looper.java:123)
07-16 03:12:30.556: E/AndroidRuntime(386):  at android.app.ActivityThread.main(ActivityThread.java:4627)
07-16 03:12:30.556: E/AndroidRuntime(386):  at java.lang.reflect.Method.invokeNative(Native Method)
07-16 03:12:30.556: E/AndroidRuntime(386):  at java.lang.reflect.Method.invoke(Method.java:521)
07-16 03:12:30.556: E/AndroidRuntime(386):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-16 03:12:30.556: E/AndroidRuntime(386):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-16 03:12:30.556: E/AndroidRuntime(386):  at dalvik.system.NativeStart.main(Native Method)
07-16 03:12:30.556: E/AndroidRuntime(386): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
07-16 03:12:30.556: E/AndroidRuntime(386):  at android.app.ListActivity.onContentChanged(ListActivity.java:245)
07-16 03:12:30.556: E/AndroidRuntime(386):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:201)
07-16 03:12:30.556: E/AndroidRuntime(386):  at android.app.Activity.setContentView(Activity.java:1647)
07-16 03:12:30.556: E/AndroidRuntime(386):  at com.database_demo.Database_demo.onCreate(Database_demo.java:40)
07-16 03:12:30.556: E/AndroidRuntime(386):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-16 03:12:30.556: E/AndroidRuntime(386):  at         android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-16 03:12:30.556: E/AndroidRuntime(386):  ... 11 more

my java code

package com.database_demo;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
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.app.ListActivity;
import android.net.ParseException;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class Database_demo extends ListActivity {
ListView list;
List<String> items = new ArrayList<String>();
String result = null;
InputStream is = null;;
StringBuilder sb = null;
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    list = (ListView)findViewById(R.id.listView1);



    items.add("Employ");

    try{

    //http post
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://192.168.0.2/ListView/wa.php");
    List<NameValuePair> nameValue=new ArrayList<NameValuePair>();
    httppost.setEntity(new UrlEncodedFormEntity(nameValue));
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();
    is = entity.getContent();
    }
    catch(Exception e){
        Toast.makeText(getBaseContext(),e.toString() ,Toast.LENGTH_LONG).show();
   }

    //Convert response to string  
    try
    {
      BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));

      sb = new StringBuilder();

      String line = null;

      while ((line = reader.readLine()) != null) 
      {
         sb.append(line + "\n");
      }

      is.close();

      result = sb.toString();
    }
    catch(Exception e)
    {
        Toast.makeText(getBaseContext(),e.toString() ,Toast.LENGTH_LONG).show();
    }
    //END Convert response to string  
    String Cat;
    try{
            JSONArray jArray = new JSONArray(result);
            JSONObject json_data=null;
            for(int i=0;i<jArray.length();i++)
            {
               json_data = jArray.getJSONObject(i);
               Cat=json_data.getString("category");
               items.add("Category: " + Cat);
           }
        setupList();
        }
        catch(JSONException e1){
            Toast.makeText(getBaseContext(),e1.toString() ,Toast.LENGTH_LONG).show();
        } catch (ParseException e1) {
            Toast.makeText(getBaseContext(),e1.toString() ,Toast.LENGTH_LONG).show();
    }

}
private void setupList(){
    list.setAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, items));
}
}

can you help me with this?

Simon
  • 14,407
  • 8
  • 46
  • 61
Ørly
  • 65
  • 7
  • inside `R.layout.main` you have to declara a ListView with id `@android:id/list` – Blackbelt Jul 15 '13 at 12:19
  • tanx it helped me but I didnt see the datas yet here is the logcat says `07-16 03:35:33.409: I/global(335): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.` – Ørly Jul 15 '13 at 12:34

4 Answers4

1

Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

<ListView
 android:id="@android:id/list" // must be there is xml

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

ListActivity has a default layout that consists of a single, full-screen list in the center of the screen. However, if you desire, you can customize the screen layout by setting your own view layout with setContentView() in onCreate(). To do this, your own view MUST contain a ListView object with the id "@android:id/list" (or list if it's in code)

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • tanx it helped me but I didnt see the datas yet here is the logcat says `07-16 03:35:33.409: I/global(335): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.` – Ørly Jul 15 '13 at 12:33
  • @Ørly there seems to lot of mistakes. You are doing network related operation on the ui thread whihc is not possible post honeycomb. You should use a thread or asynctask – Raghunandan Jul 15 '13 at 13:07
  • it's difficult to say everything at once. post a new question as and when you find it difficult. – Raghunandan Jul 15 '13 at 13:11
  • I change the line from: `BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));` to this: `BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"), 8192);` It remove the meessage but another came out from logcat `07-16 04:22:47.098: I/AndroidRuntime(495): NOTE: attach of thread 'Binder Thread #3' failed` – Ørly Jul 15 '13 at 13:27
  • @Ørly post a new question on stackoverflow there are many mistakes which can't be addressed in the comments section. – Raghunandan Jul 15 '13 at 13:28
0

You are extending ListActivity. That means that layout main.xml must contain listview with android:id="@android:id/list"

Tomislav
  • 3,181
  • 17
  • 20
  • tanx it helped me but I didnt see the datas yet here is the logcat says `07-16 03:35:33.409: I/global(335): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.` – Ørly Jul 15 '13 at 12:33
0

You need to use the xml of list view

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

Since you are using ListActivity your xml file must specify the keyword android while mentioning to a ID.

If you need a custom ListView then instead of Extending a ListActivity, you have to simply extend an Activity and should have the same id without the keyword android.

Sunil Kumar
  • 7,086
  • 4
  • 32
  • 50
  • tanx it helped me but I didnt see the datas yet here is the logcat says `07-16 03:35:33.409: I/global(335): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.` – Ørly Jul 15 '13 at 12:33
0

Change this line:

list = (ListView)findViewById(R.id.listView1);

to

list = getListView(); 

//or write

ListView list = (ListView)findViewById(android.R.id.list); 

and in your layout XML for the listView id use

android:id="@android:id/list"
SKK
  • 5,261
  • 3
  • 27
  • 39
  • tanx it helped me but I didnt see the datas yet here is the logcat says `07-16 03:35:33.409: I/global(335): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required. ` – Ørly Jul 15 '13 at 12:31