我一直在关注“Head First Android”这本书,我被困在第3章。
这个小应用程序是一个非常基本的布局; 3个文本视图和1个图像视图,应该在阅读NASA RSS日常图像后更新。
我已经完成了这一章,但现在运行应用程序时只显示一个空白屏幕。
任何帮助表示赞赏。这是代码:
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;
}
}
答案 0 :(得分:3)
您应该使用AsyncTask
作为内部类。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IotdHandler handler = new IotdHandler ();
new MyTask().execute();
}
然后在doInBackground()中解析文档并在onPostExecute()中调用resetDisplay。
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);
}
}
有关如何传递参数,返回结果等的更多信息。AsyncTask Document
答案 1 :(得分:0)
如果你继续阅读这本书,你将面临更多的错误,这就是为什么首先从亚马逊那里拿到这本书。如果你是初学者,那么试试波士顿,Marcana,vogella tutorils或者你可以尝试{ {3}}以及您可以参考Android tutorials from google
的同一问题答案 2 :(得分:0)
此问题的另一个问题是截至撰写本书时的RSS提要是一个帖子,现在它是每日图像提要的最近三十天的列表。这让我遇到了同样的问题。因此,需要更新XML处理并使用AsyncTask才能获得可用的应用程序。
答案 3 :(得分:0)
两个部分解决了:
1 - IotdHandler:
package com.headfirstlab.nasadailyimage;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;
public class NasaDailyImage extends ActionBarActivity {
IotdHandler iotdHandler = new IotdHandler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nasa_daily_image);
IotdHandler handler = new IotdHandler();
handler.processFeed();
resetDisplay(iotdHandler.getTitle(), iotdHandler.getDate(),
iotdHandler.getUrl(), iotdHandler.getDescription());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.nasa_daily_image, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void resetDisplay(String title, String date,
String imageUrl, String 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(iotdHandler.getUrl());
imageView.setImageBitmap(IotdHandler.getBitmap(iotdHandler.getUrl()));
TextView descriptionView = (TextView)findViewById(R.id.imageDescription);
descriptionView.setText(description);
}
}
2 - NasaDailyImage:
package com.headfirstlab.nasadailyimage;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;
public class NasaDailyImage extends ActionBarActivity {
IotdHandler iotdHandler = new IotdHandler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nasa_daily_image);
IotdHandler handler = new IotdHandler();
handler.processFeed();
resetDisplay(iotdHandler.getTitle(), iotdHandler.getDate(),
iotdHandler.getUrl(), iotdHandler.getDescription());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.nasa_daily_image, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void resetDisplay(String title, String date,
String imageUrl, String 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(IotdHandler.getBitmap(iotdHandler.getUrl()));
TextView descriptionView = (TextView)findViewById(R.id.imageDescription);
descriptionView.setText(description);
}
}
希望你觉得它很有用:)