0
我在使用線程時出現問題。我有他的服務班,我需要檢查更新,然後睡了24小時。Android線程問題Thread-387
public class SimpleService extends Service {
private static final int NOVI_VESTI = 1;
private static final int NOVA_OGLASNA = 2;
private List<String> titles;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Thread t = new Thread() {
@Override
public void run() {
while (true) {
try {
String vesti, oglasna;
vesti = readRss("http://www.finki.ukim.mk/mk/rss/news");
// readRss("http://www.finki.ukim.mk/mk/rss/news");
// getPrefs("vesti");
if (!vesti.equals(getPrefs("vesti"))) {
Context context = SimpleService.this;
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(NOTIFICATION_SERVICE);
Notification updateComplete = new Notification();
updateComplete.icon = R.drawable.ic_launcher;
updateComplete.tickerText = context
.getText(R.string.newVesti);
updateComplete.when = System.currentTimeMillis();
Intent notificationIntent = new Intent(context,
Vesti.class);
PendingIntent contentIntent = PendingIntent
.getActivity(context, 0,
notificationIntent, 0);
String contentTitle = context.getText(
R.string.newVesti).toString();
String contentText;
contentText = vesti.toString();
updateComplete.setLatestEventInfo(context,
contentTitle, contentText, contentIntent);
notificationManager.notify(NOVI_VESTI,
updateComplete);
}
oglasna = readRss("http://www.finki.ukim.mk/mk/rss/announcements");
if (!oglasna.equals(getPrefs("oglasna"))) {
Context context = SimpleService.this;
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(NOTIFICATION_SERVICE);
Notification updateComplete = new Notification();
updateComplete.icon = R.drawable.ic_launcher;
updateComplete.tickerText = context
.getText(R.string.newOglasna);
updateComplete.when = System.currentTimeMillis();
Intent notificationIntent = new Intent(context,
OglasnaTabla.class);
PendingIntent contentIntent = PendingIntent
.getActivity(context, 0,
notificationIntent, 0);
String contentTitle = context.getText(
R.string.newOglasna).toString();
String contentText;
contentText = vesti.toString();
updateComplete.setLatestEventInfo(context,
contentTitle, contentText, contentIntent);
notificationManager.notify(NOVA_OGLASNA,
updateComplete);
sleep(60 * 60 * 24);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
t.start();
}
@Override
public void onDestroy() {
super.onDestroy();
}
// title for both
public String readRss(String feedLink) {
try {
URL url = new URL(feedLink);
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp = factory.newPullParser();
// We will get the XML from an input stream
xpp.setInput(getInputStream(url), "UTF_8");
boolean insideItem = false;
// Returns the type of current event: START_TAG, END_TAG, etc..
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
if (xpp.getName().equalsIgnoreCase("item")) {
insideItem = true;
} else if (xpp.getName().equalsIgnoreCase("title")) {
if (insideItem)
titles.add(xpp.nextText());
// headlines.add(xpp.nextText()); // extract the
}
} else if (eventType == XmlPullParser.END_TAG
&& xpp.getName().equalsIgnoreCase("item")) {
insideItem = false;
}
eventType = xpp.next(); // move to next element
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return titles.get(0);
}
public InputStream getInputStream(URL url) {
try {
return url.openConnection().getInputStream();
} catch (IOException e) {
return null;
}
}
private String getPrefs(String category) {
SharedPreferences preferences = PreferenceManager
.getDefaultSharedPreferences(this);
String pref = preferences.getString(category, "");
return pref;
}
}
但是,當我嘗試在DDMS運行此我得到以下日誌:
03-01 14:46:26.913: E/AndroidRuntime(3308): FATAL EXCEPTION: Thread-387
03-01 14:46:26.913: E/AndroidRuntime(3308): java.lang.NullPointerException
03-01 14:46:26.913: E/AndroidRuntime(3308): at com.finki.darko.mk.services.SimpleService.readRss(SimpleService.java:146)
03-01 14:46:26.913: E/AndroidRuntime(3308): at com.finki.darko.mk.services.SimpleService$1.run(SimpleService.java:47)
有誰有一個想法如何解決這個問題?