当前位置: 首页 > 面试题库 >

从MainActvity中的onCreate()启动另一个活动?

訾旭
2023-03-14
问题内容

我遇到的问题是MainActivity中的onCreate()方法似乎无法启动另一个活动。

我的代码在工作,因此当我单击按钮时,“
AboutActivity”将启动。但是,我要这样做,以便MainActivity中的onCreate()完成后立即启动“ AboutActivity”。

尝试从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.

我不知道如何解决这个问题。请帮忙!


问题答案:

您的AboutActivity课程…

public class AboutActivity extends MainActivity {

请更改为:

public class AboutActivity extends Activity {

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



 类似资料:
  • 我在使ActivityRecognitation服务保持运行时遇到问题。我目前有一个在后台连续运行的服务(GService)。我希望在GService中启动ActivityRecognitation服务,并让ActivityRecognitation服务将活动结果广播回GService。我能够启动服务并接收它正在运行的反馈,我还从意图处理程序获得一个结果(没有实际数据),但再也不会得到了。 以下是

  • 我试图通过按cardview开始另一项活动,cardview有一个朋友查找id。但是当我写回家时。java它给了我setOnClickListener中的问题。在homeActivity中,它告诉我无法解析“homeActivity”中的方法“homeActivity”。因为

  • 在我的程序中,我有一个当应用程序打开时启动的活动。如果我再打开几个活动,我怎么能回到主活动?在意图过滤器中,活动的名称是“android.intent.action.MAIN”,它不允许我在上面调用start Active()。我该怎么办?

  • 我有一个活动,可以在收藏夹列表中添加书签。每个宠儿都有一个按钮。当我点击那个按钮时,我必须进入那个特定的活动。但它显示了一个错误。应用程序不会崩溃,但书签活动不会从单击开始。 以下是“收藏夹”活动的代码: 错误是onClick方法,具体如下: 它给我一个ClassNotFoundExc0019。 似乎有了这段代码,我就可以启动这个包中的活动了。但问题是,我想开始的活动在另一个包中。logcat说它

  • 问题内容: 在我通过Admob广告中介添加MoPub的最新版本中,我在崩溃日志中看到了一堆ConcurrentModificationException。所有这些似乎都是本机代码,我使用的是与广告相关的所有最新版本以及与Google / android相关的所有库。有人遇到过吗? 这是直到BaseActivity的第21行的代码: 这就是代码,直到最近的最近70行 问题答案: 就我们而言,我们只会

  • 问题内容: 从Activity中包含的RecyclerView的适配器中,我试图在按下RecyclerView的元素时启动一个片段,这是我现在的代码: 我测试了它是否启动了我创建的一些“测试活动”,所以我知道除了片段启动之外的所有功能都可以正常工作。 错误在这里: 我正在启动作为活动的Fragment,所以当我运行应用程序时,它崩溃了,并告诉我在清单中将MainFragment声明为活动。 如何从