MD.MD
MD.MD

Reputation: 718

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>

Upvotes: 3

Views: 169

Answers (5)

Dan Harms
Dan Harms

Reputation: 4840

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.

Upvotes: 1

Raghunandan
Raghunandan

Reputation: 133560

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

Upvotes: 1

Scorpio
Scorpio

Reputation: 1144

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

Upvotes: 1

BSK-Team
BSK-Team

Reputation: 1810

Intent intent = new Intent(MyActivity.this, OtherActivity.class);
startActivity(intent);

Upvotes: 2

Ahmed Ekri
Ahmed Ekri

Reputation: 4651

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 ..

Upvotes: 1

Related Questions