5

我昨天刚刚发布了我的第一个 android 应用程序。我没有在 android 4.0 上进行测试,我的朋友只是告诉我我的应用在他的 Galaxy S2 (4.0.3) 上崩溃了

在我的闪屏活动中几秒钟后它就崩溃了,它只是几行代码,也许你们可以检查一下:

 @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();

}

在 Android 2.3 到 3.1 上运行良好,但我无法弄清楚 4.0+ 有什么问题

请帮忙谢谢!

编辑:

如果我删除我的线程一切正常。所以我的新问题是...... 4.0 中的线程有什么新功能?我只是运行了一个什么都不做的线程,甚至我也遇到了崩溃。

4

4 回答 4

8

Thread.stop(), resume(), 并且suspend()不再适用于 Android 4.0。源代码如下:

/**
 * 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();
}

由于这个原因,很多应用程序在 Android 4.0 上崩溃了。这不是谷歌的错。从几年前开始,Java SDK 一直不鼓励在线程上使用 stop()。

引用更新日志:

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-04T08:20:32.553 回答
7

正如@Yuku 所说,Thread.stop()在 ICS 中并没有以某种方式被破坏,它被专门更改为抛出异常,因为它不安全:

http://developer.android.com/reference/java/lang/Thread.html#stop ()

公共最终同步无效停止(Throwable throwable)

自:API 级别 1 此方法已弃用。因为以这种方式停止线程是不安全的,并且会使您的应用程序和 VM 处于不可预测的状态。

引发 UnsupportedOperationException。如果 throwable() 为 null,则抛出 NullPointerException

如果你希望你的线程被强制停止,而不是threadName.interrupt()在你的线程外部使用。在你的线程生命周期中自然结束编程,以便在任务完成时自然停止。

在您的示例中,您可以简单地删除命令stop(),因为线程将在其方法结束时自然停止执行run()

EDIT finish()是要求您Activity完成,而不是您的Thread. 在上面的示例中,Thread无论如何都会自然退出,但请不要与停止 aThread和完成a 混淆,Activity因为它们是完全不同的东西。

于 2012-04-04T08:41:22.513 回答
4

stop()我在 Android 4.0上使用时遇到了同样的问题。尝试finish()改用,这解决了我的问题。

于 2012-04-04T08:16:33.330 回答
3

我想这stop()不再适用于 ICS。

在 droidnova.com上的教程没有更新为在 ICS 上工作,抱歉,没有时间。今天我将使用处理程序而不是单独的线程。更容易使用和更强大。

于 2012-04-04T08:13:45.373 回答