我正在试图提供一个RSS FEED of weather&& psi进入我的应用程序。 它们分别来自雅虎天气和nea.gov.sg。
然而,当我运行应用程序时,我收到强制关闭错误。在查看logcat之后,显示无效索引0,大小为0.有人可以告诉我错误有什么问题,我该如何解决? 我正在尝试使用rss feed。
logcat的
02-20 22:11:50.906: E/AndroidRuntime(12108): FATAL EXCEPTION: main
02-20 22:11:50.906: E/AndroidRuntime(12108): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fuellogproject/com.example.fuellogproject.HomeActivity}: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
02-20 22:11:50.906: E/AndroidRuntime(12108): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
02-20 22:11:50.906: E/AndroidRuntime(12108): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
02-20 22:11:50.906: E/AndroidRuntime(12108): at android.app.ActivityThread.access$700(ActivityThread.java:140)
02-20 22:11:50.906: E/AndroidRuntime(12108): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
02-20 22:11:50.906: E/AndroidRuntime(12108): at android.os.Handler.dispatchMessage(Handler.java:99)
02-20 22:11:50.906: E/AndroidRuntime(12108): at android.os.Looper.loop(Looper.java:137)
02-20 22:11:50.906: E/AndroidRuntime(12108): at android.app.ActivityThread.main(ActivityThread.java:4921)
02-20 22:11:50.906: E/AndroidRuntime(12108): at java.lang.reflect.Method.invokeNative(Native Method)
02-20 22:11:50.906: E/AndroidRuntime(12108): at java.lang.reflect.Method.invoke(Method.java:511)
02-20 22:11:50.906: E/AndroidRuntime(12108): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
02-20 22:11:50.906: E/AndroidRuntime(12108): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
02-20 22:11:50.906: E/AndroidRuntime(12108): at dalvik.system.NativeStart.main(Native Method)
02-20 22:11:50.906: E/AndroidRuntime(12108): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
02-20 22:11:50.906: E/AndroidRuntime(12108): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
02-20 22:11:50.906: E/AndroidRuntime(12108): at java.util.ArrayList.get(ArrayList.java:304)
02-20 22:11:50.906: E/AndroidRuntime(12108): at com.example.fuellogproject.HomeActivity.onCreate(HomeActivity.java:135)
02-20 22:11:50.906: E/AndroidRuntime(12108): at android.app.Activity.performCreate(Activity.java:5188)
02-20 22:11:50.906: E/AndroidRuntime(12108): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
02-20 22:11:50.906: E/AndroidRuntime(12108): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
02-20 22:11:50.906: E/AndroidRuntime(12108): ... 11 more
CODE
public class HomeActivity extends Activity{
Button btnLogIn;
Button btnExit;
TextView name;
final Context context = this;
Session session;
//WEATHERVAR
ArrayList<String> psi;
public TextView psi_text;
TextView weather;
ImageView image;
private static Handler mHandler = new Handler();
class MyWeather{
String conditiontext;
String conditiontemp;
String conditiondate;
public String forecastToString(){
return
" " + conditiontemp + "°C" ;
}
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.homescreen);
weather = (TextView)findViewById(R.id.weather);
image = (ImageView)findViewById(R.id.image);
psi = new ArrayList<String>();
psi_text = (TextView) findViewById(R.id.psi_text);
session = new Session(getApplicationContext());
session.checkLogin();
Log.i("HomeActivity.java", "User login status: " + session.isLoggedIn());
name = (TextView) findViewById(R.id.name);
Bundle extras = getIntent().getExtras();
HashMap<String, String> user = session.getUserDetails();
String username = user.get(Session.KEY_NAME);
name.setText(username);
btnLogIn = (Button) findViewById(R.id.buttonLogIn);
btnExit=(Button) findViewById(R.id.buttonExit);
btnExit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
try {
URL url = new URL(
"http://app2.nea.gov.sg/data/rss/nea_psi.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("item");
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
Element fstElmnt = (Element) node;
NodeList websiteList = fstElmnt.getElementsByTagName("psi");
Element websiteElement = (Element) websiteList.item(0);
websiteList = websiteElement.getChildNodes();
psi.add(""+ ((Node) websiteList.item(0)).getNodeValue());
}
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
String temp = Html.fromHtml(psi.get(0)).toString();
String a[] = temp.split("\\)");
psi_text.setText(""+a[0]+")");
Thread myThread = new Thread(new Runnable(){
@Override
public void run() {
String weatherString = QueryYahooWeather();
Document weatherDoc = convertStringToDocument(weatherString);
final MyWeather weatherResult = parseWeather(weatherDoc);
runOnUiThread(new Runnable(){
@Override
public void run() {
weather.setText(weatherResult.forecastToString());
}});
}});
myThread.start();
}
public void CheckUser(View v)
{
Intent intentAbout=new Intent(getApplicationContext(),MainActivity.class);
startActivity(intentAbout);
}
protected void onDestroy() {
super.onDestroy();
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onRestart() {
super.onRestart();
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onStart() {
super.onStart();
}
@Override
protected void onStop() {
super.onStop();
}
private MyWeather parseWeather(Document srcDoc){
MyWeather myWeather = new MyWeather();
//<yweather:condition.../>
Node conditionNode = srcDoc.getElementsByTagName("yweather:condition").item(0);
String weatherCode = conditionNode.getAttributes()
.getNamedItem("code")
.getNodeValue()
.toString();
// thunderstorms
if(weatherCode.equals("4")){
mHandler.post(new Runnable() {
@Override
public void run() {
// This gets executed on the UI thread so it can safely modify
// Views
image.setImageResource(R.drawable.thunderstorm);
}
});
}
//isolated thunderstorms
else if ( weatherCode.equals("37")) {
mHandler.post(new Runnable() {
@Override
public void run() {
image.setImageResource(R.drawable.thunderstorm);
}
});
}
//scattered thunderstorms
else if ( weatherCode.equals("38")) {
mHandler.post(new Runnable() {
@Override
public void run() {
image.setImageResource(R.drawable.thunderstorm);
}
});
}
//scattered thunderstorms
else if ( weatherCode.equals("39")) {
mHandler.post(new Runnable() {
@Override
public void run() {
image.setImageResource(R.drawable.thunderstorm);
}
});
}
//thundershowers
else if ( weatherCode.equals("45")) {
mHandler.post(new Runnable() {
@Override
public void run() {
image.setImageResource(R.drawable.thunderstorm);
}
});
}
//isolated thundershowers
else if ( weatherCode.equals("47")) {
mHandler.post(new Runnable() {
@Override
public void run() {
image.setImageResource(R.drawable.thunderstorm);
}
});
}
//drizzle
else if ( weatherCode.equals("9")) {
mHandler.post(new Runnable() {
@Override
public void run() {
image.setImageResource(R.drawable.rainy);
}
});
}
//showers
else if ( weatherCode.equals("11")) {
mHandler.post(new Runnable() {
@Override
public void run() {
image.setImageResource(R.drawable.rainy);
}
});
}
//showers
else if ( weatherCode.equals("12")) {
mHandler.post(new Runnable() {
@Override
public void run() {
image.setImageResource(R.drawable.rainy);
}
});
}
//scattered showers
else if ( weatherCode.equals("40")) {
mHandler.post(new Runnable() {
@Override
public void run() {
image.setImageResource(R.drawable.rainy);
}
});
}
//hail
else if ( weatherCode.equals("17")) {
mHandler.post(new Runnable() {
@Override
public void run() {
image.setImageResource(R.drawable.hail);
}
});
}
//mixed rain and hail
else if ( weatherCode.equals("35")) {
mHandler.post(new Runnable() {
@Override
public void run() {
image.setImageResource(R.drawable.hail);
}
});
}
//foggy
else if ( weatherCode.equals("20")) {
mHandler.post(new Runnable() {
@Override
public void run() {
image.setImageResource(R.drawable.foggy);
}
});
}
//haze
else if ( weatherCode.equals("21")) {
mHandler.post(new Runnable() {
@Override
public void run() {
image.setImageResource(R.drawable.foggy);
}
});
}
//smoky
else if ( weatherCode.equals("22")) {
mHandler.post(new Runnable() {
@Override
public void run() {
image.setImageResource(R.drawable.foggy);
}
});
}
//windy
else if ( weatherCode.equals("24")) {
mHandler.post(new Runnable() {
@Override
public void run() {
image.setImageResource(R.drawable.windy);
}
});
}
//cloudy
else if ( weatherCode.equals("26")) {
mHandler.post(new Runnable() {
@Override
public void run() {
image.setImageResource(R.drawable.cloudy);
}
});
}
//fair (night)
else if ( weatherCode.equals("33")) {
mHandler.post(new Runnable() {
@Override
public void run() {
image.setImageResource(R.drawable.cloudy);
}
});
}
//fair (day)
else if ( weatherCode.equals("34")) {
mHandler.post(new Runnable() {
@Override
public void run() {
image.setImageResource(R.drawable.cloudy);
}
});
}
//partly cloudy
else if ( weatherCode.equals("44")) {
mHandler.post(new Runnable() {
@Override
public void run() {
image.setImageResource(R.drawable.cloudy);
}
});
}
//mostly cloudy (night)
else if ( weatherCode.equals("27")) {
mHandler.post(new Runnable() {
@Override
public void run() {
image.setImageResource(R.drawable.night_cloudy);
}
});
}
//partly cloudy (night)
else if ( weatherCode.equals("29")) {
mHandler.post(new Runnable() {
@Override
public void run() {
image.setImageResource(R.drawable.night_cloudy);
}
});
}
//mostly cloudy (day)
else if ( weatherCode.equals("28")) {
mHandler.post(new Runnable() {
@Override
public void run() {
image.setImageResource(R.drawable.day_cloudy);
}
});
}
//partly cloudy (day)
else if ( weatherCode.equals("30")) {
mHandler.post(new Runnable() {
@Override
public void run() {
image.setImageResource(R.drawable.day_cloudy);
}
});
}
//clear(night)
else if ( weatherCode.equals("31")) {
mHandler.post(new Runnable() {
@Override
public void run() {
image.setImageResource(R.drawable.moon);
}
});
}
//sunny
else {
mHandler.post(new Runnable() {
@Override
public void run() {
image.setImageResource(R.drawable.sunny);
}
});
}
myWeather.conditiontext = conditionNode.getAttributes()
.getNamedItem("text")
.getNodeValue()
.toString();
myWeather.conditiontemp = conditionNode.getAttributes()
.getNamedItem("temp")
.getNodeValue()
.toString();
return myWeather;
}
private Document convertStringToDocument(String src){
Document dest = null;
DocumentBuilderFactory dbFactory =
DocumentBuilderFactory.newInstance();
DocumentBuilder parser;
try {
parser = dbFactory.newDocumentBuilder();
dest = parser.parse(new ByteArrayInputStream(src.getBytes()));
} catch (ParserConfigurationException e1) {
e1.printStackTrace();
Toast.makeText(HomeActivity.this,
e1.toString(), Toast.LENGTH_LONG).show();
} catch (SAXException e) {
e.printStackTrace();
Toast.makeText(HomeActivity.this,
e.toString(), Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(HomeActivity.this,
e.toString(), Toast.LENGTH_LONG).show();
}
return dest;
}
private String QueryYahooWeather(){
String qResult = "";
String queryString = "http://weather.yahooapis.com/forecastrss?w=1062617&u=c";
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(queryString);
try {
HttpEntity httpEntity = httpClient.execute(httpGet).getEntity();
if (httpEntity != null){
InputStream inputStream = httpEntity.getContent();
Reader in = new InputStreamReader(inputStream);
BufferedReader bufferedreader = new BufferedReader(in);
StringBuilder stringBuilder = new StringBuilder();
String stringReadLine = null;
while ((stringReadLine = bufferedreader.readLine()) != null) {
stringBuilder.append(stringReadLine + "\n");
}
qResult = stringBuilder.toString();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
Toast.makeText(HomeActivity.this,
e.toString(), Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(HomeActivity.this,
e.toString(), Toast.LENGTH_LONG).show();
}
return qResult;
}
}
答案 0 :(得分:0)
在第135行,您应该检查psi.get(0)
是否为空。也许尝试实现它:
if (psi.get(0).length() != 0){
String temp = Html.fromHtml(psi.get(0)).toString();
} else {
String temp = "";}