简体   繁体   English

我的应用程序在 android 2.3.3 到 android 3.1 上运行,但在 4.0 + 上因错误而停止

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

I just publicated yesterday my first android application.我昨天刚刚发布了我的第一个 android 申请。 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 )我没有在 android 4.0 上测试,我的朋友刚刚告诉我我的应用程序在他的 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+在 Android 2.3 到 3.1 上运行良好,但我无法弄清楚 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?所以我的新问题是...... 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. Thread.stop()resume()suspend()不再适用于 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.因此,很多应用程序在 Android 4.0 上崩溃。 This is not Google's fault;这不是谷歌的错; from years ago Java SDK has discouraged using stop() on a thread.从多年前 Java SDK 开始,我们不鼓励在线程上使用 stop()。

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

As @Yuku says, Thread.stop() isn't somehow broken in ICS it's specifically changed to throw an exception as it's unsafe:正如@Yuku 所说, Thread.stop()在 ICS 中并没有以某种方式被破坏,它被专门更改为抛出异常,因为它不安全:

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

public final synchronized void stop (Throwable throwable) public final synchronized void stop (Throwable throwable)

Since: API Level 1 This method is deprecated.自:API 级别 1 此方法已弃用。 because stopping a thread in this manner is unsafe and can leave your application and the VM in an unpredictable state.因为以这种方式停止线程是不安全的,并且会使您的应用程序和 VM 处于不可预测的状态 state。

Throws UnsupportedOperationException.抛出 UnsupportedOperationException。 Throws NullPointerException if throwable() is null如果 throwable() 为 null,则抛出 NullPointerException

If you want you're thread to be forcefully stopped instead use threadName.interrupt() while external to your thread.如果你想让你的线程被强制停止,而不是在你的线程外部使用threadName.interrupt() 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.在您的示例中,您可以简单地删除命令stop()因为线程将在其run()方法结束时自然停止执行。

EDIT finish() is a call to your Activity to finish, not your Thread . EDIT finish()是调用您的Activity完成,而不是您的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.在上面的示例中, Thread无论如何都会自然退出,但请不要将停止Thread和完成Activity混淆,因为它们是截然不同的事情。

I had the same issue using stop() on Android 4.0.我在 Android 4.0 上使用stop()时遇到了同样的问题。 Try using finish() instead, that solved my problem.尝试改用finish() ,这解决了我的问题。

I guess that stop() is no longer working on ICS.我想stop()不再适用于 ICS。

Mytutorial at droidnova.com is not updated to work on ICS, sorry, hadn't time for that.在 droidnova.com 上的教程未更新以适用于 ICS,抱歉,没时间。 Today I would use a handler instead of the separate thread.今天我会使用处理程序而不是单独的线程。 Much easier to use and more robust.使用起来更容易,也更健壮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM