10-08 10:23:40.433:E / MoreInfoHPW_ViewGroup(10127):父视图不是TextView

时间:2014-10-08 12:42:04

标签: java android

嗨我不明白为什么我得到这个错误,父视图不是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();

         

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

2 个答案:

答案 0 :(得分:1)

试试这个

   LayoutInflater inflater = getLayoutInflater();
   View layout = inflater.inflate(R.layout.toast_layout,
                           (ViewGroup) findViewById(R.id.toast_layout_root));

   ImageView image = (ImageView) layout.findViewById(R.id.image);
   image.setImageResource(R.drawable.android);
   TextView text = (TextView) layout.findViewById(R.id.text);   /// You have to provide textview
   text.setText("Hello! This is a custom toast!");

   Toast toast = new Toast(getApplicationContext());
   toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
   toast.setDuration(Toast.LENGTH_LONG);
   toast.setView(layout);
   toast.show();

和xml

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/toast_layout_root"
     android:orientation="horizontal"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:padding="10dp"
     android:background="#DAAA" >

    <ImageView android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_marginRight="10dp" />

    <TextView android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:textColor="#FFF" />

</LinearLayout> 

答案 1 :(得分:0)

您必须向TextView课程提供Toast。但是你提供Layout。这是例外。

如果您想显示自定义Toast消息,可以参考here

我希望这会对你有所帮助。