为AlertDialog充气布局时出现Android异常

时间:2017-06-12 12:09:47

标签: java android alertdialog layout-inflater inflate-exception

我试图给布局充气以用作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>

1 个答案:

答案 0 :(得分:-1)

如果使用Fragment尝试此代码

@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
 }