gpt4 book ai didi

android - 获取运行时崩溃日志说明您的内容必须有一个 ListView ,其 id 属性为 'android.R.id.list' ,不确定为什么?

转载 作者:行者123 更新时间:2023-11-29 21:51:02 24 4
gpt4 key购买 nike

我收到运行时异常 java.lang.RuntimeException:您的内容必须有一个 ListView,其 id 属性为“android.R.id.list”。不确定这里出了什么问题。我查看了其他一些答案,似乎将 ListView id 更改为 list 是问题所在,但这似乎不起作用。

activity_search.xml

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

</LinearLayout>

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- Single List Item Design -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold" >
</TextView>

搜索 Activity .java

package com.chance.squat;

import com.chance.squat.R;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class SearchActivity extends ListActivity {

// url to make request
private static String url = "http://192.168.1.115:3000/bathrooms/nearby.json/?lat=45.580639&lon=-122.677682";

// JSON Node names
private static final String TAG_BATHROOMS = "bathrooms";
private static final String TAG_ID = "ID";
private static final String TAG_NAME = "name";
private static final String TAG_DISTANCE = "distance";

// contacts JSONArray
JSONArray bathrooms = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);

// Hashmap for ListView
ArrayList<HashMap<String, String>> bathroomList = new ArrayList<HashMap<String, String>>();

// Creating JSON Parser instance
JSONParser jParser = new JSONParser();

// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);

try {
// Getting Array of Contacts
bathrooms = json.getJSONArray(TAG_BATHROOMS);

// looping through All Contacts
for(int i = 0; i < bathrooms.length(); i++){
JSONObject c = bathrooms.getJSONObject(i);

// Storing each json item in variable
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String distance = c.getString(TAG_DISTANCE);
String distanceTrimmed = distance.substring(0,4) + " " + "miles away";

System.out.println(name);

// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();

// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_NAME, name);
map.put(TAG_DISTANCE, distanceTrimmed);

// adding HashList to ArrayList
bathroomList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}


/**
* Updating parsed JSON data into ListView
* */

ListAdapter adapter = new SimpleAdapter(this, bathroomList,
R.layout.list_item,
new String[] { TAG_NAME}, new int[] {
R.id.name });

setListAdapter(adapter);

// selecting single ListView item
ListView lv = getListView();

// Launching new screen on Selecting Single ListItem
lv.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
//String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
//String distance = ((TextView) view.findViewById(R.id.distance)).getText().toString();


// Starting new intent
//Intent in = new Intent(getApplicationContext(), SeachActivity.class);
//in.putExtra(TAG_NAME, name);
//in.putExtra(TAG_DISTANCE, distance);
//startActivity(in);

}
});


}

}

最佳答案

将 ListView id 更改为 android:id="@android:id/list" 因为当您扩展 ListActivity 时,您必须具有 ListView id android.R.id。列表

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

关于android - 获取运行时崩溃日志说明您的内容必须有一个 ListView ,其 id 属性为 'android.R.id.list' ,不确定为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14417211/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com