简体   繁体   中英

Activity not found exception occurs when new activity is started using Intent

I am using thread concept ie, when my app loads it displays a pic for 3 secs and then loads an Activity. But i am getting an activity not found exception

WelcomeScreen-1st Activity(Loads up and stays for 3secs) MainActivity-2nd activity which shld start after the timer.(throws error saying Unfortunately stopped ). I am completely new to Android and this is my first handson App. Kindly dont make it duplicate. Thanks in Advance

WelcomeScreen code :

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 code :

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 logs :

 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)

Answer to this question might be

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

I think this would help

Instead of your intent use this

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

You should specify default activities that are not set as a Launcher in the manifest like this:

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

And then you can simply start a new activity

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM