使用Intent启动新活动时会发生活动未找到异常

时间:2014-11-08 17:48:13

标签: android android-intent android-activity android-manifest

我正在使用线程概念,即,当我的应用程序加载时,它会显示3秒的图片,然后加载一个活动。 但我得到活动未找到异常

WelcomeScreen-1st活动(加载并保持3秒) MainActivity - 在计时器之后开始的第二个活动。(抛出错误说不幸停止)。 我是Android新手,这是我的第一个双手App。请不要让它重复。 在此先感谢

WelcomeScreen代码:

public class WelcomeScreen extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcomescreen);
        Thread thread=new Thread(){
            public void run(){
                try{
                    sleep(3000);
                }catch(InterruptedException e){
                    e.printStackTrace();
                }finally{
                    Intent startMainActivity = new Intent("com.thenewboston.helloworld.MainActivity");
                    startActivity(startMainActivity);
                }
            }
        };thread.start();

    }


}

MainActivity代码:

public class MainActivity extends ActionBarActivity {


    int counter;
    Button add, sub;
    TextView displayTxt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        counter=0;
       add=(Button) findViewById(R.id.addButton);
       sub=(Button) findViewById(R.id.subButton);
       displayTxt=(TextView) findViewById(R.id.textView1);
       add.setOnClickListener(new View.OnClickListener(

               ) {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            counter++;
            displayTxt.setText("Your Count is "+counter);
        }
    });
       sub.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            counter--;
            displayTxt.setText("Your Count is "+counter);
        }
    });


    }

Android Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.thenewboston.helloworld"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.thenewboston.helloworld.WelcomeScreen"
            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="com.thenewboston.helloworld.MainActivity"
            android:label="@string/app_name" >

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

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

</manifest>

Logcat日志:

 11-08 12:55:03.172: D/dalvikvm(1994): GC_FOR_ALLOC freed 58K, 5% free 2891K/3032K, paused 58ms, total 61ms
11-08 12:55:03.172: I/dalvikvm-heap(1994): Grow heap (frag case) to 8.050MB for 5404816-byte allocation
11-08 12:55:03.242: D/dalvikvm(1994): GC_FOR_ALLOC freed 2K, 2% free 8166K/8312K, paused 63ms, total 63ms
11-08 12:55:04.462: D/gralloc_goldfish(1994): Emulator without GPU emulation detected.
11-08 12:55:07.152: W/dalvikvm(1994): threadid=11: thread exiting with uncaught exception (group=0xb2a9aba8)
11-08 12:55:07.172: E/AndroidRuntime(1994): FATAL EXCEPTION: Thread-129
11-08 12:55:07.172: E/AndroidRuntime(1994): Process: com.thenewboston.helloworld, PID: 1994
11-08 12:55:07.172: E/AndroidRuntime(1994): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.thenewboston.helloworld.MAINACTIVITY }
11-08 12:55:07.172: E/AndroidRuntime(1994):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1632)
11-08 12:55:07.172: E/AndroidRuntime(1994):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
11-08 12:55:07.172: E/AndroidRuntime(1994):     at android.app.Activity.startActivityForResult(Activity.java:3424)
11-08 12:55:07.172: E/AndroidRuntime(1994):     at android.app.Activity.startActivityForResult(Activity.java:3385)
11-08 12:55:07.172: E/AndroidRuntime(1994):     at android.app.Activity.startActivity(Activity.java:3627)
11-08 12:55:07.172: E/AndroidRuntime(1994):     at android.app.Activity.startActivity(Activity.java:3595)
11-08 12:55:07.172: E/AndroidRuntime(1994):     at com.thenewboston.helloworld.WelcomeScreen$1.run(WelcomeScreen.java:22)

3 个答案:

答案 0 :(得分:1)

回答这个问题可能是

Intent startMainActivity = new Intent(this,MainActivity.class);

我认为这会有所帮助

答案 1 :(得分:1)

而不是你的意图使用这个

  

new Intent(WelcomeScreen.this,MainActivity.class);

答案 2 :(得分:0)

您应该在清单中指定未设置为Launcher的默认活动,如下所示:

<activity
    android:name=".MainActivity"
    android:label="@string/app_name" >
</activity>

然后你可以简单地开始一项新活动

startActivity(new Intent(this,MainActivity.class));