Android错误:include的null ponter异常



我有以下

main.xml:

...
 <include
                    android:id="@+id/msg"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_margin="3.5dp"
                    android:layout_weight="2"
                    layout="@layout/view_msg" />
..

view_msg.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="true" >
    <RelativeLayout
        android:id="@+id/relLayoutIcon"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <ImageView
            android:id="@+id/view_img"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/icon" >
        </ImageView>
        <RelativeLayout
            android:id="@+id/relLayouttest"
            android:layout_width="40dip"
            android:layout_height="40dip"
            android:layout_centerHorizontal="true" >
            <RelativeLayout
                android:id="@+id/relLayouttess"
                android:layout_width="wrap_content"
                android:layout_height="22dip"
                android:layout_alignParentBottom="true"
                android:layout_alignParentRight="true"
                android:gravity="center" >
                <TextView
                    android:id="@+id/txtViewtess"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="234"/>
            </RelativeLayout>
        </RelativeLayout>
    </RelativeLayout>
</LinearLayout>

Main.java//扩展活动{..

Test.setImageButton(this,
                R.id.msg,
                R.drawable.tess, Test.TAG_MESSAGES,
                mOnDialerClick);

Test.java//是一个简单的java类

public static  void setImageButton(Activity parent, int viewId,
            int imageId, int tag, View.OnClickListener listener) {
        View view = parent.findViewById(viewId);
        view.setTag(tag);
        view.setOnClickListener(listener);
        ((ImageView) view.findViewById(R.id.view_img)).setImageResource(imageId);
        RelativeLayout relLayouttess =((RelativeLayout)view.findViewById(R.id.relLayouttess));
        TextView txtViewtess = ((TextView)view.findViewById(R.id.txtViewtess));
        relLayouttess
                .setBackgroundResource(R.drawable.redbackground);
                txtViewNotifications.setVisibility(View.GONE);
    }

relLayouttesstxtViewtess均为NULL。为什么?我做错了什么?

您不需要查找R.id.msg。您可以直接查找R.id.relLayoutIcon

View view = parent.findViewById(viewId);

其中viewId是R.id.relLayoutIcon

最新更新