Timer is not working. after few second it should goto another activity
public class intro extends Activity {
int count=0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.intro);
final Toast tosta = Toast.makeText(this, String.valueOf(count+"."), Toast.LENGTH_SHORT);
final Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
count++;
if(count == 5){
Intent app = new Intent("com.jasrajcomputers.MainActivity");
startActivity(app);
}
tosta.show();
}
}, 1000, 1000);
}}
but after 5 sec app unfortunately has stopped.
manifext.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jasrajcomputers"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:theme="#style/noactionbarvirat"
android:label="#string/app_name" >
<activity
android:name=".intro"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.jasrajcomputers.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.jasrajcomputers.MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
MY LOGCAT:
07-12 12:46:38.979: E/AndroidRuntime(14495): FATAL EXCEPTION: Timer-0
07-12 12:46:38.979: E/AndroidRuntime(14495): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.jasrajcomputers.MainActivity }
07-12 12:46:38.979: E/AndroidRuntime(14495): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1697)
07-12 12:46:38.979: E/AndroidRuntime(14495): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1492)
07-12 12:46:38.979: E/AndroidRuntime(14495): at android.app.Activity.startActivityForResult(Activity.java:3388)
07-12 12:46:38.979: E/AndroidRuntime(14495): at android.app.Activity.startActivityForResult(Activity.java:3349)
07-12 12:46:38.979: E/AndroidRuntime(14495): at android.app.Activity.startActivity(Activity.java:3584)
07-12 12:46:38.979: E/AndroidRuntime(14495): at android.app.Activity.startActivity(Activity.java:3552)
07-12 12:46:38.979: E/AndroidRuntime(14495): at com.jasrajcomputers.intro$1.run(intro.java:72)
07-12 12:46:38.979: E/AndroidRuntime(14495): at java.util.Timer$TimerImpl.run(Timer.java:284)
Just have
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:theme="#style/noactionbarvirat"
android:label="#string/app_name" >
<activity
android:name=".intro"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
</activity>
</application>
Use Explicit intents
Intent app = new Intent(intro.this, MainActivity.class);
Remove
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.jasrajcomputers.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.jasrajcomputers.MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
Update :
Timer is not required here. You can use a Handler instead.
Quoting docs
Explicit intents specify the component to start by name (the
fully-qualified class name). You'll typically use an explicit intent
to start a component in your own app, because you know the class name
of the activity or service you want to start. For example, start a new
activity in response to a user action or start a service to download a
file in the background.
Use a Handler
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
Intent app = new Intent("com.example.MainActivity");
startActivity(app);
}
}, 5000);
Try to replace this code :
Intent app = new Intent("com.jasrajcomputers.MainActivity");
With this code :
Intent app = new Intent(intro.this,MainActivity.class);
Note :
MainActivity must be define in AndroidManifest.xml
Related
Just started learning android app develpoment.
getting error "android.content.activitynotfoundexception no activity found"
Code:-
my Android Mainfest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gtctest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="14" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".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>
</application>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".StartingPointActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.GTC.STARTINGPOINTACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
and my Splash Screen.java
package com.example.gtctest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class SplashScreen extends Activity {
#Override
protected void onCreate(Bundle splashBundle) {
// TODO Auto-generated method stub
super.onCreate(splashBundle);
setContentView(R.layout.splash);
Thread timer=new Thread() {
public void run() {
try {
sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
Intent openStartingPointActivityIntent=new Intent("com.GTC.STARTINGPOINTACTIVITY");
startActivity(openStartingPointActivityIntent);
}
}
};
timer.start();
}
#Override
protected void onPause() {
super.onPause();
finish();
}
}
error:-
09-15 14:31:19.587: E/AndroidRuntime(1088): FATAL EXCEPTION: Thread-123
09-15 14:31:19.587: E/AndroidRuntime(1088): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.GTC.STARTINGPOINTACTIVITY }
09-15 14:31:19.587: E/AndroidRuntime(1088): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1512)
09-15 14:31:19.587: E/AndroidRuntime(1088): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1384)
09-15 14:31:19.587: E/AndroidRuntime(1088): at android.app.Activity.startActivityForResult(Activity.java:3190)
09-15 14:31:19.587: E/AndroidRuntime(1088): at android.app.Activity.startActivity(Activity.java:3297)
09-15 14:31:19.587: E/AndroidRuntime(1088): at com.example.gtctest.SplashScreen$1.run(SplashScreen.java:28)
Cant't figure out what is wrong.please help to solve this issue
And one more question
In android mainfest the name action name can be anything or it has to be path from package like "com.GTC.classname"
you have two application tags in your manifest. change it to one with both activities
please modify your manifest like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gtctest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="14" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".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=".StartingPointActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.GTC.STARTINGPOINTACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
ps: in android mainfest, the action name can be anything, but we used to make it like "com.xxx.intent.action.XXX".
Try this way,hope this will help you to solve your problem.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gtctest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="14" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".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>
</application>
<activity
android:name=".StartingPointActivity"
android:label="#string/app_name"/>
</manifest>
public class SplashScreen extends Activity {
#Override
protected void onCreate(Bundle splashBundle) {
// TODO Auto-generated method stub
super.onCreate(splashBundle);
setContentView(R.layout.splash);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(SplashScreen.this,StartingPointActivity.class);
startActivity(intent);
finish();
}
},3000);
}
#Override
protected void onPause() {
super.onPause();
}
}
Create Intent Like Following...
Intent intentname =new Intent(SplashScreen.this,StartingPointActivity.class);
startActivity(intentname);
Change in your AndroidManifest.xml File..
You have defined two <application> tag....
remove that and change it to following..
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gtctest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="14" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".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>
<!-- add new activities like this -->
<activity
android:name=".StartingPointActivity"
android:label="#string/app_name" >
<!-- if your activity in different package you can define like this... (xx.xxx is your package name define your package name there..)
<activity
android:name="xx.xxx.StartingPointActivity"
android:label="#string/app_name" >
-->
</application>
</manifest>
Within my application I am getting the following error from logcat, What is the error with my code? I have searched for over an hour online and cannot solve the issue.
It is relating to the intent that I am trying to use in order to open up a new activity but I am not sure how to solve it.
07-20 20:02:18.976: E/AndroidRuntime(7381): FATAL EXCEPTION: Thread-9022
07-20 20:02:18.976: E/AndroidRuntime(7381): Process: com.example.brianapp, PID: 7381
07-20 20:02:18.976: E/AndroidRuntime(7381): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.example.brianapp.MeditationResults }
07-20 20:02:18.976: E/AndroidRuntime(7381): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1672)
07-20 20:02:18.976: E/AndroidRuntime(7381): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1442)
07-20 20:02:18.976: E/AndroidRuntime(7381): at android.app.Activity.startActivityForResult(Activity.java:3511)
07-20 20:02:18.976: E/AndroidRuntime(7381): at android.app.Activity.startActivityForResult(Activity.java:3472)
07-20 20:02:18.976: E/AndroidRuntime(7381): at android.app.Activity.startActivity(Activity.java:3714)
07-20 20:02:18.976: E/AndroidRuntime(7381): at android.app.Activity.startActivity(Activity.java:3682)
07-20 20:02:18.976: E/AndroidRuntime(7381): at com.example.brianapp.Meditation$2.run(Meditation.java:115)
It is relating from this code section as this error only occured once I changed this code section:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.meditation);
//new thread
Thread timer = new Thread() {
/**
* Method that firstly starts the thread and makes it sleep,
* then using intents opens the main activity using
* a reference to its name
*/
public void run() {
try {
//sleep for 3.5 seconds
sleep(15000);
} catch (InterruptedException e) {
// exceptions caught here
e.printStackTrace();
} finally {
Intent openActivity = new Intent("com.example.brianapp.MeditationResults");
// //Start activity
startActivity(openActivity);
}
}
};
//Start timer
timer.start();
The following is my manifest, note that I have included an intent filter for this class (meditation), the class I am trying to open with the intent is called MeditationResults:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.brianapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.brianapp.Menu"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.brianapp.menu" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.brianapp.Meditation"
android:label="Meditation" >
<intent-filter>
<action android:name="com.example.brianapp.meditation" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.brianapp.MeditationResults"
android:label="#string/title_activity_meditation_results" >
</activity>
<activity
android:name="com.example.brianapp.UserName"
android:label="#string/title_activity_user_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.brianapp.ServerInterface"
android:label="#string/title_activity_server_interface" >
</activity>
</application>
</manifest>
Change this in your manifest to
<activity
android:name="com.example.brianapp.MeditationResults"
android:label="#string/title_activity_meditation_results" >
</activity>
to
<activity
android:name="com.example.brianapp.MeditationResults"
android:label="#string/title_activity_meditation_results" >
<intent-filter>
<action android:name="com.example.brianapp.MeditationResults" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
I have a splash screen with a timer that runs for 3 seconds. After the 3 seconds it is supposed to auto load another activity called Dashboard. I made a new class that I want to have load after the splash screen called WelcomeSplash. I changed the classes in the splash screen from Dashboard to WelcomeSplash and the app force closes and the logcat says it has a null point exception. Here is the snip of code from the splash class and logcat snip.
02-13 23:18:46.826: E/AndroidRuntime(2475): FATAL EXCEPTION: main
02-13 23:18:46.826: E/AndroidRuntime(2475): java.lang.RuntimeException: Unable to start
activity ComponentInfo{com.magicbuddy.gamble/com.magicbuddy.gamble.welcomeSplash}:
java.lang.NullPointerException
02-13 23:18:46.826: E/AndroidRuntime(2475): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
Here is the snip of the Splash code
startActivity(new Intent(Splash.this, welcomeSplash.class));
When I change the welcomeSPlash.class to Dashboard.class the app does not force close. Here is the welcomeSplash Activity,
public class welcomeSplash extends Activity implements OnClickListener {
Button btnskip;
Button btnplay;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
btnskip =(Button) findViewById(R.id.btnSkip);
btnskip.setOnClickListener(this);
btnplay =(Button) findViewById(R.id.btnOk);
btnplay.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnSkip:
Intent a = new Intent(welcomeSplash.this, Dashboard.class);
startActivity(a);
break;
case R.id.btnOk:
Intent i = new Intent(welcomeSplash.this, Profile.class);
startActivity(i);
break;
}
}
}
THe manifest is copied below too.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.magicbuddy.gamble"
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="com.magicbuddy.gamble.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.magicbuddy.gamble.welcomeSplash"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.magicbuddy.gamble.Dashboard" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.magicbuddy.gamble.Dashboard"
android:label="#string/app_name" >
</activity>
<activity
android:name="com.magicbuddy.gamble.MainActivity"
android:label="#string/app_name" >
</activity>
<activity
android:name="com.magicbuddy.gamble.Player"
android:label="#string/app_name" >
</activity>
<activity
android:name="com.magicbuddy.gamble.Menu"
android:label="#string/app_name" >
</activity>
<activity
android:name="com.magicbuddy.gamble.Popup"
android:label="#string/title_activity_popup" >
</activity>
</application>
</manifest>
Remove this code from your menifest
<intent-filter>
<action android:name="com.magicbuddy.gamble.Dashboard" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
07-18 04:48:22.465: E/AndroidRuntime(19105): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.liamwli.fa.yearbook/com.liamwli.fa.yearbook.Home}: java.lang.NullPointerException
That is the error I get.
I have defined the Home class in the manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.liamwli.fa.yearbook"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.Holo" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Home"
android:label="#string/app_name"
android:theme="#android:style/Theme.Holo" >
<intent-filter>
<action android:name="com.liamwli.fa.yearbook.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
And it only started doing this when I added the putExtras line in my main activity:
enter.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
myname = name.getText().toString();
Intent i = new Intent("com.liamwli.fa.yearbook.HOME");
i.putExtra("myname", myname);
startActivity(i);
}
});
So, can anyone please explain what is happening?
use this:
Intent i = new Intent(context,otherActivity.class);
context of that activity from where you want to start activity.otherActivity is the name of activity which you want to start
Try this once
Intent i = new Intent(MainActivity.this,Home.class);
i.putExtra("myname", myname);
startActivity(i);
And I don't think this is required in Manifest
<intent-filter>
<action android:name="com.liamwli.fa.yearbook.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
change like this
Intent i = new Intent(MainActivity.this, Home.class);
i.putExtra("myname", myname);
startActivity(i);
Try this...
Intent i = new Intent( YourActivityName.this , otherActivity.class);
Ok, I looked and it looked like I was setting a variable contents outside of the onCreate method. So that is why it wasn't working.
I'm trying to call a service from a activity:
When I'm running the program the activity throws an error which says: Not allowed to start service Intent. What am I doing wrong? I'm sorry for possible stupid mistakes in the code, but I'm a newbie.
activity code:
public void startService() {
try {
startService (new Intent ( this , SmsReminderService.class)) ; }
catch (Exception e ) { Log.v("Error" , e.getMessage()) }
}
service code :
public class SmsReminderService extends Service {
#Override
public void onStart(Intent intent, int startid) {
Log.v("SSms", "Service started") ; }}
manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="me.sms.smsReminder"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<permission android:name="SEND_SMS"></permission>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".SmsReminderActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".SmsReminderService"
android:permission="android.permission.BIND_REMOTEVIEWS">
<intent-filter>
<action android:name = "android.intent.category.LAUNCHER" ></action>
</intent-filter>
</service>
</application>
</manifest>
Thanks in advance, Tom
Why this in the service?
<intent-filter>
<action android:name = "android.intent.category.LAUNCHER" ></action>
</intent-filter>
Try to add this:
<uses-permission android:name="android.permission.BIND_REMOTEVIEWS" />