5

昨日、最初のAndroidアプリケーションを公開しました。私はAndroid4.0でテストしていませんでしたが、友人が私のアプリが彼のギャラクシー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()Android4.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();
}

このため、Android4.0では多くのアプリケーションがクラッシュします。これはGoogleのせいではありません。何年も前から、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()

パブリックファイナルシンクロナイズドボイドストップ(スロー可能スロー可能)

以来:APIレベル1このメソッドは非推奨です。この方法でスレッドを停止することは安全ではなく、アプリケーションとVMを予測できない状態にする可能性があるためです。

UnsupportedOperationExceptionをスローします。throwable()がnullの場合、NullPointerExceptionをスローします

スレッドを強制的に停止したい場合は、代わりにthreadName.interrupt()スレッドの外部で使用してください。タスクが完了すると自然に停止するように、スレッドのライフサイクルを自然に終了するようにプログラムします。

この例では、スレッドはメソッドstop()の最後で実行を自然に停止するため、コマンドを削除するだけで済みます。run()

EDIT は、終了するためfinish()の呼び出しであり、あなたのではありません。上記の例では、とにかく自然に終了しますが、aを停止して終了することと混同しないでください。これらは、大きく異なるものです。ActivityThreadThreadThreadActivity

于 2012-04-04T08:41:22.513 に答える
4

stop()Android4.0で使用しても同じ問題が発生しました。finish()代わりに使用してみてください、それは私の問題を解決しました。

于 2012-04-04T08:16:33.330 に答える
3

私はそれstop()がもはやICSで機能していないと思います。

droidnova.comでの私のチュートリアルは、ICSで動作するように更新されていません。申し訳ありませんが、その時間はありませんでした。今日は、別のスレッドの代わりにハンドラーを使用します。はるかに使いやすく、より堅牢です。

于 2012-04-04T08:13:45.373 に答える