我是一個完整的newcome android和java和我正在寫android應用程序與android工作室,應顯示在地圖上的每個5秒點(這只是一個測試)。當我運行模擬器上的應用程序,我得到這個在logcat的:java.lang.RuntimeException:無法實例化服務:java.lang.NullPointerException
03-13 13:22:28.373 2964-2964/com.example.francesca.geoapp
E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.francesca.geoapp, PID: 2964
java.lang.RuntimeException: Unable to instantiate service com.example.francesca.geoapp.DataService: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2716)
at android.app.ActivityThread.access$1800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1361)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:105)
at android.support.v4.content.LocalBroadcastManager.getInstance(LocalBroadcastManag er.java:102) at com.example.francesca.geoapp.DataService.<init> (DataService.java:56)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1572)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2713)
at android.app.ActivityThread.access$1800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1361)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
的intentService DataService的代碼是
public class DataService extends IntentService {
private LocalBroadcastManager BroadcastNotifier ;
private Timer timer = new Timer();
private List dataList = new ArrayList<testData>();
int i = 0;
//constructor
public DataService() {
super("DataService");
dataList.add(new testData(new LatLng(52.519804, 13.404988), new LatLng(52.519125, 13.406061), 50, 30));
dataList.add(new testData(new LatLng(52.520692, 13.403722), new LatLng(52.519804, 13.404988), 60, 40));
dataList.add(new testData(new LatLng(52.522023, 13.404730), new LatLng(52.520692, 13.403722), 50, 30));
dataList.add(new testData(new LatLng(52.523388, 13.407143), new LatLng(52.522023, 13.404730), 40, 20));
dataList.add(new testData(new LatLng(52.522911, 13.408807), new LatLng(52.523388, 13.407143), 80, 30));
dataList.add(new testData(new LatLng(52.523982, 13.409623), new LatLng(52.522911, 13.408807), 50, 40));
dataList.add(new testData(new LatLng(52.524713, 13.407434), new LatLng(52.523982, 13.409623), 30, 50));
BroadcastNotifier = LocalBroadcastManager.getInstance(this);
timer.scheduleAtFixedRate(task, 0, 5000);
}
protected void onHandleIntent(Intent i) {
}
//timerTask to send request to the webservice
TimerTask task = new TimerTask() {
@Override
public void run() {
if(i <7) {
testData test = (testData) dataList.get(i);
data.setCurrentPosition(test.currentPosition);
data.setLatestPosition(test.latestPosition);
data.setSpeed(test.speed);
data.setMediumSpeed(test.mediumSpeed);
i++;
}
}
};
//method to tell subscribers that a newData has arrived
private void OnNewDataSeen() {
Intent localIntent = new Intent();
localIntent.setAction("com.example.android.threadsample.BROADCAST");
localIntent.addCategory(Intent.CATEGORY_DEFAULT);
// Broadcasts the Intent
BroadcastNotifier.sendBroadcast(localIntent);
}
}
,並應啓動intentService的MenuActivity被
}
我做錯了什麼?
問了很多次,你正試圖在上下文創建之前使用上下文(在構造函數中) – Selvin 2015-03-13 14:26:28