How to start a new activity in Android

I want to start a new action called Counterwhen I click a button, but I got an error that is not an activity ... so somewhere is 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>
+4
source share
5 answers

Change it

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

This is called an implicit intent and it needs an intent filter.

to

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

This is called an explicit intent, and there is no need for an intent filter.

You must use explicit intentions, you have

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

Quotation of documents

( ). , , . , .

. , - , .

Edit:

start() run

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

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

, .

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

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

intent-filer, -.

+1

t.start();. , .

+1

, finally :

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

, , t.start() t.run(), run() , .

+1

Source: https://habr.com/ru/post/1533188/


All Articles