Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
填充ListView将抛出android.R.id.list_Android_Json_Android Listview_Populate - Fatal编程技术网

填充ListView将抛出android.R.id.list

填充ListView将抛出android.R.id.list,android,json,android-listview,populate,Android,Json,Android Listview,Populate,我通过JSON从我的服务器获取数据,我试图在ListView中显示我获取的数据,但每次我获取此错误时您的内容必须有一个id属性为“android.R.id.list”的ListView。我从服务器获取的一切都很好,数据设置很好,但是仅仅将其添加到listView是不起作用的。我不知道为什么。我在这里做错什么了吗 填充ListView的代码 public class List extends ListActivity { String myLat = ""; String myL

我通过JSON从我的服务器获取数据,我试图在ListView中显示我获取的数据,但每次我获取此错误时
您的内容必须有一个id属性为“android.R.id.list”的ListView。
我从服务器获取的一切都很好,数据设置很好,但是仅仅将其添加到listView是不起作用的。我不知道为什么。我在这里做错什么了吗

填充ListView的代码

public class List extends ListActivity {

    String myLat = "";
    String myLng = "";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout._list);
        getLocations();
        new GetTask().execute(myLat, myLng);
    }

    private void getLocations() {
        String[] locations = getApplicationContext().fileList();
        FileInputStream fis;

        for (int i = 0; i < locations.length; i++) {
            if (locations[i].equals("my_latitude")) {
                try {
                    fis = openFileInput(locations[i]);
                    byte[] input = new byte[fis.available()];
                    while (fis.read(input) != -1) {
                        myLat += new String(input);
                    }
                    fis.close();
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            if (locations[i].equals("my_longitude")) {
                try {
                    fis = openFileInput(locations[i]);
                    byte[] input = new byte[fis.available()];
                    while (fis.read(input) != -1) {
                        myLng += new String(input);
                    }
                    fis.close();
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

    public void CreateList(Objecter[] objecter) {

         this.setListAdapter(new ArrayAdapter<String>(this,
         R.layout.my_row));
        ArrayList<HashMap<String, String>> List = new ArrayList<HashMap<String, String>>();

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

            HashMap<String, String> map = new HashMap<String, String>();
            map.put("Name", objecter[i].getName());
            map.put("Latitude", Float.toString(objecter[i].getLatitude()));
            map.put("Longitude", Float.toString(objecter[i].getLongitude()));
            map.put("Distance", Double.toString(Math.round(objecter[i]
                    .getDistance() * 1000) / 1000.0));

            List.add(map);
        }

        final ListView list = (ListView)findViewById(R.id.my_list);
        //final ListView list = getListView();
        ListAdapter adapter = new ListAdapter(this, List);
        list.setAdapter(adapter);

        list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                TextView txtLat = (TextView) parent.getChildAt(
                        position - list.getFirstVisiblePosition())
                        .findViewById(R.id.lblListlatitude);
                TextView txtLng = (TextView) parent.getChildAt(
                        position - list.getFirstVisiblePosition())
                        .findViewById(R.id.lblListlongitude);
                String lat = txtLat.getText().toString();
                String lng = txtLng.getText().toString();
                Log.v("Lat ", "Lat of selected item is " + lat);
                Log.v("Lng ", "Lng of selected itemis " + lng);

                Uri uri = Uri.parse("http://maps.google.com/maps?&saddr="
                        + myLat + "," + myLng + "&daddr=" + lat + "," + lng);
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);
                finish();
            };
        });
    }

    class GetTask extends AsyncTask<String, Void, JSONArray> {

        @Override
        protected JSONArray doInBackground(String... data) {
            String lat = data[0];
            String lng = data[1];

            JSONParser jParser = new JSONParser();
            JSONArray json = jParser
                    .getJSONFromUrl("http://www.mysite.eu/index.php/searchJSON?lat="
                            + lat + "&lng=" + lng + "&radius=10");

            return json;
        }

        protected void onPostExecute(JSONArray result) {

            if (result != null) {

                try {
                    Log.v(result.getString(0), result.getString(0));

                } catch (JSONException e) {
                    e.printStackTrace();
                }

//              Toast toast = Toast.makeText(getApplicationContext(), "Done",
//                      Toast.LENGTH_SHORT);
//              toast.show();
//              Log.d("whut", "" + result.length());

                String[] building = new String[result.length()];
                Objecter[] objecter= new Objecter[result.length()];
                try {
                    for (int i = 0; i < result.length(); i++) {
                        JSONObject row;
                        row = result.getJSONObject(i);
                        building[i] = row.getString("Name");
                        building[i] += "" + row.getDouble("Latitude");
                        building[i] += "" + row.getDouble("Longitude");
                        building[i] += ". Distance: "
                                + row.getDouble("distance") + "km";

                        String Name = row.getString("Name");
                        float lat = (float) row.getDouble("Latitude");
                        float lng = (float) row.getDouble("Longitude");
                        float dist = (float) row.getDouble("distance");

                        objecter[i] = new Objecter(Name, lat, lng, dist);
                    }
                } catch (JSONException e) {
                    Log.v("Noooooooooooo", "You were the chosen one");
                    Log.e("Noooooooooooo", "Dis iz die erreur: " + e.toString());
                }

                CreateList(objecter);
            } else {
                Toast toast = Toast.makeText(getApplicationContext(),
                        "Please enable internet", Toast.LENGTH_LONG);
                toast.show();
            }
        }
    }
}

您的
\u list.xml
必须包含id为“@android:id/list”

像这样:

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

根据on
ListActivity

ListActivity有一个默认布局,由位于屏幕中心的单个全屏列表组成。但是,如果需要,可以通过在onCreate()中使用setContentView()设置自己的视图布局来自定义屏幕布局。要做到这一点,您自己的视图必须包含id为“@android:id/list”的ListView对象(如果是代码,则为list)


但是我在
my_list.xml
中有一个
Listview
,带有
id my_list
。这就是我在代码中设置以下行的原因`最终ListView list=(ListView)findViewById(R.id.my_list)`如果您正在扩展ListActivity,则无法设置自己的id。必须与我前面描述的一样。您可以通过调用
getListView()
()获取ListView对象,在那里我应该调用
getListView()
final ListView list=getListView()<ListView 
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>