我一直在关注这本书深入浅出Android的,而我被困在第3章。
这一点的应用程序是一个非常基本的布局; 3文本视图和1的图像来看,应该从美国航空航天局的RSS日常图像读取后更新。
我已经完成了一章,但现在运行应用程序时只显示一个空白屏幕。
任何帮助AP preciated。这是code:
公共类MainActivity延伸活动{
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
IotdHandler处理器=新IotdHandler();
handler.processFeed();
resetDisplay(handler.getTitle(),handler.getDate(),handler.getImage(),handler.getDescription());
}
公共类IotdHandler扩展的DefaultHandler {
私人字符串URL =http://www.nasa.gov/rss/dyn/image_of_the_day.rss;
私人布尔INURL = FALSE;
私人INTITLE布尔值= FALSE;
私人布尔inDescription = FALSE;
私人布尔inItem = FALSE;
私人布尔inDate = FALSE;
私人位图图像= NULL;
私人字符串标题= NULL;
私人StringBuffer的说明=新的StringBuffer();
私人字符串日期= NULL;
公共无效processFeed(){
尝试 {
的SAXParserFactory厂=
SAXParserFactory.newInstance();
的SAXParser解析器= factory.newSAXParser();
XMLReader的读卡器= parser.getXMLReader();
reader.setContentHandler(本);
的InputStream的InputStream =新的网址(URL).openStream();
reader.parse(新的InputSource(InputStream中));
}赶上(例外五){}
}
私人位图getBitmap(字符串URL){
尝试 {
HttpURLConnection的连接=(HttpURLConnection类)新的网址(URL).openConnection();
connection.setDoInput(真正的);
connection.connect();
输入的InputStream = connection.getInputStream();
位图bilde = BitmapFactory.de codeStream(输入);
input.close();
返回bilde;
}赶上(IOException异常IOE){返回null; }
}
公共无效的startElement(URL字符串,字符串的localName,字符串QNAME,属性的属性)抛出的SAXException {
如果(localName.endsWith(JPG)){INURL = TRUE; }
其他{INURL = FALSE; }
如果(localName.startsWith(项目)){inItem = TRUE; }
否则,如果(inItem){
如果(localName.equals(标题)){= INTITLE真实; }
其他INTITLE {= FALSE; }
如果(localName.equals(说明)){inDescription = TRUE; }
其他{inDescription = FALSE; }
如果(localName.equals(pubdate的)){inDate = TRUE; }
其他{inDate = FALSE; }
}
}
公共无效字符(字符CH [],诠释开始,诠释长度){字符串的字符=新的String(CH).substring(启动,启动+长度);
如果(inurl这样和放大器;&安培; URL == NULL){图像= getBitmap(字); }
如果(与INTITLE功放;&安培;标题== NULL){标题=字符; }
如果(inDescription){description.append(字); }
如果(inDate&安培;&放大器;日期== NULL){日期=字符; }
}
公共位图的getImage(){返回图像; }
公共字符串的getTitle(){返回称号; }
公共StringBuffer的getDescription(){返回描述; }
公共字符串GETDATE(){返回日期; }
}
私人无效resetDisplay(标题字符串,字符串日期,位图图像,StringBuffer的描述){
TextView的titleview的=(的TextView)findViewById(R.id.imageTitle);
titleView.setText(职称);
TextView的dateView =(TextView中)findViewById(R.id.imageDate);
dateView.setText(日期);
ImageView的ImageView的=(ImageView的)findViewById(R.id.imageDisplay);
imageView.setImageBitmap(图像);
TextView的descriptionView =(TextView中)findViewById(R.id.imageDescription);
descriptionView.setText(介绍);
}
@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
//充气菜单;这增加了项目操作栏,如果它是present。
。getMenuInflater()膨胀(R.menu.main,菜单);
返回true;
}
}
解决方案
您应该使用的AsyncTask
作为一个内部类。
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
IotdHandler处理器=新IotdHandler();
新MyTask的()执行()。
}
,然后解析doInBackground(文件),并调用resetDisplay在onPostExecute()。
公共类MyTask的扩展AsyncTask的<虚空,虚空,虚空> {
@覆盖
保护无效doInBackground(虚空...... PARAMS){
handler.processFeed();
返回null;
}
@覆盖
保护无效onPostExecute(无效的结果){
resetDisplay(handler.getTitle(),handler.getDate(),handler.getImage(),handler.getDescription());
super.onPostExecute(结果);
}
}
有关更多信息如何传递参数,返回结果等。 AsyncTask的文档
I've been following the book "Head First Android", and I'm stuck in Chapter 3.
This little app is a really basic layout; 3 text views and 1 image view, that should update after reading from NASA RSS daily image.
I've completed the chapter but now when running the app only shows a Blank screen.
Any help appreciated. This is the code:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IotdHandler handler = new IotdHandler ();
handler.processFeed();
resetDisplay (handler.getTitle(), handler.getDate(), handler.getImage(), handler.getDescription());
}
public class IotdHandler extends DefaultHandler {
private String url = "http://www.nasa.gov/rss/dyn/image_of_the_day.rss";
private boolean inUrl = false;
private boolean inTitle = false;
private boolean inDescription = false;
private boolean inItem = false;
private boolean inDate = false;
private Bitmap image = null;
private String title = null;
private StringBuffer description = new StringBuffer();
private String date = null;
public void processFeed() {
try {
SAXParserFactory factory =
SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
XMLReader reader = parser.getXMLReader();
reader.setContentHandler(this);
InputStream inputStream = new URL(url).openStream();
reader.parse(new InputSource(inputStream));
} catch (Exception e) { }
}
private Bitmap getBitmap(String url) {
try {
HttpURLConnection connection = (HttpURLConnection)new URL(url).openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap bilde = BitmapFactory.decodeStream(input);
input.close();
return bilde;
} catch (IOException ioe) { return null; }
}
public void startElement(String url, String localName, String qName, Attributes attributes) throws SAXException {
if (localName.endsWith(".jpg")) { inUrl = true; }
else { inUrl = false; }
if (localName.startsWith("item")) { inItem = true; }
else if (inItem) {
if (localName.equals("title")) { inTitle = true; }
else { inTitle = false; }
if (localName.equals("description")) { inDescription = true; }
else { inDescription = false; }
if (localName.equals("pubDate")) { inDate = true; }
else { inDate = false; }
}
}
public void characters(char ch[], int start, int length) { String chars = new String(ch).substring(start, start + length);
if (inUrl && url == null) { image = getBitmap(chars); }
if (inTitle && title == null) { title = chars; }
if (inDescription) { description.append(chars); }
if (inDate && date == null) { date = chars; }
}
public Bitmap getImage() { return image; }
public String getTitle() { return title; }
public StringBuffer getDescription() { return description; }
public String getDate() { return date; }
}
private void resetDisplay (String title, String date, Bitmap image, StringBuffer description) {
TextView titleView = (TextView) findViewById (R.id.imageTitle);
titleView.setText(title);
TextView dateView = (TextView) findViewById(R.id.imageDate);
dateView.setText(date);
ImageView imageView = (ImageView) findViewById (R.id.imageDisplay);
imageView.setImageBitmap(image);
TextView descriptionView = (TextView) findViewById (R.id.imageDescription);
descriptionView.setText(description);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
解决方案
You should use AsyncTask
as an inner class.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IotdHandler handler = new IotdHandler ();
new MyTask().execute();
}
and then parse document in doInBackground() and call resetDisplay in onPostExecute().
public class MyTask extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... params) {
handler.processFeed();
return null;
}
@Override
protected void onPostExecute(Void result) {
resetDisplay (handler.getTitle(), handler.getDate(), handler.getImage(), handler.getDescription());
super.onPostExecute(result);
}
}
For more info how to pass parameter,return result etc.. AsyncTask Document
上一篇:ListView控件的消息应用程序滚动显示错后的listItem布局控件、应用程序、布局、消息
下一篇:无法解析符号奇巧不能解析方法getAllocationByteCount()奇巧、符号、方法、getAllocationByteCount