0

I have a service, an instance of which is passed in as a parameter to the constructor of another object. I then call the startActivity of that context instance. When doing so, my application force closes due to an exception. It doesn't event get caught in my try catch blocks.

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);
    }
}

The output from log ca is as follows...

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

What am I doing wrong ?

Heshan Perera
  • 4,592
  • 8
  • 44
  • 57
  • I second Akki's question. I'm not quite sure what you mean. You are trying to start a service via startActivity? It sounds to me like you need context.startService() instead of startActivity – Joey Roosing May 23 '12 at 14:11
  • sorry, how about now ? This is just an example of what i want to do. – Heshan Perera May 23 '12 at 14:19
  • [This seems to be the same problem](http://stackoverflow.com/questions/3606596/android-start-activity-from-service) - can you try that out? – Ken Y-N Jun 06 '12 at 08:03

0 Answers0