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
i think it is better if you use Handler put this code at the splash Activity at the onCreate
and if you want to open it one time it is good to use SharedPreferences
I guess,
Splash
is your Launcher Activity, make following changes in your manifest file:Make your activity this way:
I hope this should solve your problem now.
You need to declare an activity in manifest file.
Like this:
Hope it helps.
I think you should be able to use (implicit Intent):
There is no reason to change it to (explicit intent):
but then you need to change the
action
inintent filter
of MainActivity from:to:
You have to reference the
class
you want to start. So you'd need something like:What you're passing is an
Action
that is not understood as a class name.There are two ways of doing what you are trying to do.
Intent
Intent
Refer Intent Types
Intent
Declare
Intent Filters
for yourActivity
in yourAndroidManifest.xml
. By doing that theAndroid
system understands what kind ofIntents
your component(in this case your MainActivity) can handle.Now you will be able to launch your
Activity
with the sameIntent
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.
Intent
In your case, you should use an explicit
Intent
as you already know whichActivity
you want to start. So create anIntent
by passing the context and the component(Activity) class you want to start.