从MainActivity的onCreate()方法启动另一个Activity?

7
我遇到的问题是,我的MainActivity中的onCreate()方法似乎无法启动另一个活动。 我有代码可以在我点击按钮时启动我的 "AboutActivity"。然而,我想让 "AboutActivity" 在 MainActivity 中的 onCreate() 完成后立即启动。 当我尝试从 onCreate() 启动 "AboutActivity" 时,程序卡在空屏幕上。 发布的代码是问题的简化版本,本质上就是我要做的事情。
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);
    }

AboutActivity类的代码:

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);
    }
}
LogCat消息是:
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.
如果我让应用程序在AVD上运行,LogCat就会一直循环记录日志中的这一部分内容:
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.
我不知道如何解决这个问题,请帮助!

1
暂时不要考虑代码、onCreate和所有那些东西。你想做什么? - Simon
我正在尝试如果我的SharedPreferences中的布尔值设置为true,则弹出一个屏幕。同样的屏幕也可以通过MainActivity UI中的按钮访问。最终,我试图实现类似于启动画面的效果。 - Lethal Left Eye
什么是MainActivity?你能发布它的代码吗? - MGDroid
1
你的AboutActivity必须扩展MainActivity吗?还是可以直接扩展Activity?我没有测试过,但这可能会导致一些问题。 - Mike M.
5个回答

4

您的AboutActivity类...

public class AboutActivity extends MainActivity {
请将其更改为:
public class AboutActivity extends Activity {

正如其他人所指出的,当构造您的意图时,请使用this或MainActivity.this。


3
尝试使用以下方法:
Intent aboutScreen = new Intent(MainActivity.this, AboutActivity.class);
this.startActivity(aboutScreen);

那么你的问题可能出在其他地方。你能贴出整个代码吗?看起来你正在进行一些内存密集型的工作。 - salrawaf
我认为你的问题在主活动中,而且我认为甚至没有到达关于屏幕的调用。你可以通过在调用之前添加日志并查看是否打印来检查它。 - salrawaf
我已经通过应用程序并在各个位置设置了断点。我可以向您保证,它确实到达了 "this.startActivity(aboutScreen);" 这一行代码。 - Lethal Left Eye
嗯...那么请检查一下是否到达了关于活动的setContentView。也许您的布局无法膨胀或在膨胀时出现问题? - salrawaf

2

试着将this传入intent,而不是getBaseContext()

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

这似乎不能解决我的问题。:/ - Lethal Left Eye
请问您能分享一下AboutActivity类的实现吗? - Kashif Nazar
AboutActivity现在在原始帖子中可见。 - Lethal Left Eye
你还做了什么? - Kashif Nazar
最初,我在MainActivity的onCreate()方法内运行了一些其他代码。但是,为了简单起见,我将问题简化为您在onCreate()方法中看到的所有内容。它完全被复制。 - Lethal Left Eye

1
我刚刚意识到问题所在。 问题在于AboutActivity导致意图重复触发。 AboutActivity的onCreate()中的第一行是super.onCreate(savedInstanceState)。这意味着应用程序控制将返回MainActivity的onCreate(),然后再次触发意图。因此,我似乎引起了无限循环的意图调用。 如果我找到解决方案,我会发布它。

@Melquiades 比我更快地找到了我的解决方案。 - Lethal Left Eye
1
为什么你的AboutActivity要继承MainActivity?它应该只需要继承普通的Activity。 - Nathan Walters
1
@Lethal Left Eye:很高兴你自己找到了问题 :) 的确,因为AboutActivity扩展了MainActivity,并调用了它的super(),所以你正在Main的onCreate中一遍又一遍地创建一个意图。 - Melquiades

0
@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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,