-3

I'm trying to inflate a layout to use as the content for an AlertDialog. I'm using the following code:

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

At this line, the following exception is thrown:

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)

I've also tried using

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

and with passing a ViewGroup as the last parameter instead of null but these throw the same exception, and by using dialog.setContentView(R.layout.my_dialog) on the resulting dialog but this throws the same exception at the line where setContentView is called (because presumably this inflates the layout the same way internally).

EDIT: I've also tried

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

and the same exception is thrown.

This is the contents of 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>
micheal65536
  • 558
  • 3
  • 6
  • 26

1 Answers1

-1

if using Fragment try this code

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

if using Activity

 @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
 }
Fakhriddin Abdullaev
  • 4,169
  • 2
  • 35
  • 37
  • I'm not using a fragment or an activity. This is a separate class. As I say, I've already tried passing a `ViewGroup` to the `inflate` function (where said `ViewGroup` was obtained from the activity). – micheal65536 Jun 12 '17 at 14:16
  • @MichealJohnson you should be using a Activiy or Fragment to access to views or Viewgroup. Maybe you are trying to do in a java simple class or inside adapter. I would like to suggest try Listeners! and Inside of your parent view try to inflate it. It is better for you, for you code, for you app and for the performance. – Cristofer Nov 23 '17 at 18:49