2014-10-08 45 views
-1

嗨在不明白爲什麼我得到這個錯誤,父視圖不是一個TextView任何幫助,將不勝感激。我嘗試了幾件事情,但似乎沒有任何工作。10-08 10:23:40.433:E/MoreInfoHPW_ViewGroup(10127):父視圖不是一個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> 
+3

的OP問同樣的問題在幾個小時前:http://stackoverflow.com/questions/26252328/10-08-102340- 433-E-moreinfohpw-viewgroup10127父 - 視圖 - 是 - 不-A-textvie – Nija 2014-10-08 12:46:39

回答

0

你必須給一個TextViewToast類。但你提供Layout。這是例外。

如果你想顯示自定義烤麪包信息,你可以參考here

我希望這會幫助你。

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> 
相關問題