My App works on android 2.3.3 to android 3.1 but stops with error on 4.0 +

Go To StackoverFlow.com

5

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.

2012-04-04 07:59
by Adam Varhegyi
Look in LogCat - Ollie C 2012-04-04 08:01
It would be very helpful if you can share LogCa - Naved 2012-04-04 08:02
http://tinyurl.com/7tyuqb - weakwire 2012-04-04 08:07
Something not okay with my thread.. Logcat: 04-04 08:05:01.055: W/dalvikvm(858): threadid=11: thread exiting with uncaught exception (group=0x409961f8) 04-04 08:05:01.078: E/AndroidRuntime(858): FATAL EXCEPTION: Thread-90 04-04 08:05:01.078: E/AndroidRuntime(858): java.lang.UnsupportedOperationException 04-04 08:05:01.078: E/AndroidRuntime(858): at java.lang.Thread.stop(Thread.java:1076) 04-04 08:05:01.078: E/AndroidRuntime(858): at java.lang.Thread.stop(Thread.java:1063) 04-04 08:05:01.078: E/AndroidRuntime(858): at proba.test.IcecreamActivity$1.run(IcecreamActivity.java:55 - Adam Varhegyi 2012-04-04 08:08


5

I had the same issue using stop() on Android 4.0. Try using finish() instead, that solved my problem.

2012-04-04 08:16
by Fiur
Thanks man you made my day - Adam Varhegyi 2012-04-04 08:19
This answer is wrong. finish() closes the Activity it doesn't stop the Thread. In this case the Thread exits naturally anyway but in all other circumstances the Thread would continue to perform even after the death of the Activity - Graeme 2012-04-04 14:42


8

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 <enh@google.com>
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
2012-04-04 08:20
by Randy Sugianto 'Yuku'


7

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.

2012-04-04 08:41
by Graeme


3

I guess that stop() is no longer working on ICS.

My tutorial at droidnova.com is not updated to work on ICS, sorry, hadn't time for that. Today I would use a handler instead of the separate thread. Much easier to use and more robust.

2012-04-04 08:13
by WarrenFaith
Ads