7

I'm new in this world. I have a problem when I use startActivity(intent). This is the Manifest:

<activity
        android:name="com.example.counter.Splash"
        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.counter.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>

And this is the code:

 public class Splash extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.splash);

    Thread timer = new Thread(){
        public void run()
        {
            try
            {
                sleep(5000);

            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
            finally
            {

                Intent i=new Intent ("com.example.counter.MainActivity");
                startActivity(i);
            }
        }
    };

    timer.start();

}

I'd want to show Splash activity for 5 seconds and then show MainActivity. LogErrors: !https://www.dropbox.com/s/kg7xyp6h4b95itq/Screenshot%202014-02-08%2016.57.36.png

Ali Khaki
  • 1,184
  • 1
  • 13
  • 24
  • Have a look at the different [Intent constructors](http://developer.android.com/reference/android/content/Intent.html#pubctors). You are trying to launch with an Intent which has an action that doesn't exist and hence the error. Use Intent(android.content.Context, java.lang.Class>) instead – coderplus Feb 08 '14 at 15:40

6 Answers6

15

There are two ways of doing what you are trying to do.

  1. Using an implicit Intent
  2. Using an explicit Intent

Refer Intent Types

  1. Implicit Intent

Declare Intent Filters for your Activity in your AndroidManifest.xml. By doing that the Android system understands what kind of Intents your component(in this case your MainActivity) can handle.

<activity
        android:name="com.example.counter.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.example.counter.MainAction" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
<activity>

Now you will be able to launch your Activity with the same Intent

Intent i=new Intent ("com.example.counter.MainAction");
startActivity(i);

Such implicit Intents are used when you don't explicitly know which Activity has to be started and you want the Android system to decide which component to start. If the system finds multiple components which can handle your Intent, it will allow the user to choose.

Note: it is possible that there are no applications that can handle your intent. In this case, your application will crash when you invoke startActivity(). To avoid this, before calling startActivity() you should first verify that there is at least one application registered in the system that can handle the intent. To do this use resolveActivity() on your intent object.

  1. Explicit Intent

In your case, you should use an explicit Intent as you already know which Activity you want to start. So create an Intent by passing the context and the component(Activity) class you want to start.

Intent i=new Intent (this,MainActivity.class);
startActivity(i);
Kiran Maniya
  • 8,453
  • 9
  • 58
  • 81
coderplus
  • 5,793
  • 5
  • 34
  • 54
3

You have to reference the class you want to start. So you'd need something like:

Intent newAct = new Intent(this, Splash.class);
startActivity(newAct);

What you're passing is an Action that is not understood as a class name.

nKn
  • 13,691
  • 9
  • 45
  • 62
1

I guess, Splash is your Launcher Activity, make following changes in your manifest file:

<activity
    android:name="com.example.counter.Splash"
    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.counter.MainActivity"
    android:label="@string/app_name" >
</activity>

Make your activity this way:

public class Splash extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

        /*Splach screen that display for 5 seconds when app start*/
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent i = new Intent(Splash.this, MainActivity.class);
                startActivity(i);
                finish();
            }
        }, 5000);

    }

}

I hope this should solve your problem now.

Chintan Soni
  • 24,761
  • 25
  • 106
  • 174
  • This may lead to huge memory leaks if not well handled. I recommend reading this for knowing how to get `Context` without the danger of memory leaks: http://stackoverflow.com/questions/7144177/getting-the-application-context – nKn Feb 08 '14 at 15:24
  • there is another error. in the first instruction the word this give me this problem : "Type mismatch: cannot convert from new Thread(){} to Context" –  Feb 08 '14 at 15:43
  • see the link that i put in the description –  Feb 08 '14 at 16:02
1

I think you should be able to use (implicit Intent):

Intent i=new Intent ("com.example.counter.MainActivity");

There is no reason to change it to (explicit intent):

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

but then you need to change the action in intent filterof MainActivity from:

<action android:name="android.intent.action.MAIN" />

to:

<action android:name="com.example.counter.MainActivity"/>
Ali
  • 9,800
  • 19
  • 72
  • 152
  • heyy, I didn't find if there exists any action like `` – Chintan Soni Feb 08 '14 at 15:31
  • [Intent i = new Intent(String action)](http://stackoverflow.com/questions/11703143/how-can-i-start-main-activity-with-the-help-of-intent-filter). – Ali Feb 08 '14 at 15:49
  • There are reasons to change: security concerns. For example, in this case a competing app can also provide the "com.example.counter.MainActivity" intent, and, every time they open your app, the user would be asked which provider they want to choose. Implicit intents are only for inter-app services (e.g. a document viewer), or transitions to/from other apps in your app family (secure them by a permission to prevent access by unaffiliated apps, unless and until you want to make the intent a part of your API contract with the world). – Theodore Murdock Jul 27 '16 at 21:34
0

You need to declare an activity in manifest file.

Like this:

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".FirstActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activty
        android:name="com.example.counter.MainActivity"/>
</application>

Hope it helps.

Community
  • 1
  • 1
MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
0

i think it is better if you use Handler put this code at the splash Activity at the onCreate

new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(intent);
                finish();
            }
        }, 1500); 

and if you want to open it one time it is good to use SharedPreferences