启动服务时强制关闭
我有一项活动,我希望通过服务对其进行更新。当我尝试启动服务时,出现强制关闭错误。
LOGCAT
12-13 15:26:24.574: E/AndroidRuntime(13741): FATAL EXCEPTION: main
12-13 15:26:24.574: E/AndroidRuntime(13741): java.lang.RuntimeException: Unable to instantiate service www.freshapp.com.wherewhen.html.SetTime: java.lang.NullPointerException
12-13 15:26:24.574: E/AndroidRuntime(13741): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2005)
12-13 15:26:24.574: E/AndroidRuntime(13741): at android.app.ActivityThread.access$2500(ActivityThread.java:124)
12-13 15:26:24.574: E/AndroidRuntime(13741): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1022)
12-13 15:26:24.574: E/AndroidRuntime(13741): at android.os.Handler.dispatchMessage(Handler.java:99)
12-13 15:26:24.574: E/AndroidRuntime(13741): at android.os.Looper.loop(Looper.java:123)
12-13 15:26:24.574: E/AndroidRuntime(13741): at android.app.ActivityThread.main(ActivityThread.java:3806)
12-13 15:26:24.574: E/AndroidRuntime(13741): at java.lang.reflect.Method.invokeNative(Native Method)
12-13 15:26:24.574: E/AndroidRuntime(13741): at java.lang.reflect.Method.invoke(Method.java:507)
12-13 15:26:24.574: E/AndroidRuntime(13741): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-13 15:26:24.574: E/AndroidRuntime(13741): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-13 15:26:24.574: E/AndroidRuntime(13741): at dalvik.system.NativeStart.main(Native Method)
12-13 15:26:24.574: E/AndroidRuntime(13741): Caused by: java.lang.NullPointerException
12-13 15:26:24.574: E/AndroidRuntime(13741): at www.freshapp.com.wherewhen.html.SetTime.<init>(SetTime.java:21)
12-13 15:26:24.574: E/AndroidRuntime(13741): at java.lang.Class.newInstanceImpl(Native Method)
12-13 15:26:24.574: E/AndroidRuntime(13741): at java.lang.Class.newInstance(Class.java:1409)
12-13 15:26:24.574: E/AndroidRuntime(13741): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2002)
服务类别
public class SetTime extends Service {
private Handler hanl;
private Context context;
LayoutInflater inflater = (LayoutInflater)context.getSystemService(LAYOUT_INFLATER_SERVICE);
View name = inflater.inflate(R.id.editName, null);
View lon = inflater.inflate(R.id.editName, null);
View lat = inflater.inflate(R.id.editName, null);
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate(){
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
final LocationManager mlocManager =
(LocationManager) getSystemService(Context.LOCATION_SERVICE);
final LocationListener mlocListener = new LocationHelper();
Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
final Runnable r = new Runnable(){
public void run(){
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
mlocListener);
((TextView) lat).setText(""+ LocationHelper.getLatitude());
((TextView) lon).setText("" + LocationHelper.getLongitude());
hanl.postDelayed(this, 1000);
//String date = s.format(new Date());
String mName = ((EditText) name).getText().toString();
String mLat = ((EditText) lat).getText().toString();
String mLon = ((EditText) lon).getText().toString();
}
};
hanl.postDelayed(r, 1000);
// If we get killed, after returning from here, restart
return START_STICKY;
}
}
LINE 21
LayoutInflater inflater = (LayoutInflater)context.getSystemService(LAYOUT_INFLATER_SERVICE);
I have an activity and I want it to be updated from a service. when I try to start the service I get a force close error.
LOGCAT
12-13 15:26:24.574: E/AndroidRuntime(13741): FATAL EXCEPTION: main
12-13 15:26:24.574: E/AndroidRuntime(13741): java.lang.RuntimeException: Unable to instantiate service www.freshapp.com.wherewhen.html.SetTime: java.lang.NullPointerException
12-13 15:26:24.574: E/AndroidRuntime(13741): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2005)
12-13 15:26:24.574: E/AndroidRuntime(13741): at android.app.ActivityThread.access$2500(ActivityThread.java:124)
12-13 15:26:24.574: E/AndroidRuntime(13741): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1022)
12-13 15:26:24.574: E/AndroidRuntime(13741): at android.os.Handler.dispatchMessage(Handler.java:99)
12-13 15:26:24.574: E/AndroidRuntime(13741): at android.os.Looper.loop(Looper.java:123)
12-13 15:26:24.574: E/AndroidRuntime(13741): at android.app.ActivityThread.main(ActivityThread.java:3806)
12-13 15:26:24.574: E/AndroidRuntime(13741): at java.lang.reflect.Method.invokeNative(Native Method)
12-13 15:26:24.574: E/AndroidRuntime(13741): at java.lang.reflect.Method.invoke(Method.java:507)
12-13 15:26:24.574: E/AndroidRuntime(13741): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-13 15:26:24.574: E/AndroidRuntime(13741): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-13 15:26:24.574: E/AndroidRuntime(13741): at dalvik.system.NativeStart.main(Native Method)
12-13 15:26:24.574: E/AndroidRuntime(13741): Caused by: java.lang.NullPointerException
12-13 15:26:24.574: E/AndroidRuntime(13741): at www.freshapp.com.wherewhen.html.SetTime.<init>(SetTime.java:21)
12-13 15:26:24.574: E/AndroidRuntime(13741): at java.lang.Class.newInstanceImpl(Native Method)
12-13 15:26:24.574: E/AndroidRuntime(13741): at java.lang.Class.newInstance(Class.java:1409)
12-13 15:26:24.574: E/AndroidRuntime(13741): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2002)
SERVICE CLASS
public class SetTime extends Service {
private Handler hanl;
private Context context;
LayoutInflater inflater = (LayoutInflater)context.getSystemService(LAYOUT_INFLATER_SERVICE);
View name = inflater.inflate(R.id.editName, null);
View lon = inflater.inflate(R.id.editName, null);
View lat = inflater.inflate(R.id.editName, null);
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate(){
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
final LocationManager mlocManager =
(LocationManager) getSystemService(Context.LOCATION_SERVICE);
final LocationListener mlocListener = new LocationHelper();
Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
final Runnable r = new Runnable(){
public void run(){
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
mlocListener);
((TextView) lat).setText(""+ LocationHelper.getLatitude());
((TextView) lon).setText("" + LocationHelper.getLongitude());
hanl.postDelayed(this, 1000);
//String date = s.format(new Date());
String mName = ((EditText) name).getText().toString();
String mLat = ((EditText) lat).getText().toString();
String mLon = ((EditText) lon).getText().toString();
}
};
hanl.postDelayed(r, 1000);
// If we get killed, after returning from here, restart
return START_STICKY;
}
}
LINE 21
LayoutInflater inflater = (LayoutInflater)context.getSystemService(LAYOUT_INFLATER_SERVICE);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在尝试在静态初始值设定项中创建 LayoutInflater。这是行不通的。从战术上讲,它不会起作用,因为您尝试在
context
上调用getSystemService()
,而此时该上下文为null
。此外,
Service
没有使用LayoutInflater
,因此您应该删除第 21 行以及引用它的所有内容,例如所有EditText
小部件你不能使用的。或者,将此Service
转换为Activity
,因为Activity
有 UI,而Service
没有。You are trying to create a
LayoutInflater
in a static initializer. This will not work. Tactically, it will not work because you are trying to callgetSystemService()
oncontext
, which isnull
at this point in time.Moreover, a
Service
has no use for aLayoutInflater
, so you should delete line 21 and everything that references it, such as all theEditText
widgets that you cannot use. Or, convert thisService
into anActivity
, as anActivity
has a UI, while aService
does not.