Application stopped unexpectedly Android Development - java

I followed few tutorials and I found this error unsolved.
Here are my code. My application was stopped unexpectedly every time I run after 2 seconds (pre-set timer)
The error was: The application Test(process com.juicy.test) has stopped unexpectedly. Please try again. It prompts on the AVD.
W/dalvikvm(281): threadid=7: thread exiting with uncaught exception
(group=0x4001d800)
E/AndroidRuntime(281): FATAL EXCEPTION: Thread-8
E/AndroidRuntime(281): android.content.ActivityNotFoundException: No
Activity found to handle Intent { act=com.juicy.test.MAINACTIVITY }
E/AndroidRuntime(281): at
android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1408)
E/AndroidRuntime(281): at
android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
E/AndroidRuntime(281): at
android.app.Activity.startActivityForResult(Activity.java:2817)
E/AndroidRuntime(281): at
android.app.Activity.startActivity(Activity.java:2923)
E/AndroidRuntime(281): at com.juicy.test.Tree$1.run(Tree.java:24)**
Tree.xml
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tree);
Thread timer = new Thread(){
public void run(){
try{
sleep(2000);
}catch(InterruptedException e){
e.printStackTrace();
}
finally{
Intent openMainActivity = new Intent("com.juicy.test.MAINACTIVITY");
startActivity(openMainActivity);
}
}
};
timer.start();
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.juicy.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Tree"
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" >
<intent-filter>
<action android:name="com.juicy.test.MAINACTIVITY" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java
package com.juicy.test;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
int counter;
Button add, sub;
TextView display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
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() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter++;
display.setText("Your total is " + counter);
}
});
sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

In your manifest file you are starting two activities at a time so either remove this code from .MainActivity or .Tree activity ..
<intent-filter>
<action android:name="com.juicy.test.MAINACTIVITY" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
hope this helps ...

Open the manifest and Ctrl click on the activity class name. If the class file doesn't open, the chances are, it is not mapped correctly. This may lead to this error.

In your manifest file you have set two launchers in manifest file. Here launcher means which activity to start at first so there can be only one launcher.
Whenever you are launching your application it searches for activity containing launcher from top in manifest and opens the first activity containing Launcher(which is .Tree in your case). Remember to set category other than launcher for other activities.
In AndroidManifest.xml change application containing MainActivity to android.intent.category.DEFAULT
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.juicy.test.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Note: You could also remove the category field as it is set as default when you donot specify any category

Related

Putting multiple onClickListener and OnClick in the same activity

I am trying to setup multiple buttons but I seem to be having trouble doing it. I have 2 onClickListener functions in the same activity. Am I right in doing this or should this be done some other way? btnChpt3 works but when I input the onclicklistener for btnChpt3_1 it force closes as soon as it opens. Thanks.
MainMenu.java
package com.th3ramr0d.learnar670_1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainMenu extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button chapterThree = (Button) findViewById(R.id.btnChpt3);
chapterThree.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainMenu.this, Chapter3.class));
}
});
Button chapterThree_1 = (Button) findViewById(R.id.btnChpt3_1);
chapterThree_1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainMenu.this, Chapter3_1.class));
}
});
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}
So I added a new class to handle the buttons on a sub menu page. But now I am running into the same problem I was previously where the button wont open. I am guessing its because the code isnt being ran for some reason.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.th3ramr0d.learnar670_1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainMenu"
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=".SubMenuChapter3"
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=".Chapter3"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.th3ramr0d.learnar670_1.CHAPTER3" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Chapter4"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.th3ramr0d.learnar670_1.CHAPTER4" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
You can see here that my .MainMenu launches. I am guessing that means it launches at startup. So the button works. However, now I want the button associated with .SubMenuChapter3 to launch and it does nothing. Here Program runs with no errors but button wont open you can see I had the same problem until codepg was nice enough to tell me where I was wrong. But I want to use several of these sub menus so how would I do that?
Your problem, after reading, is that you are referencing a button that you have created in another layout which you are not inflating.
In the setContentView(R.layout.activity_main); you are inflating activity_main so you can only have onclick listeners for buttons and elements in that xml.
Make sure that you add btnChpt3 in this xml or if this button is supposed too be in another layout then have another activity or fragment control that layout by inflating it.
Right now this method Button chapterThree = (Button) findViewById(R.id.btnChpt3); is returning a null pointer exception.
I can see from your comments that you are inflating the view activity_main but Button with id bthChpt3_1 is in another file. This is an error as you can use findViewById(R.id.buttonId) to find the id of a button which exists within the view you are using. Since the button is in another xml file, it will throw a null pointer exception.

Program runs with no errors but button wont open

I have been following the mybringback tutorials on youtube and I tried implementing what I learned. Trying to get a button on my main page to open another page. Finally got the program to run without errors but now when I press the button nothing opens.
Main .xml file where my button is
<Button
android:id="#+id/btnChpt3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="Appearance and Grooming Policies"
android:textSize="18sp"
android:textStyle="bold|italic"
android:gravity="center"
/>
Name of .xml file im trying to get to is chapter3.xml
Menu.java
package com.th3ramr0d.learnar670_1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Menu extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button chapterThree = (Button) findViewById(R.id.btnChpt3);
chapterThree.setOnClickListener(new View.OnClickListener() {
// #Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("com.th3ramr0d.learnar670_1.CHAPTER3"));
}
});
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}
And my manifest.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.th3ramr0d.learnar670_1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".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=".Menu"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.th3ramr0d.learnar670_1.MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Chapter3"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.th3ramr0d.learnar670_1.CHAPTER3" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
So button ided as btnChpt3 wont open up my .xml file named chapter3.xml. Thanks for the help.
Here is my Chapter3.java
package com.th3ramr0d.learnar670_1;
import android.app.Activity;
import android.os.Bundle;
public class Chapter3 extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.chapter3);
}
}
Here is my MainActivity.java
package com.th3ramr0d.learnar670_1;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Copy this and paste in your AndroidManifest and try,
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.th3ramr0d.learnar670_1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Menu"
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=".Chapter3"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.th3ramr0d.learnar670_1.CHAPTER3" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
To help you understand the problem,
<category android:name="android.intent.category.LAUNCHER" />
The code above in the AndroidManifest defines the activity to be launced when the App Icon is pressed. As per your earlier manifest, it launches the activity MainActivity which also sets it setContentView(R.layout.activity_main); by default as the IDE creates a Hello World program.
So when you launch you app, its MainActivity (that looks the same layout you have designed) which is loading and not Menu activity which you want to load. Hence making few changes in the manifest where we declare the Menu activity as the launcher now launches Menu activity which has the piece of code to process your button click.
I hope this helped!
Actually you can try a more convenient way of starting activities inside your application:
startActivity(new Intent(Menu.this, Chapter3.class))
Also you can read more how it works here:
http://developer.android.com/training/basics/firstapp/starting-activity.html
Good day.
Try replacing the line :
startActivity(new Intent("com.th3ramr0d.learnar670_1.CHAPTER3"));
with the code below :
Intent intent = new Intent(Menu.this, Chapter3.class);
startActivity(intent);
Try reverting your code to the original code, copy your layout activity_main.xml and rename it menu.xml.
now in the layout menu.xml change this line:
android:text="Appearance and Grooming Policies"
to:
android:text="Go to menu"
and the line:
android:id="#+id/btnChpt3"
with:
android:id="#+id/btnMenu"
and replace the line:
setContentView(R.layout.activity_main);
in Menu.java with:
setContentView(R.layout.menu.xml);
finally in MainActivity.java add the following to your oncreate method:
Button btnGoToMenu = (Button) findViewById(R.id.btnMenu);
btnGoToMenu.setOnClickListener(new View.OnClickListener() {
// #Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Menu.class);
startActivity(intent);
}
});
and re-run the application.

The application crashes after the splash screen (When the menu screen supposed to appear)

I made a game with the menu screen and a splash screen. I followed the steps on YouTube tutorials. The application crashes every time right after the splash screen ends (when the menu screen supposed to appear).
Here is my code, there's no error. Where could the problem be?
Splash screen:
package com.group5.littlered;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
public class MyMain extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//set content view AFTER ABOVE sequence (to avoid crash)
this.setContentView(R.layout.main|R.layout.splash);
setContentView(R.layout.splash);
Thread timer = new Thread(){
public void run(){
try{
sleep(5000);
} catch (InterruptedException e){
e.printStackTrace();
}finally{
Intent openStartingPoint = new Intent("com.group5.littlered.STARTINGPOINT");
startActivity(openStartingPoint);
}
}
};
timer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
Menu Screen:
package com.group5.littlered;
import android.app.Activity;
import android.os.Bundle;
public class MyMenu extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}}
andriodManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.group5.littlered"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MyMain"
android:label="#string/app_name"
android:screenOrientation="landscape"
android:configChanges="keyboard|keyboardHidden|orientation" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MyMenu"
android:label="#string/app_name"
android:screenOrientation="landscape"
android:configChanges="keyboard|keyboardHidden|orientation" >
<intent-filter>
<action android:name="com.group5.littlered.SPLASH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
Try this..
Change this..
Intent openStartingPoint = new Intent("com.group5.littlered.STARTINGPOINT");
startActivity(openStartingPoint);
to
Intent openStartingPoint = new Intent(MyMain.this,MyMenu.class);
startActivity(openStartingPoint);

Unable to instantiate activity ComponentInfo… java.lang.NullPointerException

OK, so the problem I'm having occurs when the startGame button is pressed. The app crashes when the button is pressed. The activity is instantiated in the manifest so I'm not sure where the error is. The code for the intent is a copy of the other one (which works) so I have no clue where I went wrong.
Error Log:
02-25 14:46:51.064: E/AndroidRuntime(1261): FATAL EXCEPTION: main
02-25 14:46:51.064: E/AndroidRuntime(1261): Process: com.example.hegemony, PID: 1261
02-25 14:46:51.064: E/AndroidRuntime(1261): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.hegemony/com.example.hegemony.PlayerTurn}: java.lang.NullPointerException
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hegemony"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.hegemony.SplashScreen"
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.hegemony.StartScreen" >
<intent-filter>
<action android:name="com.example.hegemony.STARTSCREEN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.hegemony.SetupHomeScreen" >
<intent-filter>
<action android:name="com.example.hegemony.SETUPHOMESCREEN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.hegemony.SetupPlayer"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.hegemony.SETUPPLAYER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.hegemony.PlayerTurn"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.hegemony.PLAYERTURN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
Java Code of sending activity:
public class SetupHomeScreen extends Activity{
private ArrayList<Player> p = GameMaster.players;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setup_home_screen);
getActionBar().hide();
updatePlayers();
Button gotoInput = (Button) findViewById(R.id.btnSetupPlayer);
gotoInput.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent toInput = new Intent("com.example.hegemony.SETUPPLAYER");
startActivity(toInput);
}
});
Button startGame = (Button) findViewById(R.id.btnStartGame);
startGame.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent toStart = new Intent("com.example.hegemony.PLAYERTURN");
startActivity(toStart);
}
});
}
public void updatePlayers(){
TextView playerList = (TextView) findViewById(R.id.playerList);
String msg = "";
for(int i=0;i < p.size();i++)
msg = msg + "\n - "+p.get(i).getName();
playerList.setText(msg);
if(p.size() >=2){
Button enable = (Button) findViewById(R.id.btnStartGame);
enable.setEnabled(true);
}
}
}
Java code of receiving activity:
public class PlayerTurn extends Activity {
final ActionBar actionBar = getActionBar();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player_turn);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
};
}
}
Button startGame = (Button) findViewById(R.id.btnStartGame);
startGame.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent toStart = new Intent(SendingActivity.this, NewActivity.class);
startActivity(toStart);
}
});
I think this is how you should start an intent. Maybe its missing a context or the "activityname" is not the right way to do it. I tried to start an activity the way you are trying and it gave me an error as well, not the same one, but it didn't work.
I'm sorry if the solution I'm trying to provide doesn't work. This is really the first time I'm trying to help someone. I hope it works.
As per the logcat error message, your activity class is failing to instantiate. Instantiation involves the allocation and assignment of any class member variables. In the case of PlayerTurn, the only one is this:
final ActionBar actionBar = getActionBar();
The call to getActionBar() throws a NullPointerException because the activity's window has not been built yet - you should call getActionBar() after setContentView() has been called in onCreate(). You can resolve this by simply moving that line of code into onCreate().
If you still want to retain this as a class member variable, declare it but don't assign it:
ActionBar actionBar;
...and then do the assignment in onCreate():
actionBar = getActionBar();

Android application doesn't launch on device

I'm a beginner android developer here and I coded a basic activity that loads an image, waits for 5 seconds and opens the main activity that holds a text view. There are no errors and it compiles perfectly fine. When I launch on my device though, it doesn't open. (Doesn't open on the emulator either).
I'm not sure why this is.
Here is the manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gui"
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"
android:debuggable="true">
<activity
android:name="com.example.gui.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.gui.Splash"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.SPLASH" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Here is the Splash activity I intent to load first.
package com.example.gui;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class Splash extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread splash_thread = new Thread(){
public void run(){
try{
sleep(5000);
} catch(InterruptedException e)
{ e.printStackTrace();
}finally{ Intent splash_intent = new Intent("android.intent.action.MAIN");
startActivity(splash_intent);
}
}
};
splash_thread.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
And here is the other activity I intend to run:
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
I'm following a tutorial off the internet, this still doesn't run for some reason. My layout's are very standard as well. Help'd be appreciated.
You can set wrong path in manifest.xml file.
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
android:debuggable="true">
<activity
android:name="com.example.gui.Splash"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.gui.MainActivity"
android:label="#string/app_name" >
try this and now your splash screen lauch first and the main activity will launch...
if it not working then try this code only if your activity will not give response.
public class Splash_screen extends Activity {
private static String TAG = Splash_screen.class.getName();
private static long SLEEP_TIME = 3; // Sleep for some time
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE); // Removes title bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // Removes notification bar
setContentView(R.layout.splash_screen);
IntentLauncher launcher = new IntentLauncher();
launcher.start();
}
private class IntentLauncher extends Thread{
#Override
/**
* Sleep for some time and than start new activity.
*/
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_screen.this,Login_screen.class);
startActivity(intent);
finish();
}
}

Categories

Resources