我正在关注本教程。
我已将其修改为满足我的应用程序的需求,例如没有CRUD功能和主菜单-我只想列出启动应用程序时执行的主要活动期间的所有项目。
代码似乎没有错误,但是运行它可以给我:Unfortunately, MyFirstApp has stopped在VM中。
LogCat给我这个:
E / AndroidRuntime(910):java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.myfirstproject / com.example.myfirstproject.MainActivity}:java.lang.RuntimeException:您的内容必须具有ID属性为'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);
}
});
}
}
}
Run Code Online (Sandbox Code Playgroud)
activity_main.xml(具有listview的mainactivity的布局):
<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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
看看他们的ListActivity文档中如何定义布局在这里
您的ListView ID是android:id="@+id/list",它必须是android:id="@android:id/list"
此外,您的ListAdapter将会崩溃
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, carsList,
android.R.id.list, new String[] {TAG_NAME},
new int[] { R.id.title });
Run Code Online (Sandbox Code Playgroud)
您正在告诉适配器使用android ListView作为Item视图。.您应该为此输入您的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});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10785 次 |
| 最近记录: |