0

I'm having an issue with 4.x devices with my app. It looks like a thread is making it crash while changing activity (from the splash screen to the real application). Here's a screenshot:

UnsupportedOperationException screenshot

Splash activity code is:

public class Splash extends Activity {

protected boolean _active = true;
protected int _splashTime = 3000; // tempo di permanenza spash screen

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);

    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();
                Intent i = new Intent(Splash.this, Test01Activity.class);
                startActivity(i);
                stop();
            }
        }
    };
splashTread.start();
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        _active = false;
    }
    return true;
}
}

Also, the app doens't really crash, as the main activity keeps working in the background (behind the "Unfortunatly, app has stopped working" alert). This problems has only been found in 4.x devices, 2.x and 3.x are all-working. The error is at line 37.

Stefano
  • 263
  • 1
  • 3
  • 10

2 Answers2

2

I think exception is due to use of deprecated stop method of Thread class. It may be not supported in ICS 4.0.

You can use Handler instead of thread for your purpose.

You can try below code inside your splash screen onCreate method

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);

    Handler mHandler = new Handler();
        mHandler.postDelayed(new Runnable() {

            @Override
            public void run() {
                Intent i = new Intent(Splash.this, Test01Activity.class);
                startActivity(i);
            finish();

            }
        }, _splashTime);

}
anujprashar
  • 6,263
  • 7
  • 52
  • 86
0

Every operation with GUI must be proceeded in event UI thread. Android 4 is more severe in this requirement. This might be a reason why your app crashes under it. As I see, you don't change activity in UI thread. See AsyncTask for details.

Exterminator13
  • 2,152
  • 1
  • 23
  • 28