Java Android交换机视图
我构建了一个RSS阅读器,当单击列表项时,它会切换到详细视图。我在打开新视图时遇到问题。我的代码在这里:Java Android交换机视图,java,android,xml,onclick,Java,Android,Xml,Onclick,我构建了一个RSS阅读器,当单击列表项时,它会切换到详细视图。我在打开新视图时遇到问题。我的代码在这里: public void onItemClick(AdapterView parent, View v, int position, long id) { //Log.i(tag,"item clicked! [" + feed.getItem(position).getTitle() + "]"); Intent itemintent = new Int
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);
}
}
package com.CalvaryChapelMelbourne.feedparser;
导入android.app.Activity;
导入android.os.Bundle;
导入android.view.*;
导入android.widget.TextView;
导入android.widget.ListView;
导入android.widget.AdapterView;
导入android.widget.ArrayAdapter;
导入android.widget.AdapterView.OnItemClickListener;
导入android.util.Log;
导入java.io.PrintWriter;
导入java.io.StringWriter;
导入java.net.URL;
导入javax.xml.parsers.SAXParser;
导入javax.xml.parsers.SAXParserFactory;
导入org.xml.sax.InputSource;
导入org.xml.sax.XMLReader;
导入android.content.Intent;
导入com.CalvaryChapelMelbourne.feedparser.ShowDescription;
公共类RSSReader扩展活动实现了MClickListener
{
公共最终字符串RSSFEEDOFCHOICE=”http://app.calvaryccm.com/mobile/android/v1/devos";
公共最终字符串tag=“RSSReader”;
专用RSSFeed feed=null;
/**在首次创建活动时调用*/
创建公共空间(捆绑冰柱){
超级冰柱;
setContentView(R.layout.main);
//去拿我们的饲料!
feed=getFeed(rssfeedof选择);
//显示用户界面
UpdateDisplay();
}
私有RSSFeed getFeed(字符串urlToRssFeed)
{
尝试
{
//设置url
URL=新URL(urlToRssFeed);
//创建工厂
SAXParserFactory=SAXParserFactory.newInstance();
//创建解析器
SAXParser parser=factory.newSAXParser();
//创建读卡器(扫描仪)
XMLReader=parser.getXMLReader();
//实例化我们的处理程序
RSSHandler-theRssHandler=新的RSSHandler();
//指派我们的处理人
setContentHandler(ThersHandler);
//通过url类获取我们的数据
InputSource is=新的InputSource(url.openStream());
//执行同步解析
parse(is);
//获取结果-应为完全填充的RSSFeed实例,或错误时为null
返回theRssHandler.getFeed();
}
捕获(异常ee)
{
//如果有问题,只需返回null
System.out.println(ee.getMessage());
System.out.println(ee.getStackTrace());
System.out.println(ee.getCause());
返回null;
}
}
公共布尔onCreateOptions菜单(菜单)
{
super.onCreateOptions菜单(菜单);
menu.add(menu.NONE,0,0,“刷新”);
Log.i(标记“onCreateOptions菜单”);
返回true;
}
公共布尔值onOptionsItemSelected(菜单项项){
开关(item.getItemId()){
案例0:
Log.i(标签,“设置RSS提要”);
返回true;
案例1:
Log.i(标签,“刷新RSS提要”);
返回true;
}
返回false;
}
私有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(“没有可用的RSS源”);
返回;
}
如果(feedtitle!=null)
feedtitle.setText(feed.getTitle());
如果(feedpubdate!=null)
feedpubdate.setText(feed.getPubDate());
ArrayAdapter=newArrayAdapter(这是android.R.layout.simple_list_item_1,feed.getAllItems());
itemlist.setAdapter(适配器);
itemlist.setOnItemClickListener(此);
itemlist.setSelection(0);
}
public void onItemClick(AdapterView父视图、视图v、整型位置、长id)
{
//Log.i(标记“单击的项”[“+feed.getItem(position.getTitle()+”]);
Intent itemintent=新的Intent(这个,showsdescription.class);
Bundle b=新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);
}
}
您需要在onItemClick
方法的末尾调用startActivity(itemintent)
。此外,您正在切换的是活动,而不是视图。请参阅和以更好地理解。您必须开始您的ShowDescription
活动
public void onItemClick(AdapterView parent, View v, int position, long id) {
...
startActivity(itemintent);
}
什么样的问题?Logcat错误?崩溃或者您需要帮助?在onItemClick()方法中编写代码行startActivity(itemintent)。谢谢!但现在我有了一个新问题。当我单击某个项目时,应用程序崩溃。在原始问题中发布logcat的堆栈跟踪输出。我解决了我的问题。我忘了将ShowDescription类添加到AndoidManifest