在 onCreate() 中调用对话框时出现黑屏
我现在在几个不同的应用程序中遇到了这个问题,但似乎找不到解决方案。
如果在 Activity
的 onCreate() 中,我启动一个使用对话框主题的活动,它不会在屏幕上绘制任何内容...整个屏幕保持黑色。所有视图都在那里(例如,我可以点击 EditText
应该在的位置,它会给我键盘),它们只是不可见。
愚蠢的简单示例,为了好玩:
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
非常简单......只需扩展Activity
并将主题设置为@android:style/Theme.Dialog< /code> 在清单文件中。
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. All the views are there (e.g., I can tap where an EditText
should be and it'll give me the keyboard), they're just not visible.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
事实证明,这是 1.5 中的已知错误 (在 1.6 中已修复,在 1.1 中从来没有出现过问题)。该错误源于新活动的动画在绘制旧活动之前发生,但仅当“旧”活动是任务中的第一个活动时才会出现。
解决方法是禁用主题的动画。最简单的方法是使用扩展主对话框主题的新主题。
res/values/themes.xml:
然后只需在 AndroidManifest.xml 中引用它:
显然,您失去了动画,但至少您可以看到它:)
注意:commonsware.com 的解决方案也可以很好地工作,并且符合我在评论。
It turns out that this is a known bug in 1.5 (fixed in 1.6 and never a problem in 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:
Then just reference it in your AndroidManifest.xml:
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.
只是猜测...
我认为
@android:style/Theme.Dialog
的大部分背景设置为半透明。最初,您的MainActivity
的背景是黑色的。如果startActivityForResult()
在您的MainActivity
开始绘制之前启动,这可能可以解释您的问题。尝试在
View
上使用postDelayed()
将startActivityForResult()
延迟几百毫秒,然后看看是否会改变行为。Just a guess here...
I think
@android:style/Theme.Dialog
is set for much of the background to be translucent. Initially, yourMainActivity
's background is black. If thestartActivityForResult()
is kicking in before yourMainActivity
gets to draw, that might explain your problem.Try using
postDelayed()
on aView
to delay yourstartActivityForResult()
by a few hundred milliseconds, and see if that changes the behavior.