java.lang.IllegalStateException: ActionBarContextView can only be used with android:layout_width="

本文描述了在Android 3.1系统上使用自定义主题的对话框时遇到的崩溃问题,当用户在对话框内的EditText中双击文字时会触发此问题。文章提供了复现步骤及修改建议。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

- Steps to reproduce the problem (including sample code if appropriate).
Implement a Dialog(Maybe an Activity also has this issue. But I have not tested it.) using a customized theme which does not extends any of a Android basic theme. Such as:
    <style name="MyDialogTheme">
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowFrame">@null</item>
        <item name="android:backgroundDimEnabled">false</item>
	</style>

Also add a EditText into the dialog layout.
Run the dialog on a Tablet(I used Galaxy Tab 10.1 with Android OS 3.1). Type some words into the EditText. Then double click the typed words.

- What happened.
Crashed with this exception message:
 java.lang.IllegalStateException: ActionBarContextView can only be used with android:layout_width="match_parent" (or fill_parent)  at com.android.internal.widget.ActionBarContextView.onMeasure(ActionBarContextView.java:257)  at android.view.View.measure(View.java:10840)  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4351)  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1284)  at android.widget.LinearLayout.measureVertical(LinearLayout.java:613)  at android.widget.LinearLayout.onMeasure(LinearLayout.java:519)  at android.view.View.measure(View.java:10840)  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4351)  at android.widget.FrameLayout.onMeasure(FrameLayout.java:267)  at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2042)  at android.view.View.measure(View.java:10840)  at android.view.ViewRoot.performTraversals(ViewRoot.java:884)  at android.view.ViewRoot.handleMessage(ViewRoot.java:2017)  at android.os.Handler.dispatchMessage(Handler.java:99)  at android.os.Looper.loop(Looper.java:132)  at android.app.ActivityThread.main(ActivityThread.java:4028)  at java.lang.reflect.Method.invokeNative(Native Method)  at java.lang.reflect.Method.invoke(Method.java:491)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)  at dalvik.system.NativeStart.main(Native Method)

- What you think the correct behavior should be.
It should not crash and show OS selection UI to allow user to select words.
It works before OS 3.0. So it is a backward compatibility issue.
If it really need to throw a exception, at least we should get some information about why it happened more clearly.
Comment 1 by mr.polar...@gmail.comAug 3, 2011
If I modify the style as below. It will not crash.
<style name="MyDialogTheme" parent="@android:style/Theme.Dialog">
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowFrame">@null</item>
        <item name="android:backgroundDimEnabled">false</item>
</style>
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值