2011-10-25 43 views
0

我已經構建了一個RSS閱讀器,當單擊列表項時切換到詳細視圖。我在打開新視圖時遇到問題。我的代碼是在這裏:Android開關視圖

public void onItemClick(AdapterView parent, View v, int position, long id) 
{ 

    //Log.i(tag,"item clicked! [" + feed.getItem(position).getTitle() + "]");  

    Intent itemintent = new Intent(this,ShowDescription.class); 

    Bundle b = new Bundle(); 
    b.putString("title", feed.getItem(position).getTitle()); 
    b.putString("description", feed.getItem(position).getDescription()); 
    b.putString("link", feed.getItem(position).getLink()); 
    b.putString("pubdate", feed.getItem(position).getPubDate()); 

    itemintent.putExtra("android.intent.extra.INTENT", b); 

} 

全班的代碼如下:

package com.CalvaryChapelMelbourne.feedparser; 


import android.app.Activity; 
import android.os.Bundle; 
import android.view.*; 
import android.widget.TextView; 
import android.widget.ListView; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.AdapterView.OnItemClickListener; 
import android.util.Log; 

import java.io.PrintWriter; 
import java.io.StringWriter; 
import java.net.URL; 

import javax.xml.parsers.SAXParser; 
import javax.xml.parsers.SAXParserFactory; 

import org.xml.sax.InputSource; 

import org.xml.sax.XMLReader; 

import android.content.Intent; 

import com.CalvaryChapelMelbourne.feedparser.ShowDescription; 

public class RSSReader extends Activity implements OnItemClickListener 
{ 

public final String RSSFEEDOFCHOICE = "http://app.calvaryccm.com/mobile/android/v1/devos"; 

public final String tag = "RSSReader"; 
private RSSFeed feed = null; 

/** Called when the activity is first created. */ 

public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    setContentView(R.layout.main); 

    // go get our feed! 
    feed = getFeed(RSSFEEDOFCHOICE); 

    // display UI 
    UpdateDisplay(); 

} 


private RSSFeed getFeed(String urlToRssFeed) 
{ 
    try 
    { 
     // setup the url 
     URL url = new URL(urlToRssFeed); 

     // create the factory 
     SAXParserFactory factory = SAXParserFactory.newInstance(); 
     // create a parser 
     SAXParser parser = factory.newSAXParser(); 

     // create the reader (scanner) 
     XMLReader xmlreader = parser.getXMLReader(); 
     // instantiate our handler 
     RSSHandler theRssHandler = new RSSHandler(); 
     // assign our handler 
     xmlreader.setContentHandler(theRssHandler); 
     // get our data via the url class 
     InputSource is = new InputSource(url.openStream()); 
     // perform the synchronous parse   
     xmlreader.parse(is); 
     // get the results - should be a fully populated RSSFeed instance, or null on error 
     return theRssHandler.getFeed(); 
    } 
    catch (Exception ee) 
    { 
     // if we have a problem, simply return null 
     System.out.println(ee.getMessage()); 
     System.out.println(ee.getStackTrace()); 
     System.out.println(ee.getCause()); 
     return null; 
    } 
} 
public boolean onCreateOptionsMenu(Menu menu) 
{ 
    super.onCreateOptionsMenu(menu); 
    menu.add(Menu.NONE, 0, 0, "Refresh"); 
    Log.i(tag,"onCreateOptionsMenu"); 
    return true; 
} 

public boolean onOptionsItemSelected(MenuItem item){ 
    switch (item.getItemId()) { 
    case 0: 

     Log.i(tag,"Set RSS Feed"); 
     return true; 
    case 1: 
     Log.i(tag,"Refreshing RSS Feed"); 
     return true; 
    } 
    return false; 
} 


private void UpdateDisplay() 
{ 
    TextView feedtitle = (TextView) findViewById(R.id.feedtitle); 
    TextView feedpubdate = (TextView) findViewById(R.id.feedpubdate); 
    ListView itemlist = (ListView) findViewById(R.id.itemlist); 


    if (feed == null) 
    { 
     feedtitle.setText("No RSS Feed Available"); 
     return; 
    } 

    if(feedtitle != null) 
     feedtitle.setText(feed.getTitle()); 
    if(feedpubdate != null) 
     feedpubdate.setText(feed.getPubDate()); 


    ArrayAdapter<RSSItem> adapter = new ArrayAdapter<RSSItem>(this,android.R.layout.simple_list_item_1,feed.getAllItems()); 

    itemlist.setAdapter(adapter); 

    itemlist.setOnItemClickListener(this); 

    itemlist.setSelection(0); 

} 


public void onItemClick(AdapterView parent, View v, int position, long id) 
{ 

    //Log.i(tag,"item clicked! [" + feed.getItem(position).getTitle() + "]");  

    Intent itemintent = new Intent(this,ShowDescription.class); 

    Bundle b = new Bundle(); 
    b.putString("title", feed.getItem(position).getTitle()); 
    b.putString("description", feed.getItem(position).getDescription()); 
    b.putString("link", feed.getItem(position).getLink()); 
    b.putString("pubdate", feed.getItem(position).getPubDate()); 

    itemintent.putExtra("android.intent.extra.INTENT", b); 

} 

} 
+0

什麼樣的問題? Logcat錯誤?崩潰?或者你需要幫助? – Warpzit

+0

在onItemClick()方法中寫入代碼行startActivity(itemintent)。 – user370305

+0

發佈錯誤日誌在這裏。 – user370305

回答

2

您需要在您的onItemClick方法的最後調用startActivity(itemintent)。另外,你正在切換的是活動,而不是視圖。請參閱this documentationthis documentation on UI以獲得更好的理解。

+0

謝謝!但是現在我有一個新問題。當我點擊一個項目時,應用程序崩潰。 –

+0

在原始問題中發佈logcat的堆棧跟蹤輸出 – Craigy

+0

我解決了我的問題。我忘了將ShowDescription類添加到AndoidManifest中 –

0

你必須開始你的ShowDescription活動。

public void onItemClick(AdapterView parent, View v, int position, long id) { 
    ... 
    startActivity(itemintent); 
}