我'我是android和java的完全新手,我正在用android studio编写一个Android应用程序,应该每5秒在地图上显示一些点(它只是一个测试)。当我在模拟器上运行应用程序时,我在logcat上得到了这个:
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是
public class MenuActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
Intent mServiceIntent = new Intent(this, DataService.class);
this.startService(mServiceIntent);
}
public void goToMap(View view){
Intent intent = new Intent(this, MapActivity.class);
startActivity(intent);
}
public void goToData(View view){
Intent intent = new Intent(this, ShowDataActivity.class);
startActivity(intent);
}
}
我做错了什么?
答案 0 :(得分:4)
你不能拥有这个
BroadcastNotifier = LocalBroadcastManager.getInstance(this);
在你的构造函数中。将其放入onHandleIntent()
this
未在您的构造函数中完全初始化,并且无法获取Context
,这就是为什么它是null
并且您获得该NPE。
答案 1 :(得分:0)
您应该只使用super
调用留下构造函数,并使用onCreate
生命周期回调方法。