open another activity with a button not working - android

So when this code runs I get no errors. It displays the buttons correctly and everything, but when I click on the button nothing happens (it just clicks and clicks and clicks nothing happens).
Below I will post the xml manifest so you can see that I have done everything perfectly, but somehow it it is not doing what I am asking it to do.
package com.Tripp.thebasics;
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.jokecatagories);
//setting up the button references
Button jokeD = (Button) findViewById(R.id.jokeoftheday);
Button jokeC = (Button) findViewById(R.id.jokecatagories);
jokeD.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(getApplicationContext(),JokeOfTheDay.class);
startActivity(i);
}
});
jokeC.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent s = new Intent(".JokeCatagories");
startActivity(s);
}
});
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}
The manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.Tripp.thebasics"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
>
<activity
android:name=".Main"
android:label="#string/app_name"
android:screenOrientation="portrait"
>
<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="android.intent.action.MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".com.Tripp.thebasics.JokeCatagories"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.JOKECATAGORIES" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Sweet"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.SWEET" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".com.Tripp.thebasics.JokeOfTheDay"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.JOKEOFTHEDAY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>

Change your OnClickListeners as follows...
jokeD.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Menu.this, JokeOfTheDay.class);
startActivity(i);
}
});
jokeC.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent s = new Intent(Menu.this, JokeCatagories.class);
startActivity(s);
}
});
Then get rid of this <intent-filter> from the JokeCategories <activity> section in the manifest...
<intent-filter>
<action android:name="android.intent.action.JOKECATAGORIES" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
...and get rid of this one from the JokeOfTheDay <activity> section...
<intent-filter>
<action android:name="android.intent.action.JOKEOFTHEDAY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
Basically, don't use the application Context to start an Activity (it's not necessary from an Activity as it has its own Context) and, on the whole, use explicit Intents to start Activities rather than defining <intent-filter> section in the manifest.

Related

Android Title Bar and Icon

When my app starts up i see the title bar and the application name with a white activity.
Even though my mainactivity is a splash screen, it does not start at first, but after 2 seconds of this Title bar and white activity. How do i disable this.
thanks for the help!
Splash.java
`public class Splash extends Activity {
#Override
protected void onCreate(Bundle parameter) {
// TODO Auto-generated method stub
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.splash);
super.onCreate(parameter);
Thread timer= new Thread()
{
#Override
public void run()
{
try
{
//Display for 3 seconds
sleep(3000);
}
catch (InterruptedException e)
{
// TODO: handle exception
e.printStackTrace();
}
finally
{
//Goes to Activity StartingPoint.java(STARTINGPOINT)
Intent openstartingpoint=new Intent("com.vault.beta.MAINACTIVITY");
startActivity(openstartingpoint);
}
}
};
timer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
`
Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.vault.beta"
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=".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="Vault - Login" >
<intent-filter>
<action android:name="com.vault.beta.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Screen"
android:label="Calender" >
<intent-filter>
<action android:name="com.vault.beta.SCREEN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".MenuList"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.vault.beta.MENULIST" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Password"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.vault.beta.PASSWORD" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".EditNote"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.vault.beta.EDITNOTE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Remove the theme of the main activity
android:theme="Theme.Holo.NoActionBar"

Activity Working on emulator but doesn't load on tablet

I am developing an android application, the problem is that all activities work fine on emulator but one of the activity doesn't load on tablet (Running 4.4 kitkat) and stopes the Application, MainPage.Class is not working.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.smarttrack"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/app_icon"
android:label="#string/app_name"
android:theme="#style/ActionBarTheme" >
<activity
android:name="com.smarttrack.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.smarttrack.LoginActivity"
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.smarttrack.MainPage"
android:label="#string/app_name"
android:theme="#style/ActionBarTheme"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.smarttrack.AskActivity"
android:label="#string/app_name"
android:theme="#style/ActionBarTheme"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.smarttrack.AskLocation"
android:label="#string/app_name"
android:theme="#style/ActionBarTheme"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.smarttrack.Options"
android:label="#string/app_name"
android:theme="#style/ActionBarTheme"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.smarttrack.Settings"
android:label="#string/app_name"
android:theme="#style/ActionBarTheme"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<receiver android:name="com.smarttrack.MsgBroadcast">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
MainPage.Class
package com.smarttrack;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainPage extends Activity implements OnClickListener{
ImageView img;
TextView statusText;
String status ;
Button askActivity;
Button askLocation;
Button addActivity;
Button optionsMenu;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main_page);
initVars();
img.setOnClickListener(this);
askActivity.setOnClickListener(this);
askLocation.setOnClickListener(this);
addActivity.setOnClickListener(this);
optionsMenu.setOnClickListener(this);
}
private void initVars() {
// TODO Auto-generated method stub
img = (ImageView)findViewById(R.id.switchingImage);
statusText = (TextView)findViewById(R.id.appStatus);
img.setImageResource(R.drawable.switch_off);
status = "active";
askActivity = (Button) findViewById(R.id.AskActivity);
askLocation = (Button) findViewById(R.id.btAskLocation);
addActivity = (Button) findViewById(R.id.addActivity);
optionsMenu = (Button) findViewById(R.id.optionsButton);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int id = v.getId();
switch(id){
case R.id.switchingImage:
Runnable swap = new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
if(status == "active"){
img.setImageResource(R.drawable.switch_off);
statusText.setText("Status = Running");
status = "unactive";
}else {
img.setImageResource(R.drawable.switch_on);
statusText.setText("Status = Stopped");
status = "active";
}
}
};
img.postDelayed(swap, 100);
break;
case R.id.AskActivity:
Intent i = new Intent(this,AskActivity.class);
startActivity(i);
break;
case R.id.btAskLocation:
Intent loc = new Intent(this,AskLocation.class);
startActivity(loc);
break;
case R.id.addActivity:
break;
case R.id.optionsButton:
Intent opt = new Intent(this,Options.class);
startActivity(opt);
break;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_bar_item, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.ActionBarHelp:
AlertDialog.Builder bldr = new AlertDialog.Builder(this);
bldr.setTitle("SmartTrack").setMessage("Cool application");
Dialog dlg = bldr.create();
dlg.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
in your manifest xml your target sdk is 18 but 4.4 can run on 19

Android Splash activity not redirecting

I have created a splash activity for my app. I am trying to redirect from my Splash activity to the Starting Point activity using AndroidManifest.xml but its not working. The Splash class loads up, and then it doesn't redirect to the Starting Point. And it doesn't give any errors either.
So can you please help me figure it out.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
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.myapp.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.myapp.StartingPoint"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.myapp.STARTINGPOINT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Now I have also tried
<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=".StartingPoint"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.myapp.STARTINGPOINT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
So what can I do here. I am not getting redirected from Splash to my Starting Point.
Splash Code
package com.example.myapp;
import android.app.Activity;
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);
}
}
Add this code to your Splash.java
Thread timer = new Thread(){
public void run(){
try {
sleep(2000);
} catch (Exception e) {
// TODO: handle exception
} finally{
Intent i = new Intent(Flash_Activity.this, StartingPoint.class);
startActivity(i);
}
}
};
timer.start();
}
You are never calling anything to change the activity from your splash to the starting point. This thread will do that.
Use this easy way to redirect to acitvity
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(this,MainActivity.class);
startActivity(intent);
finish();
}
},2500);

Activity B starts before Activity A

I was trying to get a small image to show up before my Main Activity starts. This is my current coding in the android manifest
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
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" >
<intent-filter>
<action android:name="com.example.test.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
whatever I do, my splash screen does not start. They start separately, but never together, (I still haven't put a timer in my splash image as I want to check whether it works or not and it isn't working)
Remove
<intent-filter>
<action android:name="com.example.test.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
Use below code in splash screen after desired time
Intent intent=new Intent(this,MAINACTIVITY.class);
startActivity(intent);
finish(); //To close splashscreen when MAINACTIVITY loads
The above code starts with splash screen and after some time start your main activity
You should remove the <intent-filter> section from your MainActivity declaration ,
and launch the MainActivity from the splashActivity using a simple intent and startActivity call.
Try this.
<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" >
</activity>
</application>
Code for your Splash Class-
package com.example.test;
import android.app.Activity;
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);
final Handler handle = new Handler();
Runnable delay = new Runnable() {
public void run() {
startActivity(new Intent(Splash.this, MainActivity.class));
finish();
}
};
handle.postDelayed(delay,5000);
}
}
its delay next intent 5 second. you can set time according to you.
Use This:
<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" >
</activity>
</application>
and This :
public class Splash extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
Intent mInHome = new Intent(Splash.this, MainActivity.class);
Splash.this.startActivity(mInHome);
Splash.this.finish();
}
}, 3000);
}
}

Why different activities appear as separate application in android

I am creating a Jigsaw Puzzle application in android. I have created two activites, activity_jigsaw.xml and activity_level.xml. One activity is created by default (Displaying Hello World!) which I modified and created a new activity by following these steps:
File -> New -> Other -> Android Activity
But when I install the application these two files (and all other activities of the application) are installed as a separate project. But at the same time they are also interlinked. The Java code of the files as follows:
Jisaw.java file contains:
public class Jigsaw extends Activity {
Intent intent;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_jigsaw);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_jigsaw, menu);
return true;
}
public void play(View v)
{
try
{
intent = new Intent(this, Level.class);
startActivity(intent);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Here Play is a function which is called when an image is clicked.
Level.java file contains:
public class Level extends Activity {
Intent intent;
String level = "";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_level);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_level, menu);
return true;
}
public void easy(View v)
{
level = "easy";
intent = new Intent(this, Play.class);
intent.putExtra("level", level);
startActivity(intent);
}
public void medium(View v)
{
level = "medium";
intent = new Intent(this, Play.class);
intent.putExtra("level", level);
startActivity(intent);
}
public void hard(View v)
{
level = "hard";
intent = new Intent(this, Play.class);
intent.putExtra("level", level);
startActivity(intent);
}
}
Functions easy, medium and hard are called when a corresponding image is clicked.
Can somebody please tell me that what I am doing wrong?
Thanks in advance..
Here is the manifest file:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.maju.jigsawpuzzle"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.SET_WALLPAPER" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Jigsaw"
android:label="#string/title_activity_jigsaw" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Level"
android:label="#string/title_activity_level" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Play"
android:label="#string/title_activity_play" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".PlayBoard"
android:label="#string/title_activity_play_board" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
You probably are defining android.intent.category.LAUNCHER intent category to all the activities in your AndroidManifest.xml, it creates an icon in the app launcher. Activities other than main should not have this intent filter.
Do something like this:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity android:name=".JigSaw" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Level" />
<activity android:name=".Play" />
<activity android:name=".Playboard" />
</application>
EDIT:
As you just posted, you are indeed doing that, just remove the intent filter from other activities.

Categories

Resources