AlertDialog的布局膨胀时出现Android异常



我正在尝试膨胀布局以用作AlertDialog的内容。我正在使用以下代码:

        View dialog_view = ((LayoutInflater) builder.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.my_dialog, null);

在此行中,将引发以下异常:

Process: myname.myapp, PID: 12068
android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
        at android.view.LayoutInflater.createView(LayoutInflater.java:633)
        at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
        at myname.myapp.myclass.myfunction(myclass.java:106)
        at myname.myapp.myclass.myfunction(myclass.java:52)
        at myname.myapp.myotherclass.myotherfunction(myotherclass.java:188)
        at myname.myapp.myotherclass$4$1.run(myotherclass.java:218)
        at java.lang.Thread.run(Thread.java:818)
 Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
        at android.view.LayoutInflater.createView(LayoutInflater.java:607)
        at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
        at myname.myapp.myclass.myfunction(myclass.java:106)
        at myname.myapp.myclass.myfunction(myclass.java:52)
        at myname.myapp.myotherclass.myotherfunction(myotherclass.java:188)
        at myname.myapp.myotherclass$4$1.run(myotherclass.java:218)
        at java.lang.Thread.run(Thread.java:818)
 Caused by: java.lang.RuntimeException: Failed to resolve attribute at index 84
        at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:569)
        at android.view.View.<init>(View.java:3740)
        at android.view.ViewGroup.<init>(ViewGroup.java:497)
        at android.widget.LinearLayout.<init>(LinearLayout.java:200)
        at android.widget.LinearLayout.<init>(LinearLayout.java:196)
        at android.widget.LinearLayout.<init>(LinearLayout.java:192)
        at java.lang.reflect.Constructor.newInstance(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
        at android.view.LayoutInflater.createView(LayoutInflater.java:607)
        at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
        at myname.myapp.myclass.myfunction(myclass.java:106)
        at myname.myapp.myclass.myfunction(myclass.java:52)
        at myname.myapp.myotherclass.myotherfunction(myotherclass.java:188)
        at myname.myapp.myotherclass$4$1.run(myotherclass.java:218)
        at java.lang.Thread.run(Thread.java:818)

我也尝试过使用

View.inflate(this.activity, R.layout.my_dialog, null);

并且将 ViewGroup 作为最后一个参数而不是null传递,但它们会引发相同的异常,并在生成的对话框中使用 dialog.setContentView(R.layout.my_dialog),但这会在调用 setContentView 的行中抛出相同的异常(因为大概这会在内部以相同的方式膨胀布局(。

编辑:我也试过

this.activity.getLayoutInflater().inflate(R.layout.dialog_password, null)

并抛出相同的异常。

这是my_dialog.xml的内容:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              android:padding="@dimen/dialog_padding">
    <EditText android:id="@+id/my_edittext"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:hint="@string/my_hint"/>
</LinearLayout>

如果使用片段,请尝试以下代码

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_alart_basic, container, false);
    final View dialogView;
    dialogView = inflater.inflate(R.layout.alart_dialog_custom, container, false);
    //call Alert Dialog here
    return view;
}

如果使用活动

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);
    LayoutInflater inflater = getLayoutInflater();
    View dialogView = inflater.inflate(R.layout.fragment_alart_basic, container, false);
    //call Alert Dialog here
 }

最新更新