Onclick button beginer's android app isn't running: - android

I am new here,posting my first question.So this android project about showing a layout onclick of button,isn't running.When I run it,the emulator opens but my project isn't visible on it. I am trying to show another layout when button is clicked.Can anyone help me out?
My layouts are :activity_main,tutorial1 (layout to be showed after button:tutorial1 is clicked),splash(which is showed for 5 seconds on opening).
Also manja is name of sound that is played on opening,for 5 seconds.
Here's my code:
MainActivity.java:
package com.example.thebasics;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
public class MainActivity extends Activity {
MediaPlayer logoMusic;
#Override
protected void onCreate(Bundle AmanIsAwesome) {
super.onCreate(AmanIsAwesome);
setContentView(R.layout.splash);
logoMusic = MediaPlayer.create(MainActivity.this, R.raw.manja);
logoMusic.start();
Thread logoTimer= new Thread (){
public void run (){
try{
sleep(5000);
Intent menuIntent=new Intent("com.example.thebasics.MENU");
startActivity(menuIntent);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
finish();
}
}
};
logoTimer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
logoMusic.release();
}
#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;
}
}
Menu.java:
package com.example.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.activity_main);
Button tut1 = (Button) findViewById(R.id.tutorial1);
tut1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent ("com.example.thebasics.TUTORIAL"));
}
});
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}
Tutorial.java:
package com.example.thebasics;
import android.app.Activity;
import android.os.Bundle;
public class Tutorial extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tutorial1);
}
}
}
The basics androidmanifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.thebasics"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/download"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.thebasics.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAINACTIVITY" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.thebasics.MENU"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.thebasics.MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.thebasics.TUTORIAL"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.thebasics.TUTORIAL" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Also the console is showing the error:- Emulator[ Warning: No DNS servers found]
whats this?

I have uploaded a fully working of your project.Download it from here.Also use Intent as other people mentioned.
Intent intent = new Intent(MainActivity.this or getApplicationContext(), Tutorial.class);
/* First argument is your current activity.You can also mention it i.e CurrentActivity.this */
/* Second argument is the class where you wanna go i.e OtherActivity.class */
startActivity(intent);

Use this instead :
startActivity(new Intent (context, Tutorial.class));
Your way of doing it is strange too :
dont do this :
<activity
android:name="com.example.thebasics.TUTORIAL"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.thebasics.TUTORIAL" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Just do :
<activity
android:name="com.example.thebasics.Tutorial"
android:label="#string/app_name" >
</activity>
Same thing for the other one.
This doesn't exist :
<action android:name="com.example.thebasics.TUTORIAL" />

This line code
new Intent ("com.example.thebasics.TUTORIAL")
Creates intent with action name "com.example.thebasics.TUTORIAL"
What you want is:
startActivity(new Intent (MainActivity.this, Tutorial.class));
Just like Tsunaze suggested.

do as follow
Intent intent = new Intent(context, Tutorial.class);
// set some flags here according to your need
startActivity(intent);
Intent intent = new Intent(context, Menu.class);
// set some flags here according to your need
startActivity(intent);
and remove this from your manifest file as this is wrong
<intent-filter>
<action android:name="com.example.thebasics.MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<action android:name="com.example.thebasics.TUTORIAL" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>

Related

open another activity with a button not working

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.

Android: No Activity found to handle Intent error?

Im getting the error in the title when just messing around with android programming:
Code :
package co.uk.mypchealth.whatami;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Thread logoTimer = new Thread(){
public void run(){
try {
sleep(3000);
Intent menuIntent = new Intent("co.uk.mypchealth.whatami.JAVAMENU");
startActivity(menuIntent);
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
finish();
}
}
};
logoTimer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}
and the manifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="co.uk.mypchealth.whatami"
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" >
<activity
android:name="co.uk.mypchealth.whatami.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=".JavaMenu"
android:label="#string/app_name" >
<intent-filter>
<action android:name="co.uk.mypchealth.whatami.JAVAMENU" />
<category android:name="android.intent.category.DEFUALT" />
</intent-filter>
</activity>
</application>
</manifest>
Im Hoping its something simple i mean jeeze the app does nothing yet lol.... i have the intent declared and and all the names i have checked and double checked ... just cant see the wood from the trees. Any help would be great.
Try,
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
Intent menuIntent = new Intent(MainActivity.this, JavaMenu.class);
startActivity(menuIntent);
}
}, 3000);
try this :
<activity
android:name=".JavaMenu"
android:label="#string/app_name" >
<intent-filter>
<action android:name="co.uk.mypchealth.whatami.JAVAMENU" />
<category android:name="android.intent.category.DEFUALT" />
</intent-filter>
</activity>
you have defined the app name in both activities try to change this activity declaration to :
<activity android:name="co.uk.mypchealth.whatami.JavaMenu" > </activity>

Newbie in android programming, xml and class file not working

I have a problem I have created a simple list items and my item is Entertainment I have created the XML and Java file but after running into an emulator it doesn't work. I mean list items work, entertainment is here but clicking it won't make it to the next. Help
This is the main activity file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" >
</ListView>
</RelativeLayout>
This is the Java file:
package com.firstapp;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.VideoView;
public class Entertainment extends Activity {
MediaPlayer ourSong;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.entertainment);
ourSong = MediaPlayer.create(this, R.id.videoView1);
ourSong.start();
Thread timer = new Thread() {
public void run() {
try {
sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
Intent openStartingpoint = new Intent(
"com.bucky.android.Menu");
startActivity(openStartingpoint);
}
}
};
timer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
ourSong.release();
finish();
}
}
And this is the manifest
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.firstapp.Main"
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.firstapp.Entertainment"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.Main" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
In the Manifest, the tag "category" in your main activity intent is wrong:
<activity
android:name="com.firstapp.Main"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
You have to declare LAUNCHER instead of DEFAULT. See if it works after this change.
you have to override itemclick method of your listview for that
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
int position = arg2;
// click event handling
textQuestionCount.setText("Question " + String.valueOf(position + 1) + " of " + arg0.getCount());
}
and also implement the interface OnItemClickListener in your class.

Why won't my android application open/start?

When someone installs my app from the app store, the Open button (usually found next to the uninstall button upon installation completion) is grayed out, and the application cannot be found in their app drawer.
Also, when I execute from Eclipse choosing Default Activity, it says in the console that it installed the APK, but it does not start. If I select the Splash activity as the default activity in the run configuration, it runs without a hitch, but I still cannot find it in my app drawer afterwards. Help would be appreciated c:
Manifest:
<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" >
<activity
android:name="com.meteorfiber.gangnamstyle.Splash"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="com.meteorfiber.gangnamstyle.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.meteorfiber.gangnamstyle.MainActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="com.meteorfiber.gangnamstyle.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Splash:
package com.meteorfiber.gangnamstyle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
public class Splash extends Activity {
private final int SPLASH_DISPLAY_LENGTH = 3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
}
#Override
protected void onResume() {
super.onResume();
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(this);
boolean isSplashEnabled = sp.getBoolean("isSplashEnabled", true);
if (isSplashEnabled) {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Splash.this.finish();
Intent mainIntent = new Intent(Splash.this,
MainActivity.class);
Splash.this.startActivity(mainIntent);
}
}, SPLASH_DISPLAY_LENGTH);
} else {
finish();
Intent mainIntent = new Intent(Splash.this, MainActivity.class);
Splash.this.startActivity(mainIntent);
}
}
}
MainActivity:
package com.meteorfiber.gangnamstyle;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
public class MainActivity extends Activity {
MediaPlayer ourSong;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new GameView(this));
ourSong = MediaPlayer.create(MainActivity.this, R.raw.song);
ourSong.start();
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// preventing default implementation previous to
// android.os.Build.VERSION_CODES.ECLAIR
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
ourSong.release();
}
}
Change your intent filter for your starting activity to
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
The android launcher along with the settings activity try to find this intent-filter in your application in order to launch the activity. If it can't be found, your activity cannot be launched with a typical launcher and won't be displayed.

Stopping an Activity in Android

I had read about this thread around the Internet but unlucky to find a solution. The solutions that are available did not work for me like adding the android:noHistory="true" in the Manifest file. What I wanted is that after the user clicks the play game button, which will redirect the user into the select difficulty page, when the user clicks the back button (android emulator), it will go back to the main menu.
This is my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.kfc"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".NewKFCActivity"
android:label="#string/app_name"
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".HelpPageOne"
android:label="#string/app_name"
android:noHistory="true">
<intent-filter>
<action android:name="com.kfc.HELPPAGEONE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".HelpPageTwo"
android:label="#string/app_name"
android:noHistory="true">
<intent-filter>
<action android:name="com.kfc.HELPPAGETWO" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".SelectDifficulty"
android:label="#string/app_name"
android:noHistory="true">
<intent-filter>
<action android:name="com.kfc.SELECTDIFFICULTY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
This is my main activity:
package com.kfc;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
public class NewKFCActivity extends Activity {
/** Called when the activity is first created. */
ImageButton bPlay, bHelp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
bPlay = (ImageButton) findViewById(R.id.playGame);
bPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(v.getContext(), SelectDifficulty.class);
startActivityForResult(intent,0);
finish();
}
});
bHelp = (ImageButton) findViewById(R.id.Help);
bHelp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(v.getContext(), HelpPageOne.class);
startActivityForResult(intent,0);
finish();
}
});
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
This is my difficulty class:
package com.kfc;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
public class SelectDifficulty extends Activity{
ImageButton bEasy, bMedium, bHard;
#Override
protected void onCreate(Bundle kfcState) {
// TODO Auto-generated method stub
super.onCreate(kfcState);
setContentView(R.layout.difficultyxml);
bEasy = (ImageButton) findViewById(R.id.easy);
bEasy.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Intent intent = new Intent(v.getContext(), NewKFCActivity.class);
//startActivityForResult(intent,0);
}
});
}
}
First of all, you should know the difference between startActivity() and startActivityForResult(), see Android developers website.
Second, I think you should need to understand the lifecycle of an Activity, you don't need to finish() every activity once you leave it, Android will manage that for you.
Hope it helps :)
Cheers!
Remove the lines:
finish();
after
startActivityForResult(...);
in your Activity NewKFCActivity because finish() is destroying your activity and when you press back, there is no more an Activity to go back to!

Categories

Resources