【问题标题】:Android Splash Screen Crashes (simple / noob issue)Android 闪屏崩溃(简单/菜鸟问题)
【发布时间】:2025-12-29 13:30:16
【问题描述】:

我刚刚在我的应用程序中添加了一个启动画面(之前运行良好),我想知道这里是否有人足够聪明,可以发现我遇到的问题的根源。应用程序启动,启动屏幕显示,然后当它加载主布局(AppActivity.java / main.xml)时,应用程序强制关闭。 : (

package com.mkyong.android;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

public class SplashScreen extends Activity {

private static final int SPLASH_TIME = 3 * 1000;// 3 seconds

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

    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {

            Intent intent = new Intent(SplashScreen.this,
                    AppActivity.class);
            startActivity(intent);

            SplashScreen.this.finish();

            overridePendingTransition(R.anim.appear, R.anim.disappear);

        }
    }, SPLASH_TIME);

    new Handler().postDelayed(new Runnable() {
          @Override
          public void run() {
                 } 
            }, SPLASH_TIME);

}


@Override
public void onBackPressed() {
    this.finish();
    super.onBackPressed();
}
}

LogCat:

03-02 13:32:41.936: E/Trace(2065): error opening trace file: No such file or directory (2)
03-02 13:32:42.116: D/dalvikvm(2065): GC_FOR_ALLOC freed 61K, 8% free 2404K/2608K, paused 51ms, total 54ms
03-02 13:32:42.166: I/dalvikvm-heap(2065): Grow heap (frag case) to 6.000MB for 3686416-byte allocation
03-02 13:32:42.316: D/dalvikvm(2065): GC_FOR_ALLOC freed <1K, 4% free 6003K/6212K, paused 144ms, total 144ms
03-02 13:32:42.477: D/dalvikvm(2065): GC_CONCURRENT freed <1K, 4% free 6003K/6212K, paused 8ms+6ms, total 167ms
03-02 13:32:43.216: I/Choreographer(2065): Skipped 30 frames!  The application may be doing too much work on its main thread.
03-02 13:32:43.246: D/gralloc_goldfish(2065): Emulator without GPU emulation detected.
03-02 13:32:46.046: D/AndroidRuntime(2065): Shutting down VM
03-02 13:32:46.046: W/dalvikvm(2065): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
03-02 13:32:46.086: E/AndroidRuntime(2065): FATAL EXCEPTION: main
03-02 13:32:46.086: E/AndroidRuntime(2065): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.mkyong.android/com.mkyong.android.AppActivity}; have you declared this activity in your AndroidManifest.xml?
03-02 13:32:46.086: E/AndroidRuntime(2065): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1618)
03-02 13:32:46.086: E/AndroidRuntime(2065): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
03-02 13:32:46.086: E/AndroidRuntime(2065): at android.app.Activity.startActivityForResult(Activity.java:3370)
03-02 13:32:46.086: E/AndroidRuntime(2065): at android.app.Activity.startActivityForResult(Activity.java:3331)
03-02 13:32:46.086: E/AndroidRuntime(2065): at android.app.Activity.startActivity(Activity.java:3566)
03-02 13:32:46.086: E/AndroidRuntime(2065): at android.app.Activity.startActivity(Activity.java:3534)
03-02 13:32:46.086: E/AndroidRuntime(2065): at com.mkyong.android.SplashScreen$1.run(SplashScreen.java:24)
03-02 13:32:46.086: E/AndroidRuntime(2065): at android.os.Handler.handleCallback(Handler.java:725)
03-02 13:32:46.086: E/AndroidRuntime(2065): at android.os.Handler.dispatchMessage(Handler.java:92)
03-02 13:32:46.086: E/AndroidRuntime(2065): at android.os.Looper.loop(Looper.java:137)
03-02 13:32:46.086: E/AndroidRuntime(2065): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-02 13:32:46.086: E/AndroidRuntime(2065): at java.lang.reflect.Method.invokeNative(Native Method)
03-02 13:32:46.086: E/AndroidRuntime(2065): at java.lang.reflect.Method.invoke(Method.java:511)
03-02 13:32:46.086: E/AndroidRuntime(2065): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-02 13:32:46.086: E/AndroidRuntime(2065): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-02 13:32:46.086: E/AndroidRuntime(2065): at dalvik.system.NativeStart.main(Native Method)
03-02 13:32:50.336: I/Process(2065): Sending signal. PID: 2065 SIG: 9

清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mkyong.android"
    android:versionCode="1"
    android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />

<application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".SplashScreen"
            android:theme="@android:style/Theme.Black.NoTitleBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
     <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity android:name=".main2"></activity>
        <activity android:name=".home"></activity>
        <activity android:name=".App2Activity"></activity>
        <activity
            android:label="@string/app_name"
            android:name=".AppActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>      
</application>
</manifest>

【问题讨论】:

  • android.content.ActivityNotFoundException: Unable to find explicit activity class {com.mkyong.android/com.mkyong.android.AppActivity}; have you declared this activity in your AndroidManifest.xml? 嗯,有吗?
  • 是的(附加)docs.google.com/document/d/… 我可能做错了什么吗? (我从模板/教程中复制了一部分)
  • 嗯,你应该只有一个&lt;application&gt;元素。

标签: android eclipse android-intent android-activity


【解决方案1】:
 Unable to find explicit activity class {com.mkyong.android/com.mkyong.android.AppActivity}; have you declared this activity in your AndroidManifest.xml?

是吗?

将您的清单更改为:

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mkyong.android"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".SplashScreen"
            android:theme="@android:style/Theme.Black.NoTitleBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".main2" >
        </activity>
        <activity android:name=".home" >
        </activity>
        <activity android:name=".App2Activity" >
        </activity>
        <activity
            android:name=".AppActivity"
            android:label="@string/app_name" >

            <!-- <intent-filter> -->
            <!-- <action android:name="android.intent.action.MAIN" /> -->


            <!-- <category android:name="android.intent.category.LAUNCHER" /> -->
            <!-- </intent-filter> -->
        </activity>
    </application>

</manifest>

您真的需要 2 个启动器吗?我评论了一个。 (还要感谢@Raghav,没有注意到 2 个&lt;application&gt; 标签)

【讨论】:

  • 我用上面的源代码更新了它 - 仍然崩溃:((附上新的 logcat)docs.google.com/document/d/…
  • AppActivity - docs.google.com/document/d/…(谢谢!)
  • 一个java文件中不能有两个扩展Activity的类。
  • 我可以将它们连接在一起吗?我正在按照教程添加启动画面并将其代码添加到我的项目中使整个事情崩溃。我真的需要这两个元素 - 我只是不知道如何(正确)组合这两个脚本
  • 基本上 - 我想保留启动画面的源代码以及预先存在的(在添加启动画面之前)代码,而不会使整个事情崩溃:)
【解决方案2】:

从文本本身来看,这个例外太明显了..

 have you declared this activity in your AndroidManifest.xml?

请在您的 Manifest 文件中声明 AppActivity。 并且只使用一个 Application 标签。

One android application holds only one Application tag

【讨论】:

    【解决方案3】:

    就像 Raghav Sood 评论和 Karan_Rana 回答一样。
    Android 无法打开 Appactivity.class,因为 Android 不知道它存在。
    所以你需要在 Manifest 中声明它。所以Android知道它的存在以及它存在的位置。

     <activity android:name="com.mkyong.android/com.mkyong.android.AppActivity"></activity>
    


    如果您将其添加到应用程序标记中的清单中,它应该可以工作。或者你会因为另一个问题而得到另一个错误;)

    【讨论】:

    • 添加上面的代码行后出现错误:“标签 属性名称具有无效字符 '/'。” (我加了但是我觉得不喜欢activity中间的/)
    • 也许你应该使用根源文件夹的相对路径(如“.AppActivity”)。我认为 Ahmads 的回答应该对你有所帮助。