Android错误:包含空指针异常

时间:2013-05-13 12:17:34

标签: android layout android-linearlayout

我有以下

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

    }

relLayouttess txtViewtess 都是 NULL 。为什么?我做错了什么?

1 个答案:

答案 0 :(得分:0)

您无需查找R.id.msg。您可以直接查看R.id.relLayoutIcon

View view = parent.findViewById(viewId);

其中viewId为R.id.relLayoutIcon