I just publicated yesterday my first android application. I did not tested on android 4.0 and my friend just told me my app is crashes on his galaxy S2 (4.0.3 )
It is crashing after a few seconds in my splash screen activity, its just a few lines of code maybe you guys can check it:
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen);
try
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
overridePendingTransition(0 , 0);
// 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();
try
{
/*
Intent i = new Intent();
i.setClass(SplashScreen.this, MainActivity.class);
startActivity(i);
finish();
*/
}
catch(Exception e)
{
ki(e);
}
stop();
}
}
};
splashTread.start();
}
catch(Exception ex)
{
ki(ex);
}
}
@Override
public void onBackPressed() {
return;
}
//Toaster
public void ki(Exception message)
{
Toast myToast = Toast.makeText(getApplicationContext(), message.toString(), 1);
myToast.show();
}
Works verry well on Android 2.3 to 3.1 but i cant figure out whats the problem with 4.0+
Please help thank you!
Edit:
If i delete my thread everything works well. So me new question is... Whats new with threads in 4.0 ? I just ran a thread that does nothing and even i got the crash.
Thread.stop()
, resume()
, and suspend()
no longer works with Android 4.0. The source code is below:
/**
* Requests the receiver Thread to stop and throw ThreadDeath. The Thread is
* resumed if it was suspended and awakened if it was sleeping, so that it
* can proceed to throw ThreadDeath.
*
* @deprecated because stopping a thread in this manner is unsafe and can
* leave your application and the VM in an unpredictable state.
*/
@Deprecated
public final void stop() {
stop(new ThreadDeath());
}
/**
* Throws {@code UnsupportedOperationException}.
*
* @throws NullPointerException if <code>throwable()</code> is
* <code>null</code>
* @deprecated because stopping a thread in this manner is unsafe and can
* leave your application and the VM in an unpredictable state.
*/
@Deprecated
public final synchronized void stop(Throwable throwable) {
throw new UnsupportedOperationException();
}
A lot of application crashes on Android 4.0 because of this. This is not Google's fault; from years ago Java SDK has discouraged using stop() on a thread.
Quote from changelog:
Commit: a7ef55258ac71153487357b861c7639d627df82f [a7ef552]
Author: Elliott Hughes <[email protected]>
Date: 2011-02-23 6:47:35 GMT+08:00
Simplify internal libcore logging.
Expose LOGE and friends for use from Java. This is handy because it lets me use
printf debugging even when I've broken String or CharsetEncoder or something
equally critical. It also lets us remove internal use of java.util.logging,
which is slow and ugly.
I've also changed Thread.suspend/resume/stop to actually throw
UnsupportedOperationException rather than just logging one and otherwise
doing nothing.
Bug: 3477960
Change-Id: I0c3f804b1a978bf9911cb4a9bfd90b2466f8798f
As @Yuku says, Thread.stop()
isn't somehow broken in ICS it's specifically changed to throw an exception as it's unsafe:
http://developer.android.com/reference/java/lang/Thread.html#stop()
public final synchronized void stop (Throwable throwable)
Since: API Level 1 This method is deprecated. because stopping a thread in this manner is unsafe and can leave your application and the VM in an unpredictable state.
Throws UnsupportedOperationException. Throws NullPointerException if throwable() is null
If you want you're thread to be forcefully stopped instead use threadName.interrupt()
while external to your thread. Program in a natual end to you're threads lifecycle so that it stops naturally when it's task is complete.
In your example you can simply delete the command stop()
since the thread will naturally cease execution at the end of it's run()
method.
EDIT finish()
is a call to your Activity
to finish, not your Thread
. In the above example the Thread
would exit naturally anyway but please don't be confused with stopping a Thread
and finishing an Activity
as they are vastly different things.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With