我想将 java 类链接到我的 xml 文件。然而,编码中没有错误,它给了我关闭的力量。我想知道它有什么问题。谁能给我建议?
我有一个主要的 java 类,具有与此相同的代码,因为我想为两个 xml 布局显示相同的内容。主要的 java 类工作得很好,但是,当我尝试将编码转换为第二个 java 类时,会发生强制关闭错误。
这是我的编码。
public class event extends ListActivity{
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
conditiontext + "\n" + " " + conditiontemp + "°C" ;
}
}
String[] Category = {
"Scientist for a day",
"Science Trail",
"Megalog Return"
};
String [] dates = {
"Today",
"Tomorrow",
"This Week"
};
Spinner s1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.event);
weather = (TextView)findViewById(R.id.weather);
image = (ImageView)findViewById(R.id.image);
psi = new ArrayList<String>();
psi_text = (TextView) findViewById(R.id.psi_text);
TabHost th =(TabHost)findViewById(R.id.tabhost);
th.setup();
TabSpec specs = th.newTabSpec("tag1");
specs.setContent(R.id.tab1);
specs.setIndicator("Suggested");
th.addTab(specs);
specs = th.newTabSpec("tag2");
specs.setContent(R.id.tab2);
specs.setIndicator("All");
th.addTab(specs);
TabWidget tw = (TabWidget) th.findViewById(android.R.id.tabs);
View tab1 = tw.getChildTabViewAt(0);
TextView tv = (TextView) tab1.findViewById(android.R.id.title);
tv.setTextSize(15);
tv.setPadding(0, 0, 0, 50);
View tab2 = tw.getChildTabViewAt(1);
TextView tv1 = (TextView) tab2.findViewById(android.R.id.title);
tv1.setTextSize(15);
tv1.setPadding(0, 0, 0, 50);
//GridView
setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,Category));
//SpinnerView
s1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, dates);
s1.setAdapter(adapter);
s1.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0,View arg1, int arg2, long arg3) {
int index = s1.getSelectedItemPosition();
Toast.makeText(getBaseContext(), "You have seleted item :" + dates[index] , Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?>arg0) {}
});
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();
}
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(event.this,
e1.toString(), Toast.LENGTH_LONG).show();
} catch (SAXException e) {
e.printStackTrace();
Toast.makeText(event.this,
e.toString(), Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(event.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(event.this,
e.toString(), Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(event.this,
e.toString(), Toast.LENGTH_LONG).show();
}
return qResult;
}
public void onListItemClick(ListView parent, View v, int position,long id)
{
Toast.makeText(this, "You have selected " + Category[position], Toast.LENGTH_SHORT).show();
}
}