我正在关注http://www.androidhive.info/2012/05/android-rss-reader-application-using-sqlite-part-1/位于{{3}}的教程,其中第2个链接位于第2个底部。我键入了所有代码,然后尝试编译它,但我一直收到此错误引起:java.lang.NullPointerException 在bsu.edu.cs339.nbanews.MainActivity.onCreate(MainActivity.java:52),这是这行代码
lv.setOnItemClickListener(new OnItemClickListener(){
这个项目非常庞大,有9个类和9个不同的xml文件,所以我没有包含它们,但是url确实包含了本教程中包含的所有这些类。出于好奇,我粘贴了他的代码,想想也许我输错了一些东西,但它在同一行上给了我同样的错误。
public class MainActivity extends Activity {
private ProgressDialog pDialog;
ArrayList<HashMap<String, String>> rssFeedList;
RSSParser rssParser = new RSSParser();
RSSFeed rssFeed;
ImageButton btnAddSite;
String[] sqliteIds;
public static String TAG_ID = "id";
public static String TAG_TITLE = "title";
public static String TAG_LINK = "link";
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.site_list);
btnAddSite = (ImageButton) findViewById(R.id.btnAddSite);
rssFeedList = new ArrayList<HashMap<String, String>>();
new loadStoreSites().execute();
lv = (ListView) findViewById(R.id.list);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String sqlite_id = ((TextView) view.findViewById(R.id.
sqlite_id)).getText().toString();
Intent in = new Intent(getApplicationContext(),
ListRssItemsActivity.class);
in.putExtra(TAG_ID, sqlite_id);
startActivity(in);
}
});
btnAddSite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),
AddNewSiteActivity.class);
startActivityForResult(i, 100);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == 100) {
Intent intent = getIntent();
finish();
startActivity(intent);
}
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo
menuInfo) {
if (v.getId()==R.id.list) {
menu.setHeaderTitle("Delete");
menu.add(Menu.NONE, 0, 0, "Delete Feed");
}
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)
item.getMenuInfo();
int menuItemIndex = item.getItemId();
if(menuItemIndex == 0){
RSSDatabaseHandler rssDb = new RSSDatabaseHandler(getApplicationContext());
WebSite site = new WebSite();
site.setId(Integer.parseInt(sqliteIds[info.position]));
rssDb.deleteSite(site);
Intent intent = getIntent();
finish();
startActivity(intent);
}
return true;
}
class loadStoreSites extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(
MainActivity.this);
pDialog.setMessage("Loading websites ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
runOnUiThread(new Runnable() {
public void run() {
RSSDatabaseHandler rssDb = new RSSDatabaseHandler(
getApplicationContext());
List<WebSite> siteList = rssDb.getAllSites();
sqliteIds = new String[siteList.size()];
for (int i = 0; i < siteList.size(); i++) {
WebSite s = siteList.get(i);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_ID, s.getId().toString());
map.put(TAG_TITLE, s.getTitle().toString());
map.put(TAG_LINK, s.getLink().toString());
rssFeedList.add(map);
sqliteIds[i] = s.getId().toString();
}
ListAdapter adapter = new SimpleAdapter(
MainActivity.this,
rssFeedList, R.layout.site_list_row,
new String[] { TAG_ID, TAG_TITLE, TAG_LINK },
new int[] { R.id.sqlite_id, R.id.title, R.id.link });
lv.setAdapter(adapter);
registerForContextMenu(lv);
}
});
return null;
}
protected void onPostExecute(String args) {
pDialog.dismiss();
}
}
}
答案 0 :(得分:0)
确保activity_main.xml
拥有此内容,
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
.................
..........
...... >
</ListView>
从doInBackground
ListAdapter adapter = new SimpleAdapter(
MainActivity.this,
rssFeedList, R.layout.site_list_row,
new String[] { TAG_ID, TAG_TITLE, TAG_LINK },
new int[] { R.id.sqlite_id, R.id.title, R.id.link });
lv.setAdapter(adapter);
到onPostExecute
并在adapter.notifyDataSetChanged()
之后致电lv.setAdapter(adapter)
。