Start Another Activity from onCreate() in MainActv

2020-04-06 01:25发布

问题:

The problem I am having is that the onCreate() method within my MainActivity cannot seem to start another activity.

I have code working so that when I click a button, my "AboutActivity" is started. However, I want to make it so that "AboutActivity" is started as soon as onCreate() in the MainActivity finishes.

The program gets caught up at a blank screen when I run it while trying to start the "AboutActivity" from onCreate().

The code posted is a simplified version of the problem. In essence, it is what I am trying to do.

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);    
        setContentView(R.layout.activity_main);

        // Fire the intent that launches the "About" screen.
        Intent aboutScreen = new Intent(getBaseContext(), AboutActivity.class);
        this.startActivity(aboutScreen);
    }

Code for the AboutActivity class:

public class AboutActivity extends MainActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about);
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return super.onCreateOptionsMenu(menu);
    }
}

The LogCat messages are:

11-16 16:33:47.335: D/dalvikvm(1702): GC_FOR_ALLOC freed 65K, 7% free 2692K/2876K, paused 70ms, total 72ms
11-16 16:33:47.335: I/dalvikvm-heap(1702): Grow heap (frag case) to 3.347MB for 635812-byte allocation
11-16 16:33:47.435: D/dalvikvm(1702): GC_FOR_ALLOC freed 2K, 6% free 3310K/3500K, paused 98ms, total 98ms
11-16 16:33:47.715: D/dalvikvm(1702): GC_FOR_ALLOC freed 20K, 4% free 3469K/3608K, paused 28ms, total 29ms
11-16 16:33:47.735: I/dalvikvm-heap(1702): Grow heap (frag case) to 5.478MB for 2073616-byte allocation
11-16 16:33:47.845: D/dalvikvm(1702): GC_FOR_ALLOC freed 1K, 3% free 5493K/5636K, paused 112ms, total 112ms
11-16 16:33:48.175: I/Choreographer(1702): Skipped 36 frames!  The application may be doing too much work on its main thread.
11-16 16:33:48.465: D/libEGL(1702): loaded /system/lib/egl/libEGL_emulation.so
11-16 16:33:48.555: D/(1702): HostConnection::get() New Host Connection established 0x2a210280, tid 1702
11-16 16:33:48.604: D/libEGL(1702): loaded /system/lib/egl/libGLESv1_CM_emulation.so
11-16 16:33:48.615: D/libEGL(1702): loaded /system/lib/egl/libGLESv2_emulation.so
11-16 16:33:48.715: W/EGL_emulation(1702): eglSurfaceAttrib not implemented
11-16 16:33:48.725: D/OpenGLRenderer(1702): Enabling debug mode 0
11-16 16:33:51.794: D/dalvikvm(1702): GC_FOR_ALLOC freed 139K, 4% free 6352K/6608K, paused 38ms, total 46ms
11-16 16:33:53.135: D/dalvikvm(1702): GREF has increased to 201
11-16 16:33:55.315: D/dalvikvm(1702): GC_FOR_ALLOC freed 219K, 5% free 7420K/7756K, paused 58ms, total 64ms
11-16 16:34:00.825: D/dalvikvm(1702): GC_FOR_ALLOC freed 281K, 5% free 8778K/9176K, paused 131ms, total 133ms
11-16 16:34:00.955: W/EGL_emulation(1702): eglSurfaceAttrib not implemented
11-16 16:34:01.405: I/Choreographer(1702): Skipped 31 frames!  The application may be doing too much work on its main thread.
11-16 16:34:01.635: W/EGL_emulation(1702): eglSurfaceAttrib not implemented
11-16 16:34:02.295: W/EGL_emulation(1702): eglSurfaceAttrib not implemented
11-16 16:34:03.026: W/EGL_emulation(1702): eglSurfaceAttrib not implemented
11-16 16:34:03.625: W/EGL_emulation(1702): eglSurfaceAttrib not implemented
11-16 16:34:04.246: W/EGL_emulation(1702): eglSurfaceAttrib not implemented
11-16 16:34:04.875: W/EGL_emulation(1702): eglSurfaceAttrib not implemented
11-16 16:34:05.557: W/EGL_emulation(1702): eglSurfaceAttrib not implemented
11-16 16:34:06.206: W/EGL_emulation(1702): eglSurfaceAttrib not implemented
11-16 16:34:06.675: D/dalvikvm(1702): GREF has increased to 301
11-16 16:34:06.816: W/EGL_emulation(1702): eglSurfaceAttrib not implemented
11-16 16:34:07.458: W/EGL_emulation(1702): eglSurfaceAttrib not implemented
11-16 16:34:07.978: I/Choreographer(1702): Skipped 30 frames!  The application may be doing too much work on its main thread.
11-16 16:34:08.296: W/EGL_emulation(1702): eglSurfaceAttrib not implemented
11-16 16:34:08.975: W/EGL_emulation(1702): eglSurfaceAttrib not implemented
11-16 16:34:09.686: W/EGL_emulation(1702): eglSurfaceAttrib not implemented
11-16 16:34:10.315: W/EGL_emulation(1702): eglSurfaceAttrib not implemented
11-16 16:34:10.945: W/EGL_emulation(1702): eglSurfaceAttrib not implemented
11-16 16:34:13.094: D/dalvikvm(1702): GC_FOR_ALLOC freed 367K, 5% free 10460K/10944K, paused 124ms, total 128ms
11-16 16:34:16.446: W/EGL_emulation(1702): eglSurfaceAttrib not implemented
11-16 16:34:16.965: I/Choreographer(1702): Skipped 30 frames!  The application may be doing too much work on its main thread.
11-16 16:34:17.125: W/EGL_emulation(1702): eglSurfaceAttrib not implemented
11-16 16:34:17.625: I/Choreographer(1702): Skipped 30 frames!  The application may be doing too much work on its main thread.
11-16 16:34:17.827: W/EGL_emulation(1702): eglSurfaceAttrib not implemented
11-16 16:34:18.355: I/Choreographer(1702): Skipped 32 frames!  The application may be doing too much work on its main thread.
11-16 16:34:18.555: W/EGL_emulation(1702): eglSurfaceAttrib not implemented
11-16 16:34:19.145: I/Choreographer(1702): Skipped 34 frames!  The application may be doing too much work on its main thread.
11-16 16:34:19.358: W/EGL_emulation(1702): eglSurfaceAttrib not implemented
11-16 16:34:20.103: W/EGL_emulation(1702): eglSurfaceAttrib not implemented
11-16 16:34:20.615: I/Choreographer(1702): Skipped 34 frames!  The application may be doing too much work on its main thread.
11-16 16:34:20.880: W/EGL_emulation(1702): eglSurfaceAttrib not implemented
11-16 16:34:22.825: W/EGL_emulation(1702): eglSurfaceAttrib not implemented
11-16 16:34:23.445: I/Choreographer(1702): Skipped 35 frames!  The application may be doing too much work on its main thread.
11-16 16:34:23.675: W/EGL_emulation(1702): eglSurfaceAttrib not implemented
11-16 16:34:24.195: D/dalvikvm(1702): GC_FOR_ALLOC freed 381K, 4% free 12129K/12628K, paused 289ms, total 291ms
11-16 16:34:24.355: I/Choreographer(1702): Skipped 40 frames!  The application may be doing too much work on its main thread.
11-16 16:34:24.585: W/EGL_emulation(1702): eglSurfaceAttrib not implemented
11-16 16:34:25.215: I/Choreographer(1702): Skipped 36 frames!  The application may be doing too much work on its main thread.
11-16 16:34:25.496: W/EGL_emulation(1702): eglSurfaceAttrib not implemented
11-16 16:34:26.025: I/Choreographer(1702): Skipped 30 frames!  The application may be doing too much work on its main thread.
11-16 16:34:26.235: W/EGL_emulation(1702): eglSurfaceAttrib not implemented
11-16 16:34:26.735: D/dalvikvm(1702): GREF has increased to 401
11-16 16:34:26.795: I/Choreographer(1702): Skipped 32 frames!  The application may be doing too much work on its main thread.
11-16 16:34:27.006: W/EGL_emulation(1702): eglSurfaceAttrib not implemented
11-16 16:34:29.516: W/EGL_emulation(1702): eglSurfaceAttrib not implemented
11-16 16:34:30.075: I/Choreographer(1702): Skipped 37 frames!  The application may be doing too much work on its main thread.
11-16 16:34:30.305: W/EGL_emulation(1702): eglSurfaceAttrib not implemented
11-16 16:34:30.825: I/Choreographer(1702): Skipped 31 frames!  The application may be doing too much work on its main thread.
11-16 16:34:31.046: W/EGL_emulation(1702): eglSurfaceAttrib not implemented
11-16 16:34:31.595: I/Choreographer(1702): Skipped 32 frames!  The application may be doing too much work on its main thread.
11-16 16:34:31.816: W/EGL_emulation(1702): eglSurfaceAttrib not implemented
11-16 16:34:32.385: I/Choreographer(1702): Skipped 34 frames!  The application may be doing too much work on its main thread.
11-16 16:34:32.615: W/EGL_emulation(1702): eglSurfaceAttrib not implemented
11-16 16:34:33.145: I/Choreographer(1702): Skipped 31 frames!  The application may be doing too much work on its main thread.
11-16 16:34:33.365: W/EGL_emulation(1702): eglSurfaceAttrib not implemented
11-16 16:34:37.115: D/dalvikvm(1702): GC_FOR_ALLOC freed 392K, 4% free 13787K/14296K, paused 164ms, total 167ms
11-16 16:34:38.345: W/EGL_emulation(1702): eglSurfaceAttrib not implemented
11-16 16:34:45.285: W/EGL_emulation(1702): eglSurfaceAttrib not implemented
11-16 16:34:49.475: I/Choreographer(1702): Skipped 67 frames!  The application may be doing too much work on its main thread.

If I leave the application running on my AVD, the LogCat just keeps looping this section of the log:

11-16 16:34:31.046: W/EGL_emulation(1702): eglSurfaceAttrib not implemented
    11-16 16:34:31.595: I/Choreographer(1702): Skipped 32 frames!  The application may be doing too much work on its main thread.

I have no idea how to solve this issue. Please help!

回答1:

Your AboutActivity class...

public class AboutActivity extends MainActivity {

Please change it to:

public class AboutActivity extends Activity {

and as others noted, when constructing your intent, use this, or MainActivity.this.



回答2:

Try this instead:

Intent aboutScreen = new Intent(MainActivity.this, AboutActivity.class);
this.startActivity(aboutScreen);


回答3:

Try passing this instead of getBaseContext() to the intent.

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);    
    setContentView(R.layout.activity_main);

    // Fire the intent that launches the "About" screen.
    Intent aboutScreen = new Intent(this, AboutActivity.class);
    this.startActivity(aboutScreen);
}


回答4:

I just realized what the problem is.

The issue is that the AboutActivity is causing the intent to fire repeatedly. The first line in the AboutActivity's onCreate() is super.onCreate(savedInstanceState). This means that application control will go back to the MainActivity's onCreate() where the intent will then be fired again. Therefore, I seem to have caused an infinite loop of intent calling.

I will post the solution if I find it.



回答5:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);    
setContentView(R.layout.activity_main);

// Fire the intent that launches the "About" screen.
Intent i= new Intent(this, AboutActivity.class);
startActivity(i);