Reputation: 647
I have a basic Android app set up with two activities. The problem is that the first one opens, and just stays there forever, and the second one does not launch.
Could anyone say what's wrong?
MainActivity.java
package com.desecrationstudios.firstapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
int counter;
Button add, sub;
TextView display;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.linear);
counter = 0;
add = (Button) findViewById(R.id.bAdd);
sub = (Button) findViewById(R.id.bSub);
display = (TextView) findViewById(R.id.tvDisplay);
add.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
counter++;
display.setText("Your total is " + counter);
}
});
sub.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
counter--;
display.setText("Your total is " + counter);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Splash.java:
package com.desecrationstudios.firstapp;
import android.app.Activity;
import android.os.Bundle;
public class Splash extends Activity{
@Override
protected void onCreate(Bundle splash) {
super.onCreate(splash);
setContentView(R.layout.splash);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.desecrationstudios.firstapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".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=".MainActivity"
android:label="@string/app_name" >
</activity>
</application>
</manifest>
Thanks!
Upvotes: 0
Views: 2086
Reputation: 11880
You can try replacing your Splash class with this code:
package com.desecrationstudios.firstapp;
import android.app.Activity;
import android.os.Bundle;
public class Splash extends Activity{
private static long SLEEP_TIME = 5;
@Override
protected void onCreate(Bundle splash) {
super.onCreate(splash);
setContentView(R.layout.splash);
IntentLauncher launcher = new IntentLauncher();
launcher.start();
}
private class IntentLauncher extends Thread {
public void run() {
try {
// Sleeping
Thread.sleep(SLEEP_TIME*1000);
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
// Start main activity
Intent intent = new Intent(Splash.this, MainActivity.class);
Splash.this.startActivity(intent);
Splash.this.finish();
}
}
}
Note: the SLEEP_TIME variable indicates the time you want the splash screen to be shown.
Upvotes: 2
Reputation: 132992
you forget to start MainActivity
Activity from Splash
Activity. so start it as:
@Override
protected void onCreate(Bundle splash) {
super.onCreate(splash);
setContentView(R.layout.splash);
// start MainActivity here
Intent intent=new Intent(this,MainActivity.class);
startActivity(intent);
}
Upvotes: 5
Reputation: 965
The problem I see is you don't start your MainActivity with an intent from your splash activity.
I would recommend something like this:
Thread pause = new Thread(){
public void run(){
try {
sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}finally{
Intent i = new Intent("your.path.to.MAINACTIVITY");
startActivity(i);
finish();
}
}
};
pause.start();
Add this is with you onCreate method after you set the content view. This will pause your app for 3 seconds and then will launch your MainActivity after the paused.
Notes
To change the amount of time your app pauses for change the sleep(3000) to the number of milliseconds you want it to pause for.
Also your when specifying the intent your class name should be all caps though the package name should be all lower case.
Upvotes: 1
Reputation: 3814
You launch the Splash Activity, but then you have nothing there to launch the 2nd one. You must create an Intent
and launch it with startActivity()
.
Upvotes: 1