Android:从作为参数传入的Context实例调用startActivity

时间:2012-05-23 14:09:38

标签: android service android-activity

我有一个服务,其实例作为参数传递给另一个对象的构造函数。然后我调用该上下文实例的startActivity。这样做时,我的应用程序因异常而关闭。它没有被我的try catch块捕获。

public class TestService extends Service{
    public int onStartCommand(Intent intent, int flags, int startId){
       TestClass test = new Test(this);
    }

}


public class TestClass{
    private TestService testService;

    public TestClass(TestService testService){
      this.testService = testService;
    }

    public void startAct(){
      Intent i = new Intent(Settings.ATION_WIFI_SETTINGS);
      i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      testService.startActivity(i);
    }
}

log ca的输出如下......

05-23 19:35:17.570: E/AndroidRuntime(15877): FATAL EXCEPTION: main
05-23 19:35:17.570: E/AndroidRuntime(15877): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com/com.facade.ServicesFacade$ShuntActivity}: java.lang.InstantiationException: com.facade.ServicesFacade$ShuntActivity
05-23 19:35:17.570: E/AndroidRuntime(15877):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2709)
05-23 19:35:17.570: E/AndroidRuntime(15877):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)
05-23 19:35:17.570: E/AndroidRuntime(15877):    at android.app.ActivityThread.access$2300(ActivityThread.java:135)
05-23 19:35:17.570: E/AndroidRuntime(15877):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136)
05-23 19:35:17.570: E/AndroidRuntime(15877):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-23 19:35:17.570: E/AndroidRuntime(15877):    at android.os.Looper.loop(Looper.java:144)
05-23 19:35:17.570: E/AndroidRuntime(15877):    at android.app.ActivityThread.main(ActivityThread.java:4937)
05-23 19:35:17.570: E/AndroidRuntime(15877):    at java.lang.reflect.Method.invokeNative(Native Method)
05-23 19:35:17.570: E/AndroidRuntime(15877):    at java.lang.reflect.Method.invoke(Method.java:521)
05-23 19:35:17.570: E/AndroidRuntime(15877):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-23 19:35:17.570: E/AndroidRuntime(15877):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-23 19:35:17.570: E/AndroidRuntime(15877):    at dalvik.system.NativeStart.main(Native Method)
05-23 19:35:17.570: E/AndroidRuntime(15877): Caused by: java.lang.InstantiationException: com.facade.ServicesFacade$ShuntActivity
05-23 19:35:17.570: E/AndroidRuntime(15877):    at java.lang.Class.newInstanceImpl(Native Method)
05-23 19:35:17.570: E/AndroidRuntime(15877):    at java.lang.Class.newInstance(Class.java:1429)
05-23 19:35:17.570: E/AndroidRuntime(15877):    at android.app.Instrumentation.newActivity(Instrumentation.java:1036)
05-23 19:35:17.570: E/AndroidRuntime(15877):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2701)
05-23 19:35:17.570: E/AndroidRuntime(15877):    ... 11 more

我做错了什么?

0 个答案:

没有答案