在Android清单中更改时启动画面崩溃

时间:2015-03-23 22:54:49

标签: android android-intent android-manifest

我的启动画面( splash.java )工作正常,当我在Android Manifest中将其称为 Launcher 时,它会显示5秒,但是当我修复它在Android Manifest中为 Default 并通过 List Activity Menu.java )运行,运行5秒然后它返回到Menu.java而不是运行时错误崩溃。 我想让它显示5秒然后返回menu.java。

它在5秒内无效的清单,但5秒后应用程序崩溃运行时间(不幸的是你的应用程序已停止)

<activity
    android:name=".Menu"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity
    android:name=".splash"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="com.example.hello.SPLASH" />

        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

这个splash.java

protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity2);
        tone = MediaPlayer.create(splash.this, R.raw.songg);
        tone.start();
        Thread haai = new Thread()
        {
            public void run()
            {
                try
                {
                    sleep(5000);
                }
                catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
                finally 
                {
                    Intent first = new Intent("com.example.hello.Menu");
                    startActivity(first);
                }
            }       
        };
        haai.start();
    }

错误日志:

E/AndroidRuntime(25707): FATAL EXCEPTION: Thread-30562
E/AndroidRuntime(25707): Process: com.example.hello, PID: 25707
E/AndroidRuntime(25707): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.example.hello.Menu }
E/AndroidRuntime(25707):    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1672)
E/AndroidRuntime(25707):    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1442)
E/AndroidRuntime(25707):    at android.app.Activity.startActivityForResult(Activity.java:3511)
E/AndroidRuntime(25707):    at android.app.Activity.startActivityForResult(Activity.java:3472)
E/AndroidRuntime(25707):    at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:817)
E/AndroidRuntime(25707):    at android.app.Activity.startActivity(Activity.java:3714)
E/AndroidRuntime(25707):    at android.app.Activity.startActivity(Activity.java:3682)
E/AndroidRuntime(25707):    at com.example.hello.splash$1.run(splash.java:36)

我对自己的答案不满意,因为我正处于学习阶段,所以等待更好的答案并解释其工作和问题。

4 个答案:

答案 0 :(得分:4)

  

ActivityNotFoundException:否

因为清单中的com.example.hello.MENU操作找不到活动。

要解决问题,请对com.example.hello.MENU活动使用Menu操作字符串。像:

<activity
    android:name=".Menu"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="com.example.hello.MENU" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

或者您可以使用目标活动类名称启动活动:

Intent intent=new Intent (splash.this,Menu.class);
splash.this.startActivity(intent);

答案 1 :(得分:0)

问题出在您已设置活动名称.Menu的清单中,而在java代码中,您已将其设置为MENU(大写)。

<activity
    android:name=".Menu" <---- activity name as .Menu
    android:label="@string/app_name" >

在java代码中:

Intent first = new Intent("com.example.hello.MENU")<---.MENU- its all uppercase

在下面的Java代码中更改它以解决您的问题:

Intent first = new Intent("com.example.hello.Menu");
startActivity(first);

答案 2 :(得分:0)

我找到答案,它很简单,但我使用另一种方式,但我仍然不知道为什么程序在.splash作为MainLAUNCHER时工作正常,但是当将.Menu设为Main并运行LAUNCHER程序,但是当我从菜单列表中选择启动时,它会运行5秒钟,然后再次返回菜单,它会因运行时错误而崩溃。我将.splash代码更改为以下代码并且它可以正常工作,但由于我是android的新手,我不知道为什么最后一种方法无效。

public void run()
            {
                try
                {
                    sleep(5000);
                }
                catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
                finally 
                {
                    try
                    {
                    Class ourclass = Class.forName("com.example.hello.Menu");
                    Intent myclass = new Intent(splash.this , ourclass);
                    startActivity(myclass);
                    }
                    catch(ClassNotFoundException e)
                    {
                        e.printStackTrace();
                    }

                }
            }       

我没有更改任何Android Manifest

答案 3 :(得分:0)

问题是你声明的意图动作是.SPLASH但你创建的动作是.MENU
改变

Intent first = new Intent("com.example.hello.MENU");
                   startActivity(first);

Intent first = new Intent("com.example.hello.SPALSH");
                   startActivity(first);

或像这样更改你的清单

<activity
        android:name=".splash"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Menu"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.example.hello.MENU" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>