【发布时间】:2013-07-22 08:19:45
【问题描述】:
您好,我正在尝试在屏幕上显示自定义 AlertDialog。 我在对话框中添加了一个布局,这个布局包括两个 TextView、一个 RatingBar 和一个 EditText。
当我尝试弹出对话框时,我得到了这个异常:
E/AndroidRuntime(12989): android.view.InflateException:二进制 XML 文件第 25 行:错误 膨胀类 android.widget.EditText
重要的是这个错误发生在 Android 2.3.x 设备上,它在 Android 4.x 上运行良好
dialog_comment.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RatingBar
android:id="@+id/DialogCommentRatingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:stepSize="1" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/DialogCommentRatingBar"
android:text="Yorumunuz"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/DialogCommentComment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView2"
android:background="?android:attr/editTextBackground"
android:ems="10"
android:gravity="top"
android:inputType="textMultiLine" >
</EditText>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Oyunuz"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
CommentDialog.java
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater=getActivity().getLayoutInflater();
final View view=inflater.inflate(R.layout.dialog_comment, null);
final RatingBar rating=(RatingBar)view.findViewById(R.id.DialogCommentRatingBar);
final EditText comment=(EditText)view.findViewById(R.id.DialogCommentComment);
AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
builder.setView(view);
builder.setTitle("Enter Your Comment!");
return builder.create();
}
如何显示 AlertDialog
FragmentManager fm = getSupportFragmentManager();
CommentDialog commentDialog = new CommentDialog();
commentDialog.show(fm, "fragment_comment_dialog");
【问题讨论】:
-
试试这个:LayoutInflater inf = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);也许它有帮助。实际尝试调试。我认为你给出了空视图。
-
@fobus 尝试从 EditText 中删除背景属性,看看是否有什么不同
-
从 EditText 中删除这一行 android:background="?android:attr/editTextBackground" 并更新这一行 final View view=inflater.inflate(R.layout.dialog_comment, null,false);跨度>
-
是的,删除背景属性解决了我的问题。
-
你解决了这个问题吗?