I'm pretty new to Android programming, but i have never encountered this problem before. When i click the button, the emulator is just not doing anything, when it's supposed to start another activity. Here's my intent within the button:
public class StartScreen extends Activity implements OnClickListener {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start_screen);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.bStartQuiz:
Intent a = new Intent(StartScreen.this, QuizMenu.class);
startActivity(a);
break;
Here follows some more cases which are not defined yet.
onCreate method from the class i try to start:
public class QuizMenu extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.categories);
Here is the manifest:
<activity
android:name="com.ultimatequiz.StartScreen"
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.ultimatequiz.QuizMenu"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.ultimatequiz.QuizMenu" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
What i really don't understand is that i get no errors in the log. It's just not "responding", but the button do change into state_pressed.
If I understood correctly you want to open a new activity by clicking a Button. Then all you have to do is to have a Button in your activity_start_screen.xml:
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/your_button"/>
After that you have to add this in your StartScreen:
public class StartScreen extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start_screen);
Button button = (Button) findViewById(R.id.your_button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent activityChangeIntent = new Intent(StartScreen.this,QuizMenu.class);
StartScreen.this.startActivity(activityChangeIntent);
}
});
and only this in your manifest:
<activity
android:name="com.ultimatequiz.StartScreen"
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.ultimatequiz.QuizMenu"
android:label="#string/app_name"/>
I dont know if your case works well.Try this.
public class StartScreen extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start_screen);
Button myBtn = (Button) findViewById(R.id.btn);
myBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent a = new Intent(StartScreen.this, QuizMenu.class);
startActivity(a);
}
});
}
}
I always use this way, I find it easy to handle.
if you got your layout defined onlick delete it
Try to do a function for it :
public static void LaunchIntent(){
Intent a = new Intent(StartScreen.this, QuizMenu.class);
startActivity(a);
and in the click listener call the function :
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.bStartQuiz:
LaunchIntent();
break;
}
Related
I would like to make a call using one button. The phone number should be inside. But my code is inoperative. Who can support?
public class MainActivity extends AppCompatActivity {
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
}
public void onClickStart(View view) {
button = findViewById(R.id.button);
((Button)findViewById(R.id.button)).setOnClickListener((View.OnClickListener) v -> {
Intent callIntent = new Intent(Intent.ACTION_CALL);
String phNum = "tel:" + "89261234567";
callIntent.setData(Uri.parse(phNum));
startActivity( callIntent) ;
});
}
}
manifest file
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.GateApp">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Your problem is that you're never assigning the click listener to the button, since onClickStart is never invoked. This is how you should do it
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener((View.OnClickListener) v -> {
Intent callIntent = new Intent(Intent.ACTION_CALL);
String phNum = "tel:" + "89261234567";
callIntent.setData(Uri.parse(phNum));
startActivity( callIntent) ;
});
}
This way you're assigning the click listener to the button. In the documentation you can find how to use it.
Here it is the link at the call intent documentation
Just use this two lines of code inside onClickListener:
((Button)findViewById(R.id.button)).setOnClickListener((View.OnClickListener) v -> {
String phNum = "tel:" + "89261234567";
startActivity(new Intent(Intent.ACTION_DIAL, Uri.fromParts("tel", phNum, null)));
});
I think you can add to manifest file this string
<data android:scheme="tel" />
// This works well!
((Button) findViewById(R.id.button1)).setOnClickListener((View.OnClickListener) v -> {
Intent callIntent = new Intent(Intent.ACTION_CALL);
String phNum = "tel:" + "87777777777";
callIntent.setData(Uri.parse(phNum));
startActivity(callIntent);
});
I'm pretty new to Java and xml i'm looking to have a splash screen run when i start up my app for about 5 seconds. I've taken code for the splash screen from stack overflow to set it up but i cant get it to run for some reason could anybody help me out!
Cheers
MY splash class
package com.darraghoflaherty.competer.game;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
public class Splash extends Activity {
/** Duration of wait **/
private final int SPLASH_DISPLAY_LENGTH = 5000;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.splashscreen);
/* New Handler to start the Menu-Activity
* and close this Splash-Screen after some seconds.*/
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
/* Create an Intent that will start the Menu-Activity. */
Intent mainIntent = new Intent(Splash.this,Menu.class);
Splash.this.startActivity(mainIntent);
Splash.this.finish();
}
}, SPLASH_DISPLAY_LENGTH);
}
My xml code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0099FF">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/ss1"
android:id="#+id/ss1"
android:textColor="#ffffff"
android:textSize="260sp"/>
</RelativeLayout>
first change this peace of code :
public void run() {
/* Create an Intent that will start the Menu-Activity. */
Intent mainIntent = new Intent(Splash.this,Menu.class);
Splash.this.startActivity(mainIntent);
Splash.this.finish();
}
To
public void run() {
/* Create an Intent that will start the Menu-Activity. */
Intent mainIntent = new Intent(getApplicationContext(),Menu.class);
startActivity(mainIntent);
finish();
}
Now your code is clear, the error must come from the manifest file.
Go to the manifest file and change the position of the intent-filter from the Mainactivity to the slashscreen activity.
here the code :
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
My guess is that you have not changed the launcher activity in the manifest. Android looks in the AndroidManifest.xml to select the activity to start first. Your manifest probably contains these lines:
<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>
This should be changed to:
<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=".Menu"/>
It is also a good convention to name activities XyzActivity, so in your case MenuActivity and SplashActivity.
/* New Handler to start the Menu-Activity
* and close this Splash-Screen after some seconds.*/
Thread timer = new Thread(){
public void run(){
try{
Thread.sleep(2000);
}
catch(InterruptedException e){
e.printStackTrace();
}
finally{
Intent mainIntent = new Intent(Splash.this,Menu.class);
startActivity(mainIntent );
finish();
}
}
};// end thread
timer.start();
use this code for the splash screen ...
public class SPLASH extends Activity {
protected boolean _active = true;
protected int _splashTime = 3000; // time to display the splash screen in MICROSECONDS
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Thread splashTread = new Thread() {
#Override
public void run() {
try {
int waited = 0;
while (_active && (waited < _splashTime)) {
sleep(100);
if (_active) {
waited += 100;
}
}
} catch (Exception e) {
} finally {
startActivity(new Intent(Splash.this,Menu.class));
finish();
}
};
};
splashTread.start();
}
}
and make the Splash activity a launcher activity by using
<activity
android:name=".SPLASH"
android:label="#string/app_name"
android:theme="#style/NoActionBar"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Hope this will help u
Hey checkOut it i have improve little bit. thanks
public class Splash extends Activity {
private final int SPLASH_DISPLAY_LENGTH = 5000;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.splashscreen);
nav();
}
public void nav() {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent mainIntent = new Intent(Splash.this,Menu.class);
startActivity(mainIntent);
finish();
}
}, SPLASH_DISPLAY_LENGTH);
}
#Override
protected void onPause() {
super.onPause();
finish();
}
}
I havesetup a splash screen from where I want it to redirect to my main activity after 5 sec but my application always stays on the splash screen and never redirects.
I have following code in my SplashActivity.Java
public class SplashActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.SplashActivity);
Thread timer = new Thread() {
public void run() {
try {
// sleep(R.integer.SplashActivityTime);
sleep(5000);
} catch (InterruptedException iEx) {
iEx.printStackTrace();
} finally {
Intent mainActivity = new Intent(
"com.myApp.myApp.MainActivity");
startActivity(mainActivity);
}
}
};
timer.start();
}
}
And in Manifest I Have:
<activity
android:name="com.myApp.myApp.SplashActivity"
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.myApp.myApp.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.myApp.myApp.MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Use this code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Thread timer = new Thread() {
public void run() {
try {
// sleep(R.integer.SplashActivityTime);
sleep(5000);
} catch (InterruptedException iEx) {
iEx.printStackTrace();
} finally {
Intent mainActivity = new Intent(MainActivity.this,
SecondActivity.class);
startActivity(mainActivity);
finish();
}
}
};
timer.start();
}
And in manifest
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.sample.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.example.sample.SecondActivity"
android:label="#string/app_name" >
</activity>
</application>
Change your Intent:
Intent mainActivity = new Intent(this, MainActivity.class);
In addition, which class is the owner of that sleep method?
Remove intent-filter for main activity's activity in manifest file
And best way to implement splash screen click here
and starting intent line change to
Intent mainActivity = new Intent(SplashActivity .this, MainActivity.class);
use the following code
Thread logoTimer = new Thread()
{
#Override
public void run()
{
try
{
sleep(3000);
startActivity(new Intent(SplashActivity.this, MainActivity.class));
}//End of try block
catch (InterruptedException e)
{
e.printStackTrace();
}//End of catch block
finally
{
finish();
}//End of finally block
}//End of run method
};//End of anonymous thread inner class
logoTimer.start();
In Manifest
<activity
android:name="com.myApp.myApp.SplashActivity"
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.myApp.myApp.MainActivity"
android:label="#string/app_name" >
</activity>
Maybe Try this :
public class splashscreen extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash); // Your Splash Layout
Handler handler = new Handler();
// run a thread
handler.postDelayed(new Runnable() {
public void run() {
// make sure we close the splash screen so the user won't come back when it presses back key
finish();
// start the home screen
Intent intent = new Intent(splashscreen.this, TabDemo2Activity.class);
splashscreen.this.startActivity(intent);
}
}, 5000); // time in milliseconds (1 second = 1000 milliseconds) until the run() method will be called
}
}
use Handler as below it's work for me.
Handler handler = new Handler();
// run a thread after 2 seconds to start the home screen
handler.postDelayed(new Runnable() {
#Override
public void run() {
finish();
if (!mIsBackButtonPressed) {
// start next activity
Intent intent = new Intent(SplashScreen.this, SEOshopMainActivity.class);
startActivity(intent);
}
}
}, 2000); // time in milliseconds (1 second = 1000 milliseconds) until the run() method will be called
I am following an youtube tutorial to create android apps.
In one of the tutorials they told how to create a Menu class using List activity.
public class Menu extends ListActivity {
String classes[] = {"Start","example1","example2","example3","example4","example5","example6"};
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1, classes));
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String cheese = classes[position];
try{
Class ourClass = Class.forName("com.example.add." + cheese);
Intent ourIntent = new Intent(Menu.this,ourClass);
startActivity(ourIntent);
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}
And add activity in the manifest.
<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:exported="false"
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:exported="false"
android:name="com.example.add.Start"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.add.START" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
My project has a Start class which adds one to a counter and display it,
a Splash class which defines background image, music and it is the main class.
Before adding Menu class my app worked fine,but after i added it my app began to force close and logcat showed an ActivityNotFoundException and error in Splash class at line 28
public class Splash extends Activity{
MediaPlayer ourSong;
#Override
protected void onCreate(Bundle Dab) {
// TODO Auto-generated method stub
super.onCreate(Dab);
setContentView(R.layout.splash);
ourSong = MediaPlayer.create(Splash.this, R.raw.nodebeat);
ourSong.start();
Thread timer = new Thread(){
public void run(){ //framework to start a new thread
try{
sleep(5000);
}catch(InterruptedException e){
e.printStackTrace();
}finally{
Intent openStart = new Intent("com.example.add.MENU");
startActivity(openStart);
}
}
};
timer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
ourSong.release();
finish();
}
So please help i am new to android app development and im struck here.
Links to the Logcat logs:-
https://docs.google.com/file/d/0BxLaXhL-q50-MHlhUW93SmxNT0U/edit Debug Log
https://docs.google.com/file/d/0BxLaXhL-q50-b1pzbVpIcVNQR2s/edit Error Log
Thanks.
You are using the wrong intent filter for what you're trying to launch. So either change
<action android:name="android.intent.action.MENU" />
to
<action android:name="com.example.add.MENU" />
So that everything matches.
Or change your call in your code so it is:
Intent openStart = new Intent("android.intent.action.MENU");
You should read about Intent Filters.
You could also use the explict intent, since you're not exporting this Activity and you know its name, I don't see a need for an intent filter.
Intent openStart = new Intent(Splash.this, Menu.class);
startActivity(openStart);
Intent action in below both(1 and 2) places should be same , which are not. make them identical.
<intent-filter>
<action android:name="android.intent.action.MENU" />// (1)
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
and
Intent openStart = new Intent("com.example.add.MENU"); (2)
sorry for asking dumb questions, java and Android are both new to me ;)
My problem: I can't switch between two activities in a very simple app. I tried solutions described in similar topics but it didn't work.
So this is my 1st Activity (I didn't paste the imports):
public class OneActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void OnStart(){
Button Btn = (Button)findViewById(R.id.btnNext);
Btn.setOnClickListener(new OnClickListener() {
public void onClick(View Button) {
Intent myIntent = new Intent(OneActivity.this, UserInput.class);
OneActivity.this.startActivity(myIntent);
}
});
}
}
The second Activity is very simple - it is just supposed to load a layout called userinput.xml:
public class UserInput extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.userinput);
}
}
The application part of the Manifest looks like following:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".OneActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<activity
android:name=".UserInput"
android:label="#string/app_name" />
</activity>
</application>
When I run the app and click the button nothing happens. Where could be the problem?
// Alright, I have put the code into the onCreate() method so it now looks like following:
public class OneActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button Btn = (Button)findViewById(R.id.btnNext);
Btn.setOnClickListener(new OnClickListener() {
public void onClick(View Button) {
Intent myIntent = new Intent(OneActivity.this, UserInput.class);
OneActivity.this.startActivity(myIntent);
}
});
}
}
Now the app crashes (force close) anytime I click the Next button.
You define your onStart() function with a capital 'O'. That is why the function is never called.
Your onStart():
public void OnStart(){ ... }
How it should be:
// Note the lowercase 'o' in onStart
public void onStart(){ ... }
Also note that having an #Override above the function name when you want to override a method will help prevent making these mistakes, as Eclipse (or whatever IDE you use) will tell you that you are not actually overriding a function.
Write below code in onCreate() method:
Button Btn = (Button)findViewById(R.id.btnNext);
Btn.setOnClickListener(new OnClickListener() {
public void onClick(View Button) {
Intent myIntent = new Intent(OneActivity.this, UserInput.class);
startActivity(myIntent);
}
});
2nd correction for android manifest file:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".OneActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> <!-- closed here -->
<activity
android:name=".UserInput"
android:label="#string/app_name" />
</application>
You can move all the code in onCreate() and simplify like this
startActivity(new Intent(this, UserInput.class));
But I don't understand why you start another activity like this
Check it following line
Intent myIntent = new Intent(OneAvtivity.this, SecondActivity.class);
startActivity(myIntent);
Your Class name is UserIpnout and you write SecondActivity.class
Intent myIntent = new Intent(OneAvtivity.this, UserInput .class);
startActivity(myIntent);