android:null pointer at splash screen - android

I am making an app in which i have to implement alpha effect in splash screen and when i load image it gives null pointer exception.The basic Problem while starting animation.If i remove start animation then my animation do not start at all.I am really stuck.Any help will be appreciated.My code is as follows:
public class SplashScreen extends Activity {
private Thread mSplashThread;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("hello");
setContentView(R.layout.splash);
final SplashScreen sPlashScreen = this;
Animation a1 = AnimationUtils.loadAnimation(this, R.anim.alpha);
LinearLayout Ll=(LinearLayout)findViewById(R.id.mainLayoutheader);
System.out.println("hello1");
Ll.startAnimation(a1);
System.out.println("hello2");
// The thread to wait for splash screen events
mSplashThread = new Thread(){
#Override
public void run(){
try {
synchronized(this){
// Wait given period of time or exit on touch
// a.reset();
wait(6000);
}
}
catch(InterruptedException ex){
}
finish();
// Run next activity
Intent intent = new Intent();
intent.setClass(sPlashScreen, Main.class);
startActivity(intent);
stop();
}
};
mSplashThread.start();
}
}
Exception is at L1.startAnimation(a1);

I guess you forgot to call setContentView. You have to do that before calling findViewById [edit: doesn't solve the problem here]

You can try timer thread...use this link
http://thedevelopersinfo.wordpress.com/2009/10/18/scheduling-a-timer-task-to-run-repeatedly/

Ll is null or a1 is null and startAnimation throws the exception. The full stack trace shows, which one threw it.
Also:
You might have accidentally typed R.id.mainLayoutheader instead of R.id.mainLayoutHeader.
Check the name R.anim.alpha is correct.

The problem was that the view in which i was showing image was empty.
super.onCreate(savedInstanceState);
// Splash screen view
System.out.println("hello");
setContentView(R.layout.splash);
ImageView iv=(ImageView)findViewById(R.id.splashscreen12);
Animation scaleAnim = AnimationUtils.loadAnimation(this,R.anim.alpha);
iv.startAnimation(scaleAnim);
scaleAnim.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationEnd(Animation animation) {
// Start new activity after some delay
TimerTask newActivity = new TimerTask() {
#Override
public void run() {
startActivity(new Intent(SplashScreeen.this,CallingActivity.class));
SplashScreeen.this.finish();
}
};
Timer t = new Timer(false);
t.schedule(newActivity, 2500);
}
#Override
public void onAnimationRepeat(Animation animation) {
// DO NOTHING
}
#Override
public void onAnimationStart(Animation animation) {
// DO NOTHING
}
});

Related

how to stop open activity while splash screen killed

I have splash screen .
once i open my application the splash screen will appears after completion of splash screen passed intent to HomeActivity.
but when i kill this app while splash screen running after some time HomeScreen will automatically open , but i want to kill the app.
but the HomeScreen should not show when i killed the app .
public class SplashAnimation extends Activity {
ImageView imageViewSplash;
TextView txtAppName;
RelativeLayout relativeLayout;
Thread SplashThread;
MediaPlayer mySong;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_view);
mySong=MediaPlayer.create(SplashAnimation.this,R.raw.monn);
mySong.start();
imageViewSplash = (ImageView) findViewById(R.id.imageViewSplash);
txtAppName = (TextView) findViewById(R.id.txtAppName);
relativeLayout = (RelativeLayout) findViewById(R.id.relative);
startAnimations();
}
private void startAnimations() {
Animation rotate = AnimationUtils.loadAnimation(this, R.anim.translate);
Animation translate = AnimationUtils.loadAnimation(this, R.anim.translate);
rotate.reset();
translate.reset();
relativeLayout.clearAnimation();
imageViewSplash.startAnimation(rotate);
txtAppName.startAnimation(translate);
SplashThread = new Thread() {
#Override
public void run() {
super.run();
int waited = 0;
while (waited < 3500) {
try {
sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
waited += 100;
}
SplashAnimation.this.finish();
Intent intent = new Intent(SplashAnimation.this, LibraryView.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
mySong.stop();
}
};
SplashThread.start();
}
#Override
protected void onStop() {
SplashAnimation.this.finish();
finish();
mySong.stop();
super.onStop();
}
#Override
protected void onDestroy() {
finish();
mySong.stop();
super.onDestroy();
}
}
Once you have called SplashThread.start() it will do its job as long as it can do. I would recommend to use a Handler instead, tho you can remotely cancel the task, the Handler runs:
//init and declare the handler instance
private Handler delayHandler;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (delayHandler == null) {
delayHandler = new Handler();
}
//your code
}
//define the task the handler should do
private void startAnimations() {
//replace the code beginning at 'Thread SplashThread = new Thread()' with the following
delayhandler.postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(SplashAnimation.this, LibraryView.class);
//these flags will prevent to 'redo' the transition by hitting the back button, that also makes calling 'finish()' obsolete
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
//instead of the while loop just execute the runnable after below given amount of milliseconds
}, 3500)
//to remotely cancel the runnable, if the app, respectively the Activity gets killed override 'onDestroy()'
#Override
public void onDestroy() {
super.onDestroy();
mySong.stop();
//calling 'finish()' is obsolete, tho 'finish()' calls 'onDestroy()' itself
//tell the handler to quit its job
delayHandler.removeCallbacksAndMessages(null);
}
Call in onStop() method
SplashThread.interrupt()
You can use Timer instead of instantiating the Thread class.
Refer the code below to start the Activity after 4 seconds. Use this in onCreate() of SplashActivity.
timer = new Timer().schedule(new TimerTask() {
#Override
public void run() {
startActivity(new Intent(getApplicationContext(), MainActivity.class));
}
}, 4000);
In your onPause() method use:
timer.cancel()
This will terminate the timer and disregards any currently scheduled tasks.

TextView keeps popping up

I have an activity where an TextView show up, and after that it fades out. However, everytime it has fade out, it pops back in. I tried
wtext.setText("");
But then my text just disappeares without fading out(after fading in). Does anyone knows how to fix this? Thanks! Kind regards.
This is my code:
public class SplashScreen extends Activity {
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Handler handler = new Handler();
final TextView wtext = (TextView) (findViewById(R.id.wtext));
Animation animation = AnimationUtils.loadAnimation(this, R.anim.abc_fade_in);
wtext.startAnimation(animation);
Runnable task = new Runnable() {
#Override
public void run() {
Animation animation1 = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.abc_fade_out);
wtext.startAnimation(animation1);
wtext.setText("");
}
};
handler.postDelayed(task, 5000);
}
If you want something to happen after an animation is complete (in this case, hide a textview), you'll have to use an AnimationListener
Animation.AnimationListener myListener = new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
wtext.setVisibility(View.INVISIBLE);
}
};
animation1.setAnimationListener(myListener);
You could use a ViewPropertyAnimator, which will change the view's actual properties rather than just its appearance, so that it will not jump back to the original state after the animation is complete.
wtext.animate().alpha(0);
You could try setting fillAfter flag to true on the element: it will apply the final transformation to it after the animation is finished.

how to check if an application is killed during an animation in android

i have to make an application in which it starts with an animation and if we click the back button then it should return back to application manager.But what i have made in it if u click back button during that animation then it goes to application manager but after a second or two the first page(the one after this animation comes up).
Can anyone help??
This is the animation..
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.load);
im = (ImageView) findViewById(R.id.load_icon);
rotate = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.load_page);
rotate.setInterpolator(new LinearInterpolator());
im.startAnimation(rotate);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent nextPageIntent = new Intent(getApplicationContext(),
P1.class);
startActivity(nextPageIntent);
}
}, 3000);
}
The first page opens because you have added
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent nextPageIntent = new Intent(getApplicationContext(),
P1.class);
startActivity(nextPageIntent);
}
}, 3000);
This launches the activity.For knowing if the animation has stopped use AnimationListener. More details here about animation listener
Android, How to set animation listener for view group?
You just added animation to one image view thats all, you do not doing anything with animation. The problem is, you started one thread to start activity P1 after 3 seconds. That thread only starting P1 activity. Try this and try to avoid killProcess(),
public class LauncherActivity extends Activity {
private Handler mHandler;
private Runnable mRunnable;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launcher);
mHandler = new Handler();
mRunnable = new Runnable() {
#Override
public void run() {
Intent nextPageIntent = new Intent(getApplicationContext(),
XmlParserActivity.class);
startActivity(nextPageIntent);
}
};
mHandler.postDelayed(mRunnable, 3000);
}
/* #Override
public void onBackPressed() {
super.onBackPressed();
mHandler.removeCallbacks(mRunnable);
}*/
#Override
protected void onDestroy() {
super.onDestroy();
mHandler.removeCallbacks(mRunnable);
}
}
public void onBackPressed() {
android.os.Process.killProcess(android.os.Process.myPid());
}
This is the answer

Intro of loading screen

At the app I'm developing, I'm interested in an intro of a loading screen. Which automatically move to the next screen after a duration.
The intro itself, is working just fine. As well as the thread who delaying the system.
My problem is to make them work together.
The code:
public class MainActivity extends Activity implements OnClickListener {
ImageView iv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loading_screen);
final Thread t1=new Thread(new Runnable() {
public void run() {
iv=(ImageView)findViewById(R.id.imgBtn1);
iv.setBackgroundResource(R.anim.loading_i_animation);
AnimationDrawable anim=(AnimationDrawable) iv.getBackground();
anim.start();
}
});
t1.start();
try {
Thread.sleep(2000);
finish();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
Intent st=new Intent(MainActivity.this,Welcome.class);
startActivity(st);
}
}
The result of this code is opening a white screen for the thread sleep-time duration. and after that open the "Welcome.class" screen via the intent.
It's just skipping the loading_screen, as is wasn't even exist.
I hope you guys could please help my with that.
You put your sleep on the UI thread which prevents Android to show anything until it finishes. Try the following code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loading_screen);
iv=(ImageView)findViewById(R.id.imgBtn1);
iv.setBackgroundResource(R.anim.loading_i_animation);
AnimationDrawable anim=(AnimationDrawable) iv.getBackground();
anim.start();
new Handler().postDelayed(
new Runnable() {
public void run() {
Intent st=new Intent(MainActivity.this,Welcome.class);
startActivity(st);
finish();
}
}, 2000);
}
This way the delay will run on a separated thread, but after 2 seconds it changes back to the main thread and runs the code you specified in your Runnable

How to show splash image while loading activity

I have an activity that contains many UI views. In its onCreate method, I found single line of setContentView takes 8-12 seconds to be complete. So I want to show my logo image while it's loading. I tried many things but without any success. I suspect main reason might be that before finishing setContentView, nothing can be shown.
Any help would be appreciated.
UPDATE:
I think many people do not know that you cannot show any dialog before finishing setContentView. So using another splash activity does not help me at all.
UPDATE2
I forgot to update this question after I found cause of the problem. Please refer to following question: setContentView taking long time (10-15 seconds) to execute
use AsyncTask
put splash in onPreExecute()
and do your work in doInBackground()
and close splash in onPostExecute()
Below is the simple code for creating splash screen using CountDownTimer class
public class SplashDialogActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// Be sure to call the super class.
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.layout);
counter.start();
}
MyCount counter = new MyCount(5000, 1000);
public class MyCount extends CountDownTimer{
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish() {
go_back();
}
#Override
public void onTick(long millisUntilFinished) {
}
}
public void go_back()
{
counter.cancel();
Intent i=new Intent(this,account.class);
i.putExtra("first_time", true);
startActivity(i);
this.finish();
}
}
try this code for splash page
private Thread mSplashThread;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splesh);
final Splash sPlashScreen = this;
mSplashThread = new Thread(){
#Override
public void run(){
try {
synchronized(this){
wait(5000);
}
}
catch(InterruptedException ex){
}
finish();
Intent intent = new Intent();
intent.setClass(sPlashScreen,Login.class);
startActivity(intent);
stop();
}
};
mSplashThread.start();
}
// Processes splash screen touch events
#Override
public boolean onTouchEvent(MotionEvent evt) {
if(evt.getAction() == MotionEvent.ACTION_DOWN)
{
synchronized(mSplashThread){
mSplashThread.notifyAll();
}
}
return true;
}

Categories

Resources