Java 为AlertDialog扩展布局时出现Android异常
我正在尝试扩大版面以用作Java 为AlertDialog扩展布局时出现Android异常,java,android,android-alertdialog,layout-inflater,inflate-exception,Java,Android,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.vie
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)
将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
}
发布您的xml并显示my_dialog.xml文件的xml代码。您是否在“活动”或“碎片”上使用了AlertDialog您的xml代码可能有一些错误请检查此项我没有使用碎片或活动。这是一个单独的类。正如我所说,我已经尝试将
视图组
传递给充气
函数(其中所述ViewGroup
是从活动中获得的)@MichealJohnson您应该使用Activy或Fragment来访问视图或视图组。可能您正在尝试在java简单类或内部适配器中执行此操作。我建议您尝试使用侦听器!并在父视图内部尝试扩大它。这对您、您的代码、您的应用程序和性能都有好处。
@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
}