Start activity from service isn't working (Android)

12,286

See android start activity from service

Intent i= new Intent(getBaseContext(), screen.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(i);

You error seems to be inside screen activity. There are many thread which might help to figure out the error that you are getting for the activity:

Error in Android "SuperNotCalledException:Activity did not call through to super.OnCreate()"

android.app.SuperNotCalledException: Activity did not call through to super.onStop()

Update

The error is because you haven't called: super.onCreate(savedInstanceState); in your screen activity's onCreate(). That should be the first thing to be called in onCreate(). Do something like this:

public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  //.... other stuff
}

Hope this helps.

Share:
12,286
Pedro Fraga
Author by

Pedro Fraga

Updated on June 14, 2022

Comments

  • Pedro Fraga
    Pedro Fraga almost 2 years

    I created an overlay "always on top button", which is a service HUD, and I can't start an activity screen from there, it gives the error: "Unfortunately App has stopped". In the beginning, all I used to know if there was any TouchEventwas a toast, and that toast was created, but it was created several times, so I don't know if it gives that error because this code, which is on TouchEvent body , is repeated several times too. here is my code:

    public class HUD extends Service implements OnClickListener, OnTouchListener, OnLongClickListener {
    Button mButton;
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    
    @Override
    public void onCreate() {
        super.onCreate();
        //mView = new HUDView(this);
        
        
        mButton = new Button(this);
        mButton.setId(1);
        mButton.setText("Button");
        mButton.setClickable(true);
        mButton.setOnTouchListener(this);
        
        
        WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                        WindowManager.LayoutParams.WRAP_CONTENT,
                        WindowManager.LayoutParams.TYPE_PHONE,
                        WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
                        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, 
                        PixelFormat.OPAQUE);
    
        params.gravity = Gravity.LEFT | Gravity.TOP;
        params.setTitle("Load Average");
        WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
        wm.addView(mButton, params);
        
        
    }
    
    @Override
    public void onDestroy() {
        super.onDestroy();
        if(mButton != null)
        {
            ((WindowManager) getSystemService(WINDOW_SERVICE)).removeView(mButton);
            mButton = null;
        }
    }
    
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if(event.getX()<mButton.getWidth() & event.getY()>0)
        {
        Toast.makeText(this,"Overlay button event", Toast.LENGTH_SHORT).show(); //this my toast
        Intent i = new Intent();                                                //this is my new acivity (intent)
        i.setClass(HUD.this, screen.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(i);
        HUD.this.stopSelf();
        }
        return false;
    }
    
    
    @Override
    public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub
        Toast.makeText(this,"Click", Toast.LENGTH_SHORT).show();
    }
    
    @Override
    public boolean onLongClick(View v) {
        // TODO Auto-generated method stub
        System.exit(1);
        return false;
    }
    
    }
    

    So my question is, is this code on TouchEvent body being repeated several times? If it is, is that the cause of the error?

    log cat:

    07-20 22:11:06.962: I/Choreographer(1620): Skipped 52 frames!  The application may be doing too much work on      its main thread.
    07-20 22:11:08.062: D/AndroidRuntime(1620): Shutting down VM
    07-20 22:11:08.062: W/dalvikvm(1620): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
    07-20 22:11:08.132: E/AndroidRuntime(1620): FATAL EXCEPTION: main
    07-20 22:11:08.132: E/AndroidRuntime(1620): android.app.SuperNotCalledException: Activity    {com.example.screenshot/com.example.screenshot.screen} did not call through to super.onCreate()
    07-20 22:11:08.132: E/AndroidRuntime(1620):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2146)
    07-20 22:11:08.132: E/AndroidRuntime(1620):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
    07-20 22:11:08.132: E/AndroidRuntime(1620):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
    07-20 22:11:08.132: E/AndroidRuntime(1620):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
    07-20 22:11:08.132: E/AndroidRuntime(1620):     at android.os.Handler.dispatchMessage(Handler.java:99)
    07-20 22:11:08.132: E/AndroidRuntime(1620):     at android.os.Looper.loop(Looper.java:137)
    07-20 22:11:08.132: E/AndroidRuntime(1620):     at android.app.ActivityThread.main(ActivityThread.java:5041)
    07-20 22:11:08.132: E/AndroidRuntime(1620):     at java.lang.reflect.Method.invokeNative(Native Method)
    07-20 22:11:08.132: E/AndroidRuntime(1620):     at java.lang.reflect.Method.invoke(Method.java:511)
    07-20 22:11:08.132: E/AndroidRuntime(1620):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    07-20 22:11:08.132: E/AndroidRuntime(1620):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    07-20 22:11:08.132: E/AndroidRuntime(1620):     at dalvik.system.NativeStart.main(Native Method)
    

    screen.java:

    public class screen extends Activity {
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
    Toast.makeText(getApplicationContext(), "Made it", 0).show();
    finish();
    }
    }