Ada*_*gyi 5 crash android splash-screen android-4.0-ice-cream-sandwich
我刚刚公开了我的第一个Android应用程序.我没有在Android 4.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();
}
Run Code Online (Sandbox Code Playgroud)
在Android 2.3到3.1上运行良好,但我无法弄清楚4.0+的问题
请帮忙谢谢!
编辑:
如果我删除我的线程一切正常.所以我的新问题是...... 4.0中的线程是什么新东西?我刚刚运行了一个什么都没做的线程,甚至我遇到了崩溃.
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();
}
Run Code Online (Sandbox Code Playgroud)
由于这个原因,很多应用程序在Android 4.0上崩溃了.这不是谷歌的错; 从几年前开始,Java SDK不鼓励在线程上使用stop().
来自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
Run Code Online (Sandbox Code Playgroud)
正如@Yuku所说,Thread.stop()在ICS中没有以某种方式被破坏,因为它不安全而被特别改为抛出异常:
http://developer.android.com/reference/java/lang/Thread.html#stop ()
public final synchronized void stop(Throwable throwable)
从以下版本开始:API Level 1此方法已弃用.因为以这种方式停止线程是不安全的,并且可能使您的应用程序和VM处于不可预测的状态.
抛出UnsupportedOperationException.如果throwable()为null,则抛出NullPointerException
如果你想要强制停止线程,而是threadName.interrupt()在线程外部使用.在自然结束中编程为线程生命周期,以便在任务完成时自然停止.
在您的示例中,您可以简单地删除命令,stop()因为线程将在其run()方法结束时自然停止执行.
编辑 finish()是打电话给你Activity完成,而不是你的Thread.在上面的例子中,Thread无论如何都会自然退出,但请不要混淆停止Thread和完成一个Activity因为它们是截然不同的东西.
| 归档时间: |
|
| 查看次数: |
2474 次 |
| 最近记录: |