[英]Solid black screens when calling Dialogs in onCreate()
I've had this problem in a few different apps now and I can't seem to find a solution. 我现在在几个不同的应用程序中遇到了这个问题,我似乎无法找到解决方案。
If, in the onCreate() of an Activity
, I start an activity that uses the dialog theme it doesn't draw anything to screen... the whole screen stays black. 如果,在一个
Activity
的onCreate()中,我启动一个使用对话框主题的活动,它不会绘制任何东西到屏幕...整个屏幕保持黑色。 All the views are there (eg, I can tap where an EditText
should be and it'll give me the keyboard), they're just not visible. 所有的视图都在那里(例如,我可以点击
EditText
应该在哪里,它会给我键盘),它们只是不可见。
Stupid simple example, for fun: 愚蠢的简单例子,为了好玩:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startActivityForResult(new Intent(this, CredentialsInputActivity.class), 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// do some crap with the result, doesn't really matter what
}
}
CredentialsInputActivity
is pretty straight forward... just extends Activity
and has the theme set to @android:style/Theme.Dialog
in the manifest file. CredentialsInputActivity
非常简单......只需扩展Activity
并将主题设置为清单文件中的@android:style/Theme.Dialog
。
It turns out that this is a known bug in 1.5 (fixed in 1.6 and never a problem in 1.1). 事实证明,这是1.5中的一个已知错误 (在1.6中修复,在1.1中从来没有问题)。 The bug stems from the animation for the new Activity taking place before the old Activity has been drawn, but it only presents if the "old" Activity was the first Activity in the Task.
该错误源于在绘制旧活动之前发生的新活动的动画,但它仅在“旧”活动是任务中的第一个活动时才会出现。
A workaround is to disable the animation for the theme. 解决方法是禁用主题的动画。 The simplest way to do this it with a new theme that extends the main dialog theme.
最简单的方法是使用扩展主对话框主题的新主题来实现它。
res/values/themes.xml: RES /值/的themes.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CupcakeDialog" parent="android:Theme.Dialog">
<item name="android:windowAnimationStyle">@null</item>
</style>
</resources>
Then just reference it in your AndroidManifest.xml: 然后在AndroidManifest.xml中引用它:
<!-- ... -->
<activity
android:name=".CredentialsInputActivity"
android:label="@string/CredentialsInputActivity_window_title"
android:theme="@style/CupcakeDialog" />
<!-- ... -->
Obviously, you loose the animation, but at least you can see it :) 显然,你放松了动画,但至少你可以看到:)
Note: commonsware.com's solution worked fine too with the caveat I noted in the comments. 注意:commonsware.com的解决方案也很好用我在评论中提到的警告。
Just a guess here... 在这里猜一下......
I think @android:style/Theme.Dialog
is set for much of the background to be translucent. 我认为
@android:style/Theme.Dialog
设置为大部分背景都是半透明的。 Initially, your MainActivity
's background is black. 最初,您的
MainActivity
的背景是黑色的。 If the startActivityForResult()
is kicking in before your MainActivity
gets to draw, that might explain your problem. 如果
startActivityForResult()
在您的MainActivity
得到绘制之前就已经开始了,这可能会解释您的问题。
Try using postDelayed()
on a View
to delay your startActivityForResult()
by a few hundred milliseconds, and see if that changes the behavior. 尝试在
View
上使用postDelayed()
将startActivityForResult()
延迟几百毫秒,看看是否会改变行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.