3

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>
MD.MD
  • 708
  • 4
  • 14
  • 34

5 Answers5

2
Intent intent = new Intent(MyActivity.this, OtherActivity.class);
startActivity(intent);
BSK-Team
  • 1,750
  • 1
  • 19
  • 37
1

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

Ahmed Ekri
  • 4,601
  • 3
  • 23
  • 42
  • intent filter is no needed – Raghunandan Mar 24 '14 at 16:19
  • you can remove the or part not needed – Raghunandan Mar 24 '14 at 16:21
  • 2
    @AhmedEkri : Your last block of code does correctly show how to use an implicit Intent but two points...First, you should make it clear that defining an intent-filter for use with implicit Intents should only be done if your Activity is to be used by 3rd-party apps. Second, don't derive your Intent actions from `android.intent.action...` - this is not your namespace and you risk confusion if (for example) the Android devs define a `COUNTER` action which bears no relevance to your Activity. – Squonk Mar 24 '14 at 16:33
1

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

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • @user3194430 further reference http://stackoverflow.com/questions/262816/when-would-you-call-javas-thread-run-instead-of-thread-start – Raghunandan Mar 24 '14 at 16:29
1

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

Scorpio
  • 1,124
  • 1
  • 11
  • 28
1

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.

Dan Harms
  • 4,725
  • 2
  • 18
  • 28