My splash screen gets error and it is going to force close the app. in Logcat is says Permission Denied. What should i do to fix this problem. Can anyone help me with that matter, Thanks a lot
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
public class SplashScreen extends Activity {
protected boolean _active = true;
protected int _splashTime = 4000;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
// thread for displaying the SplashScreen
Thread splashTread = new Thread() {
#Override
public void run() {
try {
int waited = 0;
while(_active && (waited < _splashTime)) {
sleep(100);
if(_active) {
waited += 100;
}
}
} catch(InterruptedException e) {
// do nothing
} finally {
finish();
startActivity(new Intent("com.droidnova.android.splashscreen.BodyPartsGameActivity"));
stop();
}
}
};
splashTread.start();
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
_active = false;
}
return true;
}
}
do this it will easier to you:
private ImageView img;
private Thread t;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
img = (ImageView)findViewById(R.id.im1);
t = new Thread(){
#Override
public void run() {
try {
synchronized(this){
wait(2000);
}
// Here your code of animation
}
catch(InterruptedException ex){
ex.printStackTrace();
}
Intent i = new Intent(QuizActivity.this,main.class);
startActivity(i);
}
};
t.start();
}
Your Problem is that you don't add the QuizActivity to the AndroidMainfest.xml
<activity android:name="QuizActivity" ></activity>
than your problem should be solved.
and befor you start the new Activity in splash make
finish();
than you cant go back with back key to it =)
Can you try this once..
Thread splashTread = new Thread() {
#Override
public void run() {
try {
int waited = 0;
while(_active && (waited < _splashTime)) {
sleep(100);
if(_active) {
waited += 100;
}
}
} catch(InterruptedException e) {
// do nothing
} finally {
runOnUiThread(new Runnable() {
#Override
public void run() {
finish();
startActivity(new Intent("com.droidnova.android.splashscreen.BodyPartsGameActivity"));
stop();
}
});
}
}
};
Related
I write a Splash Screeen to run at the boot time of application
public class SplashScreen extends Activity {
ImageView imgView;
int[] imgID = new int[]{R.drawable.frame0, R.drawable.frame1, R.drawable.frame2, R.drawable.frame3,
R.drawable.frame4, R.drawable.frame5, R.drawable.frame6};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
imgView = (ImageView) findViewById(R.id.imgSplash);
new Thread(new WelcomeScreen()).start();
}
private class WelcomeScreen implements Runnable {
#Override
public void run() {
try {
for (int i = 0; i < imgID.length; i++)
{
imgView.setImageResource(imgID[i]);
sleep(500);
}
} catch (InterruptedException e) {
}finally {
Intent intent = new Intent(SplashScreen.this,LoginActivity.class);
startActivity(intent);
finish();
}
}
}
}
It getting error "Sorry the application has stopped unexpectedly" . I don't know why . Somebody can help me ????
you can not set the resource for yuor ImageView inside a thread different from the UI Thread.
you can use runOnUiThread. It takes as paramter a runnable, and post it in the UI Thread queue. There, the UI thead takes it and update your ImageView. All in all your runnable will become:
private class WelcomeScreen implements Runnable {
#Override
public void run() {
try {
for (int i = 0; i < imgID.length; i++)
{
final int resuorceId = imgID[i];
runOnUiThread(new Runnable() {
#Override
public void run() {
imgView.setImageResource(resuorceId);
}
});
sleep(500);
}
} catch (InterruptedException e) {
}finally {
Intent intent = new Intent(SplashScreen.this,LoginActivity.class);
startActivity(intent);
finish();
}
}
You can not access your views from Thread.
You will need to put your code imgView.setImageResource(imgID[i]); in runOnUiThread
use like:
runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
imgView.setImageResource(imgID[i]);
}
});
Thanks
You can not change something in UI from non-UI thread so replace this you code:
imgView.setImageResource(imgID[i]);
to:
runOnUiThread(new Runnable() {
#Override
public void run() {
imgView.setImageResource(imgID[i]);
}
});
//try code this way...
public class SplashScreen extends Activity {
private Intent launchIntent;
private Thread splashThread; //used for perform splash screen operation
private int splashTime = 10000, sleepTime = 50; //used for threading operation
private boolean active = true; //used for touch event
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen); //Set splashscreen.xml here
try {
splashThread = new Thread() { // Creating Thread for splash the screen
#Override
public void run() { // run method implemented to perform threading operation
try {
int waitTime = 0; //counter for threading
do {
sleep(sleepTime); //delay for specific time
if (active)
waitTime += 100;
//write your image code here that display your no. of images
} while (active && (waitTime < splashTime)); //Check touch condition and counter
} catch (Exception e) {
// to handle runtime error of run method
Validation.displayToastMessage(SplashScreen.this, e.toString()); //Call static method of class ToastMessage
}
finish(); //finish current activity
startJustCoupleActivityScreen(); //Call below defined function
}
};
splashThread.start(); //start thread here
} catch (Exception e) {
message("SplashScreen : "+ e.toString()); //Call static method of class ToastMessage
}
}
public void startJustCoupleActivityScreen() {
launchIntent=new Intent(SplashScreen.this,JustCoupleActivity.class); //call Next Screen
startActivity(launchIntent); //start new activity
}
#Override
public boolean onTouchEvent(MotionEvent event) { //onTouch Event
//on touch it immediate skip splash screen
if(event.getAction()==MotionEvent.ACTION_DOWN) active=false; //Check Touch happened or not
return true;
}
public void message(String msg)
{
Validation.displayToastMessage(SplashScreen.this, msg); //display Error Message
}
}
I use this Tutorial to create custom Progressbar and it works .
But I want to start new activity when progress bar go to 100% .
anyone can help to put start new activity code in correct place?
You can check if the progress has reached the maximum possible while you're setting it, like this:
#Override
public synchronized void setProgress(int progress) {
super.setProgress(progress);
// the setProgress super will not change the details of the progress bar
// anymore so we need to force an update to redraw the progress bar
invalidate();
if(progress >= getMax()) {
Intent intent....
}
}
You can call the onContinue function timer thread and set the intent to next activity and register the activity name in manifest file.
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.progress);
mProgressBar = (ProgressBar)findViewById(R.id.adprogress_progressBar);
final Thread timerThread = new Thread() {
#Override
public void run() {
mbActive = true;
try {
int waited = 0;
while(mbActive && (waited < TIMER_RUNTIME)) {
sleep(200);
if(mbActive) {
waited += 200;
updateProgress(waited);
}
}
} catch(InterruptedException e) {
} finally {
onContinue();
}
}
};
timerThread.start();
}
#Override
public void onDestroy() {
super.onDestroy();
}
public void updateProgress(final int timePassed) {
if(null != mProgressBar) {
final int progress = mProgressBar.getMax() * timePassed / TIMER_RUNTIME;
mProgressBar.setProgress(progress);
}
}
public void onContinue() {
Intent intd=new Intent(this,MainActivity.class);
startActivity(intd);
}
Try this...
new Thread(new Runnable() {
public void run() {
while (progressStatus < 100) {
progressStatus += 5;
// Update the progress bar and display the current value in the text view
handler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressStatus);
textView.setText("Loading "+progressStatus+"/"+progressBar.getMax());
}
});
try {
// Sleep for 200 milliseconds. Just to display the progress slowly
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
**Intent intent = new Intent(Main_Activity.this, Send_Email.class);
startActivity(intent);**
}
}).start();
How to display another activity?
I want to display one activity which has one Image View splash displaying an image. I want to display that just for 5 seconds then move on to another activity.splash is displaying in emulator but another activity menu is not displaying.
Here is my code:
com.basic.android;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
public class androidbasics extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
}
public void toCallActivity() {
TimerTask startNewActivity;
final Handler handler = new Handler();
final Timer timer = new Timer();
startNewActivity = new TimerTask() {
#Override
public void run() {
// TODO Auto-generated method stub
handler.post(new Runnable() {
public void run() {
try {
timer.cancel();
startActivity(new Intent(androidbasics.this,menu.class));
} catch (Exception e) {
// TODO Auto-generated catch block
}
}
});
}
};
timer.schedule(startNewActivity, 0,5000);
}
}
You didn't call toCallActivity().So your new Activity is not coming in the front.Write like this.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
toCallActivity();
}
try this ....
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
BackgroundTask b = new BackgroundTask();
b.execute("Main");
}
/** Called when the activity is first created. */
class BackgroundTask extends AsyncTask<String , Void, Void>
{
#Override
protected void onPreExecute()
{
setContentView(R.layout.splash);
}
#Override
protected Void doInBackground(String... params)
{
// TODO Auto-generated method stub
int pause=5000;
int interval =1000;
int counter=0;
while(counter<pause)
{
try
{
Thread.sleep(interval);
counter+=interval;
}
catch(Exception e)
{
System.out.println(e);
}
}
return null;
}
#Override
protected void onPostExecute(Void result)
{
startActivity( new Intent(androidbasics.this,menu.class));
androidbasics.this.finish();
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Thread splashTread = new Thread() {
#Override
public void run() {
try {
int waited = 0;
while(_active && (waited < _splashTime)) {
sleep(100);
if(_active) {
waited += 100;
}
}
}
catch(InterruptedException e)
{
} finally {
finish();
startActivity(new Intent(firstactivity.class,secondActivity.class));
stop();
}
}
};
splashTread.start();
}
I am creating a splash screen using the following code ,when i press back key the application moves to the home screen and within a few seconds shows my next mainmenu screen.I am calling finish() in onBackPressed(),I want to close the app on pressing back key in the splash screen.can any one help me on this??
Thanks!!
Thread splashThread = new Thread() {
#Override
public void run() {
try {
int waited = 0;
while (_active && (waited < 2000)) {
sleep(100);
if(_active) {
waited += 100;
}
}
} catch (InterruptedException e) {
// do nothing
} finally {
finish();
startActivity(new Intent("next activity"));
stop();
}
}
};
splashThread.start();
It's because you call finish(); before the startActivity(new Intent("next activity"));
Swap finish(); with startActivity(new Intent("next activity"));
It is working in my application
public class Splash extends Activity {
protected boolean _active = true;
protected int _splashTime = 3000;
Thread splashTread;
private boolean stop = false;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
splashTread = new Thread() {
#Override
public void run() {
try {
int waited = 0;
while(_active && (waited < _splashTime)) {
sleep(100);
if(_active) {
waited += 100;
}
}
} catch(InterruptedException e) {
// do nothing
} finally {
if(!stop){
startActivity(new Intent(Splash.this,Home.class));
finish();
}
else
finish();
}
}
};
splashTread.start();
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
if(splashTread.isAlive())
this.stop = true;
}
return true;
}
}
this solution only solves the problem for the back-button. If users press the home-button the unwanted behavior will still occur. Wouldn't it be easier to overwrite the onStop method and do your thing in there?
#Override
public void onStop(){
super.onStop();
if(splashTread.isAlive())
this.stop = true;
}
Try using this:
SplashScreen.this.finish();
where SplashScreen is the name of the Activity.
public class SplashActivity extends Activity {
Handler handler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
handler=new Handler();
handler.postDelayed(() -> {
Intent intent=new Intent(SplashActivity.this, Home.class);
startActivity(intent);
finish();
},3000);
}
}
AndriodManifest.xml
<activity
android:name=".SplashActivity"
android:theme="#style/AppTheme"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I have a problem that I want to paste images on ImageView in Android and that images are periodically changed after some interval. Means one by one images shown in ImageView. I am doing this with the help of Thread in java but I got some problem that Thread is not attached and something. Please review my code given below and tell me the exact error and how to remove that error or give me some diffrent way for doing this.
package com.ex.thread;
import com.ex.thread.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
public class thread extends Activity implements Runnable{
/** Called when the activity is first created. */
public static Integer[] mThumbIds = {
R.drawable.al1,R.drawable.al2,R.drawable.al3,R.drawable.al4,
};
Thread th;
ImageView iv;
public void run()
{
for(int i=0;i<3;i++)
{
iv.setImageResource(mThumbIds[i]);
System.out.println("Sanat Pandey");
try{
Thread.sleep(3000);
}catch(Exception e)
{
System.out.println(e);
}
}
}
public void create()
{
Thread th = new Thread(new thread());
th.start();
try{
Thread.sleep(3000);
}catch(Exception e)
{
System.out.println(e);
}
}
#Override
public void onCreate(Bundle savedInstace)
{
super.onCreate(savedInstace);
setContentView(R.layout.main);
create();
}
}
Try this..It works out well...
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);``
//
//
int []imageArray={R.drawable.img1,R.drawable.img2,R.drawable.img3};
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
int i=0;
public void run() {
imageView.setImageResource(imageArray[i]);
i++;
if(i>imageArray.length-1)
{
i=0;
}
handler.postDelayed(this, 50); //for interval...
}
};
handler.postDelayed(runnable, 2000); //for initial delay..
}
You can't use things in the UI thread from a background one. So this call:
iv.setImageResource(mThumbIds[i]);
Has to be done in the main thread. In fact you probably don't need a background thread at all to get the effect you're looking for. You can make that just an activity, no need to implement runnable. and then do something like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
iv = (ImageView) findViewById(R.id.yourImageViewID);
int i = 0;
Runnable r = Runnable(){
public void run(){
iv.setImageResource(mThumbIds[i]);
i++;
if(i >= mThumbIds.length){
i = 0;
}
iv.postDelayed(r, 3000); //set to go off again in 3 seconds.
}
};
iv.postDelayed(r,3000); // set first time for 3 seconds
Try this
it's working
public class vv extends Activity {
int b[] = {R.drawable.a, R.drawable.m, R.drawable.b, R.drawable.j, R.drawable.er, R.drawable.chan, R.drawable.vv};
public ImageView i;
int z = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
i = (ImageView) findViewById(R.id.image);
i.setImageResource(b[0]);
Thread timer = new Thread() {
public void run() {
try {
sleep(2000);
for (z = 0; z < b.length + 2; z++) {
if (z < b.length) {
sleep(2000);
runOnUiThread(new Runnable() {
public void run() {
i.setImageResource(b[z]);
}
});
} else {
z = 0;
}
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println("finally");
}
}
};
timer.start();
}
}
try this code.the images was saved in drawable. please do insert a imageview in xml code. noted that the time interval for the following code is 1 sec.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
public ImageView iv;
public static Integer[] mThumbIds = {
R.drawable.pic1,R.drawable.pic2,R.drawable.pic3,R.drawable.pic4};
int i;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.imageView);
i=0;
t.start();
}
Thread t = new Thread() {
#Override
public void run() {
try {
while (!isInterrupted()) {
Thread.sleep(1000);
runOnUiThread(new Runnable() {
#Override
public void run() {
iv.setImageResource(mThumbIds[i]);
i++;
if(i >= mThumbIds.length){
i = 0;
}}});}}
catch (InterruptedException e) {
}}};
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final int img[] = {R.drawable.flower1, R.drawable.flower2, R.drawable.flower3, R.drawable.flower4};
layout = (RelativeLayout) findViewById(R.id.activity_main);
final Handler handler=new Handler();
Runnable runnable = new Runnable() {
int i = 0;
#Override
public void run() {
layout.setBackgroundResource(img[i]);
i++;
if (i > img.length - 1) {
i = 0;
}
handler.postDelayed(this, 4000); //for interval 4s..
}
};handler.postDelayed(runnable, 100); //for initial delay..
}