[英]Launching an Activity from a Service
当我尝试从Service启动呼叫活动时,收到NullPointerException
。 这是我的代码:
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + number));
callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(callIntent);
我在startActivity
行上遇到了异常。
我尝试使用getApplication.startActivity
和getApplicationContext.startActivity
但是没有运气。
有任何想法吗?
编辑 :也许一些有用的信息:我正在尝试创建一个将在后台运行并扫描传感器数据的服务,当给出特定信号时,我想自动拨打一个号码。
编辑 :完整的adb错误代码:
03-31 09:04:10.214: ERROR/AndroidRuntime(1896): Uncaught handler: thread main exiting due to uncaught exception
03-31 09:04:10.226: ERROR/AndroidRuntime(1896): java.lang.RuntimeException: Unable to instantiate service dfz.epilepsiedetector.services.DetectionService: java.lang.NullPointerException
03-31 09:04:10.226: ERROR/AndroidRuntime(1896): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2668)
03-31 09:04:10.226: ERROR/AndroidRuntime(1896): at android.app.ActivityThread.access$3100(ActivityThread.java:116)
03-31 09:04:10.226: ERROR/AndroidRuntime(1896): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1846)
03-31 09:04:10.226: ERROR/AndroidRuntime(1896): at android.os.Handler.dispatchMessage(Handler.java:99)
03-31 09:04:10.226: ERROR/AndroidRuntime(1896): at android.os.Looper.loop(Looper.java:123)
03-31 09:04:10.226: ERROR/AndroidRuntime(1896): at android.app.ActivityThread.main(ActivityThread.java:4203)
03-31 09:04:10.226: ERROR/AndroidRuntime(1896): at java.lang.reflect.Method.invokeNative(Native Method)
03-31 09:04:10.226: ERROR/AndroidRuntime(1896): at java.lang.reflect.Method.invoke(Method.java:521)
03-31 09:04:10.226: ERROR/AndroidRuntime(1896): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
03-31 09:04:10.226: ERROR/AndroidRuntime(1896): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
03-31 09:04:10.226: ERROR/AndroidRuntime(1896): at dalvik.system.NativeStart.main(Native Method)
03-31 09:04:10.226: ERROR/AndroidRuntime(1896): Caused by: java.lang.NullPointerException
03-31 09:04:10.226: ERROR/AndroidRuntime(1896): at android.content.ContextWrapper.getPackageName(ContextWrapper.java:120)
03-31 09:04:10.226: ERROR/AndroidRuntime(1896): at android.content.ComponentName.<init>(ComponentName.java:75)
03-31 09:04:10.226: ERROR/AndroidRuntime(1896): at android.content.Intent.<init>(Intent.java:2302)
03-31 09:04:10.226: ERROR/AndroidRuntime(1896): at dfz.epilepsiedetector.services.DetectionService.<init>(DetectionService.java:35)
03-31 09:04:10.226: ERROR/AndroidRuntime(1896): at java.lang.Class.newInstanceImpl(Native Method)
03-31 09:04:10.226: ERROR/AndroidRuntime(1896): at java.lang.Class.newInstance(Class.java:1472)
03-31 09:04:10.226: ERROR/AndroidRuntime(1896): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2665)
编辑修剪的类代码:
public class DetectionService extends IntentService implements SensorEventListener
{
private SensorManager mSensorManager;
private Sensor mAccelerometer;
private boolean hasSeizure = false;
private final int POLLS_PER_SECOND = 10;
public DetectionService()
{
super("EpilepsionDetectionService");
Intent callIntent = new Intent(DetectionService.this,
InformationActivity.class);
callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(callIntent);
}
您必须从调用服务的活动中获取上下文。
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + number));
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(callIntent);
编辑 :
callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(callIntent);`
`
希望能帮助到你...
你可以试试:
getBaseContext().startActivity(callIntent);
或使用:
this.startActivity(callIntent);
备查:
当直接从Service而不是Intent服务中安装时,问题消失了。
希望对您有所帮助!
将代码移出构造函数,并移至onCreate()
方法中。 总而言之,该类尚未完全初始化,并导致NullPointerException
。
在服务的onStart
方法而不是onCreate
方法中调用意图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.