我通过JSON从我的服务器获取数据,我正在尝试显示我在ListView中获取的数据,但每次我收到此错误Your content must have a ListView whose id attribute is 'android.R.id.list'.
我从中得到的所有内容服务器很好,数据设置很好,但只是将它添加到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();
}
}
}
}
logcat的
06-09 14:54:13.970: E/AndroidRuntime(17826): FATAL EXCEPTION: main
06-09 14:54:13.970: E/AndroidRuntime(17826): java.lang.RuntimeException: Unable to start activity ComponentInfo{eu.example.www/eu.example.www.List}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
06-09 14:54:13.970: E/AndroidRuntime(17826): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
06-09 14:54:13.970: E/AndroidRuntime(17826): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
06-09 14:54:13.970: E/AndroidRuntime(17826): at android.app.ActivityThread.access$600(ActivityThread.java:140)
06-09 14:54:13.970: E/AndroidRuntime(17826): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
06-09 14:54:13.970: E/AndroidRuntime(17826): at android.os.Handler.dispatchMessage(Handler.java:99)
06-09 14:54:13.970: E/AndroidRuntime(17826): at android.os.Looper.loop(Looper.java:137)
06-09 14:54:13.970: E/AndroidRuntime(17826): at android.app.ActivityThread.main(ActivityThread.java:4898)
06-09 14:54:13.970: E/AndroidRuntime(17826): at java.lang.reflect.Method.invokeNative(Native Method)
06-09 14:54:13.970: E/AndroidRuntime(17826): at java.lang.reflect.Method.invoke(Method.java:511)
06-09 14:54:13.970: E/AndroidRuntime(17826): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
06-09 14:54:13.970: E/AndroidRuntime(17826): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
06-09 14:54:13.970: E/AndroidRuntime(17826): at dalvik.system.NativeStart.main(Native Method)
06-09 14:54:13.970: E/AndroidRuntime(17826): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
06-09 14:54:13.970: E/AndroidRuntime(17826): at android.app.ListActivity.onContentChanged(ListActivity.java:243)
06-09 14:54:13.970: E/AndroidRuntime(17826): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:311)
06-09 14:54:13.970: E/AndroidRuntime(17826): at android.app.Activity.setContentView(Activity.java:1924)
06-09 14:54:13.970: E/AndroidRuntime(17826): at eu.example.wwwList.onCreate(List.java:37)
06-09 14:54:13.970: E/AndroidRuntime(17826): at android.app.Activity.performCreate(Activity.java:5206)
06-09 14:54:13.970: E/AndroidRuntime(17826): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
06-09 14:54:13.970: E/AndroidRuntime(17826): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
06-09 14:54:13.970: E/AndroidRuntime(17826): ... 11 more
答案 0 :(得分:0)
您的_list.xml
必须包含身份ListView
"@android:id/list"
像这样:
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
根据ListActivity
ListActivity具有默认布局,该布局由屏幕中央的单个全屏列表组成。但是,如果需要,可以通过在onCreate()中使用setContentView()设置自己的视图布局来自定义屏幕布局。为此,您自己的视图必须包含一个ID为“@android:id / list”的ListView对象(如果它在代码中则列出)