0

I have been coding for JSON parsing all things are right but in logcat following error is shown

08-27 10:25:49.628: E/AndroidRuntime(816): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mokshya.hosprsing/com.mokshya.hosprsing.HomeActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

following is my main code

package com.mokshya.hosprsing;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

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

import android.os.Bundle;
import android.app.ListActivity;
import android.view.Menu;
import android.widget.ArrayAdapter;

public class HomeActivity extends ListActivity {


@SuppressWarnings({"rawtypes","unchecked"})
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    setListAdapter(new ArrayAdapter (this,     android.R.layout.simple_list_item_1,this.fetchHosTime()));
}
public ArrayList<String> fetchHosTime(){
    ArrayList<String> listitems=new ArrayList<String>();
    try {
        URL hos=new URL("http://ldsclient.com/ftp/strtojson.php");
        URLConnection tc=hos.openConnection();
        BufferedReader in=new BufferedReader(new     InputStreamReader(tc.getInputStream()));
        String line;
        while((line=in.readLine())!=null) {
            JSONArray ja=new JSONArray(line);
                for(int i=0; i<ja.length();i++) {
                    JSONObject jo=(JSONObject) ja.get(i);
                    listitems.add(jo.getString("text"));

                }
        }
    }
    catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return listitems;
}


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
 }

below is my main.xml code

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

</RelativeLayout>
surhidamatya
  • 2,419
  • 32
  • 56

1 Answers1

1

Place this ListView in your main.xml

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

See this existing answer. And, have a look at ListActivity tutorial for more help

Community
  • 1
  • 1
Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
  • it worked but following error came 08-27 10:49:08.328: E/AndroidRuntime(1051): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mokshya.hosprsing/com.mokshya.hosprsing.HomeActivity}: android.os.NetworkOnMainThreadException 08-27 10:49:08.328: E/AndroidRuntime(1051): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955) – surhidamatya Aug 27 '12 at 05:07
  • @sur007 For this, you've to use `AsyncTask` for getting details from JSon, like in [this answer](http://stackoverflow.com/q/8612406/940096) – Praveenkumar Aug 27 '12 at 05:10
  • where should i use this AsyncTask confusion – surhidamatya Aug 27 '12 at 06:11
  • @sur007 See some examples of `AsyncTask` for getting data from Json into ListView. 1) [AsyncTask with ListView](http://stackoverflow.com/questions/5987003/populating-a-listview-w-asynctask) 2) [populate ListView from JSON](http://www.mysamplecode.com/2012/07/android-populate-listview-from-json.html) – Praveenkumar Aug 27 '12 at 06:21
  • thanx this has been so helpful to me – surhidamatya Aug 27 '12 at 10:16
  • yes i have tried it but there are lots of error and m trying to solve that – surhidamatya Aug 29 '12 at 03:40
  • what i am unable to do is fecth the json data – surhidamatya Aug 29 '12 at 03:41