getChildAt()InvocationTargetException

时间:2015-02-03 10:09:23

标签: android invocationtargetexception

在row_layout的xml中,我设置了一个删除按钮,如下所示

row_layout.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rowlayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginTop="0dp"
    android:background="#ffffff"
    android:baselineAligned="false"
    android:orientation="horizontal"
    android:paddingLeft="10sp"
    android:weightSum="100" >

    <LinearLayout
        android:id="@+id/Layout"
        android:layout_width="150sp"
        android:layout_height="50dp"
        android:layout_weight="90"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/firstTextView"
            android:layout_width="150sp"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:layout_marginTop="10sp"
            android:gravity="top"
            android:text="Activity"
            android:textColor="#000" />

        <TextView
            android:id="@+id/firstTextView2"
            android:layout_width="150sp"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:gravity="top"
            android:text="Sub-Activity"
            android:textColor="#000" />
    </LinearLayout>

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"

        android:src="@drawable/edit" />

    <ImageView
        android:id="@+id/delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         android:clickable="true"
        android:onClick="onClickDelete"
        android:src="@drawable/neg" />

</LinearLayout>

这里是onClickDelete方法

    public void onClickDelete(View v) 
{


    ListView lvItems = getListView();

    //get the row the clicked button is in
    LinearLayout vwParentRow = (LinearLayout)v.getParent();

    TextView child = (TextView) vwParentRow.getChildAt(0);
    child.setText("Delete Clicked");


}

当我按下删除时,它会在(TextView child = (TextView) vwParentRow.getChildAt(0);

显示invocationtargetexception

对不起,如果这是一个非常基本的错误。我是android的新手。 谢谢你的回复。

1 个答案:

答案 0 :(得分:0)

它更好

TextView child = (TextView)findViewById("yourtextfieldIDHere");
child.setText("Delete Clicked");