我正在尝试在图像点击时为自定义对话框充气,但程序显示以下异常。
在图片上点击以下代码进入行为
iv1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
final Dialog d = new Dialog(About.this);
d.setContentView(R.layout.abtdial);
d.setTitle("About developer");
Button dbt = (Button) d.findViewById(R.id.button1);
dbt.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
d.dismiss();
}
});
d.show();
}
});
自定义对话框的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" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="32dp"
android:layout_marginTop="34dp"
android:text="about"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#00BFFF"
android:textSize="50dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="43dp"
android:text="some text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#848484" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="text abc"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#A9E2F3" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_above="@+id/textView3"
android:layout_centerHorizontal="true"
android:layout_marginBottom="17dp"
android:background="@style/btn"
android:text="Dismiss"
android:textColor="#00BFFF"
android:textSize="35dp"
android:typeface="monospace" />
</RelativeLayout>
以下代码的堆栈跟踪是
08-17 21:18:42.651: E/AndroidRuntime(851): FATAL EXCEPTION: main
08-17 21:18:42.651: E/AndroidRuntime(851): android.view.InflateException: Binary XML file line #61: Error inflating class <unknown>
08-17 21:18:42.651: E/AndroidRuntime(851): at android.view.LayoutInflater.createView(LayoutInflater.java:518)
08-17 21:18:42.651: E/AndroidRuntime(851): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
08-17 21:18:42.651: E/AndroidRuntime(851): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
08-17 21:18:42.651: E/AndroidRuntime(851): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
08-17 21:18:42.651: E/AndroidRuntime(851): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
08-17 21:18:42.651: E/AndroidRuntime(851): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
08-17 21:18:42.651: E/AndroidRuntime(851): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
08-17 21:18:42.651: E/AndroidRuntime(851): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
08-17 21:18:42.651: E/AndroidRuntime(851): at android.app.Dialog.setContentView(Dialog.java:421)
08-17 21:18:42.651: E/AndroidRuntime(851): at bitcream.my.notepad.About$1.onClick(About.java:32)
08-17 21:18:42.651: E/AndroidRuntime(851): at android.view.View.performClick(View.java:2485)
08-17 21:18:42.651: E/AndroidRuntime(851): at android.view.View$PerformClick.run(View.java:9080)
08-17 21:18:42.651: E/AndroidRuntime(851): at android.os.Handler.handleCallback(Handler.java:587)
08-17 21:18:42.651: E/AndroidRuntime(851): at android.os.Handler.dispatchMessage(Handler.java:92)
08-17 21:18:42.651: E/AndroidRuntime(851): at android.os.Looper.loop(Looper.java:123)
08-17 21:18:42.651: E/AndroidRuntime(851): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-17 21:18:42.651: E/AndroidRuntime(851): at java.lang.reflect.Method.invokeNative(Native Method)
08-17 21:18:42.651: E/AndroidRuntime(851): at java.lang.reflect.Method.invoke(Method.java:507)
08-17 21:18:42.651: E/AndroidRuntime(851): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-17 21:18:42.651: E/AndroidRuntime(851): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-17 21:18:42.651: E/AndroidRuntime(851): at dalvik.system.NativeStart.main(Native Method)
08-17 21:18:42.651: E/AndroidRuntime(851): Caused by: java.lang.reflect.InvocationTargetException
08-17 21:18:42.651: E/AndroidRuntime(851): at java.lang.reflect.Constructor.constructNative(Native Method)
08-17 21:18:42.651: E/AndroidRuntime(851): at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
08-17 21:18:42.651: E/AndroidRuntime(851): at android.view.LayoutInflater.createView(LayoutInflater.java:505)
08-17 21:18:42.651: E/AndroidRuntime(851): ... 20 more
08-17 21:18:42.651: E/AndroidRuntime(851): Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x1/d=0x7f060001 a=-1 r=0x7f060001}
08-17 21:18:42.651: E/AndroidRuntime(851): at android.content.res.Resources.loadDrawable(Resources.java:1681)
08-17 21:18:42.651: E/AndroidRuntime(851): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
08-17 21:18:42.651: E/AndroidRuntime(851): at android.view.View.<init>(View.java:1951)
08-17 21:18:42.651: E/AndroidRuntime(851): at android.widget.TextView.<init>(TextView.java:344)
08-17 21:18:42.651: E/AndroidRuntime(851): at android.widget.Button.<init>(Button.java:108)
08-17 21:18:42.651: E/AndroidRuntime(851): at android.widget.Button.<init>(Button.java:104)
08-17 21:18:42.651: E/AndroidRuntime(851): ... 23 more
答案 0 :(得分:1)
在button1
xml表示中,您有android:background="@style/btn"
。 background
属性接受drawables,而不是样式。这就是你有这个异常消息的原因
android.content.res.Resources $ NotFoundException:资源不是Drawable
答案 1 :(得分:0)
如果btn.xml
选择器中有button_pressed
,focused
和selected
类似的东西:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button_pressed"
android:state_pressed="true" />
<item android:drawable="@drawable/button_pressed"
android:state_focused="true" />
<item android:drawable="@drawable/button_selected" /> </selector>
您需要将其添加到可绘制文件夹
android:background="@drawable/btn"