gpt4 book ai didi

android - MainActivity找不到id为android.R.id.list的ListView,但是它是存在的

转载 作者:行者123 更新时间:2023-11-29 14:25:15 26 4
gpt4 key购买 nike

我正在关注 this tutorial .

我已经根据我的应用程序的需要对其进行了修改,例如没有CRUD 功能和没有主菜单 - 我只想列出应用程序启动时执行的主要 Activity 期间的所有项目.

代码似乎没有错误,但运行它给我:Unfortunately, MyFirstApp has stopped in the VM.

LogCat 给我这个:

E/AndroidRuntime(910): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstproject/com.example.myfirstproject.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

做什么?我检查了我的 .xml 布局并进行了更改,但应用程序仍然崩溃。

MainActivity.java

package com.example.myfirstproject;

//imports

public class MainActivity extends ListActivity implements OnItemClickListener {

// Progress Dialog
private ProgressDialog pDialog;

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

ArrayList<HashMap<String, String>> carsList;

// url to get all products list
private static String url_all_cars = "http://localhost/webservice/get_all_cars.php";

// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_CARS = "cars";
private static final String TAG_NAME = "name";

// products JSONArray
JSONArray cars = null;

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

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

// Loading products in Background Thread
new LoadAllcars().execute();

// Get listview
ListView lv = getListView();
}

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

/**
* Background Async Task to Load all product by making HTTP Request
* */
class LoadAllcars extends AsyncTask<String, String, String> {

/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading cars. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}

/**
* getting All products from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_cars, "GET", params);

// Check your log cat for JSON reponse
Log.d("All cars: ", json.toString());

try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);

if (success == 1) {
// products found
// Getting Array of Products
cars = json.getJSONArray(TAG_CARS);

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

// Storing each json item in variable
String title = c.getString(TAG_NAME);

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

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

// adding HashList to ArrayList
carsList.add(map);
}
} else {
// no products found
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("No cars found");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
} catch (JSONException e) {
e.printStackTrace();
}

return null;
}

/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, carsList,
android.R.id.list, new String[] {TAG_NAME},
new int[] { R.id.title });
// updating listview
setListAdapter(adapter);
}
});

}
}
}

activity_main.xml(带有 ListView 的主 Activity 的布局):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</ListView>
</LinearLayout>

list_item.xml(单个列表项的布局):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<!-- Name Label -->
<TextView
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="6dip"
android:paddingLeft="6dip"
android:textSize="17dip"
android:textStyle="bold" />

</LinearLayout>

最佳答案

看看他们如何在 ListActivity 文档中定义布局 here

您的 ListView Id 是 android:id="@+id/list" 并且它需要是 android:id="@android:id/list">/p>

另外,你的 ListAdapter 会崩溃

ListAdapter adapter = new SimpleAdapter(
MainActivity.this, carsList,
android.R.id.list, new String[] {TAG_NAME},
new int[] { R.id.title });

您告诉适配器使用 android ListView 作为 Item View 。您应该为此传递您的 list_item.xml ID 并使用正确的 TextView ID(名称)

例如:

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

关于android - MainActivity找不到id为android.R.id.list的ListView,但是它是存在的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13203092/

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