我正在开发和Android应用程序从mysql数据库检索新闻并在列表视图中填充它们,我设法成功检索数据但面临一个阻止我继续我的项目的主要问题。当我尝试在listview中填充数据时,应用程序崩溃,在日志猫中显示以下错误
your content must have a ListView whose id attribute is 'android.R.id.list'
我试图更改我的xml listview id并以不同的形式编写它但它仍然失败并显示相同的错误。 这是我的xml代码。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/android.R.id.list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#fff" />
<LinearLayout
android:id="@+id/read_news"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2"
android:baselineAligned="false" />
</LinearLayout>
这是我的java代码
package com.jetas.sportsapp;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONObject;
import android.annotation.SuppressLint;
import android.app.ListActivity;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import com.jetas.sportsapp.utils.JsonUtils;
public class News extends ListActivity {
// private ProgressDialog pDialog;
private ArrayList<HashMap<String,String>> mCommentList;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.news_single_post);
StrictMode.enableDefaults();
ArrayList<HashMap<String,String>> newsList = getData();
updateList(newsList);
}
public ArrayList<HashMap<String,String>> getData() {
TextView textview = (TextView) findViewById(R.id.newsTitle);
try {
// String s = "";
String news =
JsonUtils.read("http://10.0.2.2/index.php/android/news?offset=0&limit=10");
Log.d("News data",news);
JSONArray jArray = new JSONArray(news);
Log.d("Parsing json", jArray.toString());
for (int i = 0; i < jArray.length(); i++) {
JSONObject json = jArray.getJSONObject(i);
String title = json.getString("title");
String content = json.getString("content");
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
map.put("title", title);
map.put("content", content);
// adding HashList to ArrayList
mCommentList.add(map);
// textview.setText(title);
// s = s + "Title: " + json.getString("title") + "\n"
// + "Content: " + json.getString("content") +
"\n\n";
// textview.setText(s);
}
} catch (Exception e) {
Log.e("log_tag", "Error Parsing Data" + e.toString());
textview.setText("Error in Parsing Data !!!!!");
}
return mCommentList;
}
/**
* Inserts the parsed data into the listview.
*/
private void updateList(ArrayList<HashMap<String,String>> newsLists) {
// For a ListActivity we need to set the List Adapter, and in order
// to do
//that, we need to create a ListAdapter. This SimpleAdapter,
//will utilize our updated Hashmapped ArrayList,
//use our single_post xml template for each item in our list,
//and place the appropriate info from the list to the
//correct GUI id. Order is important here.
ListAdapter adapter = new SimpleAdapter(this, newsLists,
R.layout.news_single_post, new String[] { "title",
"content"}, new int[] { R.id.newsTitle, R.id.content,});
// I shouldn't have to comment on this one:
setListAdapter(adapter);
// Optional: when the user clicks a list item we
//could do something. However, we will choose
//to do nothing...
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// This method is triggered if an item is click
// within our
// list. For our example we won't be using this, but
// it is useful to know in real life applications.
}
});
}
// public class LoadComments extends AsyncTask<Void, Void, Boolean> {
//
// @Override
// protected void onPreExecute() {
// super.onPreExecute();
// pDialog = new ProgressDialog(News.this);
// pDialog.setMessage("Loading Comments...");
// pDialog.setIndeterminate(false);
// pDialog.setCancelable(true);
// pDialog.show();
// }
// @Override
// protected Boolean doInBackground(Void... arg0) {
// //we will develop this method in version 2
// getData();
// return null;
//
// }
//
//
// @Override
// protected void onPostExecute(Boolean result) {
// super.onPostExecute(result);
// pDialog.dismiss();
// //we will develop this method in version 2
// updateList();
// }
}
我还想设置一下,当用户点击新闻时,新活动会打开,并附上新闻的详细信息。
答案 0 :(得分:2)
试试这个..
将此android:id="@+id/android.R.id.list"
更改为android:id="@android:id/list"
,如下所示
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#fff" />
答案 1 :(得分:0)
我遇到了同样的问题并通过以下方式解决了
Listview lv;
lv = (listview) Findviewbyid(android.R.id.listviewname);