简体   繁体   中英

how to start new activity in android

I want to start a new activity called Counter when a button is clicked, but I got an error that is the activity is not found...so where is the wrong in my code:

t = new Thread(){

            public void run(){

                try{

                    sleep(5000);
                }
                catch (InterruptedException e){

                    e.printStackTrace();
                }
                finally{

                    Intent counter = new Intent("com.example.test.Counter");
                    startActivity(counter);
                }
            }
        };

        test.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                t.run();
            }
        });

this is the manifest file :

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

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

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

</manifest>
Intent intent = new Intent(MyActivity.this, OtherActivity.class);
startActivity(intent);

You can replace this

                Intent counter = new Intent("com.example.test.Counter");
                startActivity(counter);

with this, it would work ..

                Intent counter = new Intent(MainActivity.this, Counter.class);
                startActivity(counter);

Or to let your code work, you are missing intent-filter

<activity
    android:name="com.example.test.Counter"
    android:label="@string/title_activity_counter" >
    <intent-filter>
        <action android:name="android.intent.action.COUNTER" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

You should always provide an intent-filer if you want to start an activity with your way ..

Change this

   Intent counter = new Intent("com.example.test.Counter");
   startActivity(counter);

This is called implicit intent and it needs intent filter

to

  Intent counter = new Intent(MainActivity.this,Counter.class);
  startActivity(counter);

This is called explicit intent and there is no need for intent filter

You should use explicit intent coz you have

   <activity
        android:name="com.example.test.Counter"
        android:label="@string/title_activity_counter" >
    </activity>

Quoting docs

Explicit intents specify the component to start by name (the fully-qualified class name). You'll typically use an explicit intent to start a component in your own app, because you know the class name of the activity or service you want to start . For example, start a new activity in response to a user action or start a service to download a file in the background.

Note: An explicit intent is always delivered to its target, regardless of any intent filters the component declares.

Edit:

You should call start() on the thread not run

you should have t.start(); . That is the function that starts a thread.

You may also want to try running on the UI thread by replacing the contents of your finally block with this:

MyActivity.this.runOnUiThread(new Runnable() {
    public void run() {
        Intent counter = new Intent(MyActivity.this, Counter.class);
        MyActivity.this.startActivity(counter);
    }
};

And as others have said, use t.start() instead of t.run() as run() will block any further action until it completes.

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