gpt4 book ai didi

android - 无法启动 Activity ComponentInfo :java. lang.RuntimeException:您的内容必须有一个 ListView,其 id 属性为 'android.R.id.list'

转载 作者:行者123 更新时间:2023-11-30 22:34:07 25 4
gpt4 key购买 nike

我刚开始使用 json 。以 json 格式从 mysql 获取数据显示在 listview android 上。 Caused by: java.lang.RuntimeException: 你的内容必须有一个 ListView,它的 id 属性是 'android.R.id.list'

public class MainActivity extends ListActivity {
private ProgressDialog pDialog;

// URL to get contacts JSON
private static String url= "http://127.0.0.1/bloodRequest/data.php";
// JSON Node names
private static final String TAG_CONTACTS = "requests ";
private static final String TAG_NAME = "User_name";
private static final String TAG_BLOCKNO = "user_block_no";
private static final String TAG_ADDRESS = "user_stree_address";
private static final String TAG_CITY = "user_city_village";
private static final String TAG_TALUKA = "user_taluka";
private static final String TAG_DISTRICT = "user_district";
private static final String TAG_BLOOD_TYPE = "blood_type";
private static final String TAG_CREATE_DATE = "createdate";
private static final String TAG_PHONE = "user_mobile";

// contacts JSONArray
JSONArray requests= null;

// Hashmap for ListView
ArrayList<HashMap<String, String>> contactList;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView list = getListView();
new GetContacts().execute();
}
/**
* Async task class to get json by making HTTP call
* */
private class GetContacts extends AsyncTask<Void, Void, Void> {

@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();

}

@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();

// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.POST);

Log.d("Response: ", "> " + jsonStr);

if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);

// Getting JSON Array node
requests = jsonObj.getJSONArray(TAG_CONTACTS);

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


String name = c.getString(TAG_NAME);
String blockno = c.getString(TAG_BLOCKNO);
String address = c.getString(TAG_ADDRESS);
String city = c.getString(TAG_CITY);
String district=c.getString(TAG_DISTRICT);
String taluk=c.getString(TAG_TALUKA);
String blodtype=c.getString(TAG_BLOOD_TYPE);
String phone=c.getString(TAG_PHONE);
String createdate=c.getString(TAG_CREATE_DATE);

// tmp hashmap for single contact
HashMap<String, String> requests1 = new HashMap<String, String>();

// adding each child node to HashMap key => value

requests1 .put(TAG_NAME, name);
requests1 .put(TAG_BLOCKNO, blockno);
requests1 .put(TAG_ADDRESS, address);
requests1 .put(TAG_CITY, city);
requests1 .put(TAG_TALUKA, taluk);
requests1 .put(TAG_DISTRICT, district);
requests1 .put(TAG_BLOOD_TYPE, blodtype);
requests1 .put(TAG_PHONE, phone);
requests1 .put(TAG_CREATE_DATE, createdate);

// adding contact to contact list
contactList.add( requests1 );
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}

return null;
}

@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */

ListAdapter adapter = new SimpleAdapter(
MainActivity.this, contactList,
R.layout.item_list,
new String[] { TAG_NAME, TAG_BLOCKNO,TAG_ADDRESS,TAG_CITY,TAG_TALUKA,TAG_DISTRICT,TAG_BLOOD_TYPE,TAG_PHONE,TAG_CREATE_DATE},
new int[] { R.id.name, R.id.blono, R.id.street,R.id.city,R.id.taluk,R.id.distr,R.id.btype,R.id.phno,R.id.crdate });

setListAdapter(adapter);
}

}
}

最佳答案

使用 android:id="@android:id/list" 添加到您的 activity_main ListView

如果您在 Activity 布局中使用 ListActivity,您应该添加:

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

关于android - 无法启动 Activity ComponentInfo :java. lang.RuntimeException:您的内容必须有一个 ListView,其 id 属性为 'android.R.id.list',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33035378/

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