所以,我现在正在学习Android,我需要帮助。我无法测试我的服务,因为连接API Twitter有错误。在这个项目中,我正在使用jtwitter.jar库。但是,LogCat捕获异常:
> 02-12 22:10:09.880 4533-4547/com.marakana.yamba W/dalvikvm: threadid=11: thread exiting with uncaught exception (group=0x41af0da0)
02-12 22:10:09.881 4533-4547/com.marakana.yamba E/AndroidRuntime: FATAL EXCEPTION: UpdaterService-Updater
02-12 22:10:09.881 4533-4547/com.marakana.yamba E/AndroidRuntime: Process: com.marakana.yamba, PID: 4533
02-12 22:10:09.881 4533-4547/com.marakana.yamba E/AndroidRuntime: java.lang.NullPointerException
02-12 22:10:09.881 4533-4547/com.marakana.yamba E/AndroidRuntime: at com.marakana.yamba.UpdaterService$Updater.run(UpdaterService.java:86)
02-12 22:10:07.737 4533-4533/com.marakana.yamba I/YambaApplication: onCreated
02-12 22:10:07.738 4533-4533/com.marakana.yamba D/UpdaterService: onCreated
02-12 22:10:07.739 4533-4533/com.marakana.yamba D/UpdaterService: onStarted
02-12 22:10:07.740 4533-4547/com.marakana.yamba D/UpdaterService: Updater running
02-12 22:10:09.880 4533-4547/com.marakana.yamba E/UpdaterService: Failed to connect to twitter service
我不知道为什么服务不起作用。我有jtwitter-yamba.jar作为库。但是,在LogCat中,错误在上(Twitter.Status状态:时间轴)。这是我的代码之一:
UpdaterService.java
package com.marakana.yamba;
`import android.app.Service;`
`import android.content.Intent;`
`import android.os.IBinder;`
`import android.support.annotation.Nullable;`
`import android.util.Log;`
`import java.util.List;`
`import winterwell.jtwitter.Twitter;`
`import winterwell.jtwitter.TwitterException;`
public class UpdaterService extends Service {
static final String TAG = "UpdaterService";
static final int DELAY = 6000;//a minute
private boolean runFlag = false;
private Updater updater;
private YambaApplication yamba;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate(){
super.onCreate();
this.yamba = (YambaApplication) getApplication();
this.updater = new Updater();
Log.d(TAG, "onCreated");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId){
super.onStartCommand(intent, flags, startId);
this.runFlag = true;
this.updater.start();
this.yamba.setServiceRunning(true);
Log.d(TAG, "onStarted");
return START_STICKY;
}
@Override
public void onDestroy(){
super.onDestroy();
this.runFlag= false;
this.updater.interrupt();
this.updater = null;
this.yamba.setServiceRunning(false);
Log.d(TAG, "onDestroyed");
}
/**
* Thread that performs the actual update from the online service
*/
private class Updater extends Thread{
List<Twitter.Status> timeline;
public Updater(){
super("UpdaterService-Updater");
}
@Override
public void run(){
UpdaterService updaterService = UpdaterService.this;
while(updaterService.runFlag){
Log.d(TAG, "Updater running");
try {
//Some work goes here...
//Get the timeline from the cloud
try{
timeline = yamba.getTwitter().getFriendsTimeline();
}catch (TwitterException e){
Log.e(TAG, "Failed to connect to twitter service", e);
}
//Loop over the timeline and print it out
for (Twitter.Status status : timeline){
Log.d(TAG, String.format("%s: %s,", status.user.name, status.text));
}
Log.d(TAG, "Updater ran");
Thread.sleep(DELAY);
}catch (InterruptedException e){
updaterService.runFlag = false;
}
}
}
}//Updater
}