您好,不明白为什么我收到此错误:10-08 10:23:40.433: E/MoreInfoHPW_ViewGroup(10127): Parent view is not a TextView
任何帮助,将不胜感激。
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast_screen_pass,
(ViewGroup) findViewById(R.id.layout_pass));
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(layout);
toast.show();
以及 XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_pass"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="10dp"
android:background="@drawable/background_9" >
<ImageView
android:id="@+id/approval_icn"
android:layout_height="40dp"
android:layout_width="40dp"
android:src="@drawable/approval_icon"
android:layout_gravity="center_vertical" >
</ImageView>
<TextView
android:id="@+id/pass_txt"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:text="Congratulations you have passed the quiz" >
</TextView>
</LinearLayout>
LayoutInflater 的第二个参数看起来很奇怪。
(ViewGroup) findViewById(R.id.layout_pass)
是对要膨胀的布局中的 id 的引用,但参数用于将根布局传递给膨胀的对象。尝试只是通过null
作为第二个参数。
请参阅文档布局充气机
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)