Android - findViewById(int id)始终返回null

时间:2014-06-15 19:16:24

标签: java android xml listview baseadapter

我创建了CreateEvent活动

        @Override
        public void onClick(View v) {
            Intent in = new Intent(UserActivity.this, CreateEvent.class);
            startActivity(in);
        }
    });

在CreateEvent活动中,我想创建listView,这是我的代码:

@Override
public void friendsDownlaoded(ArrayList<User> friends) {
    this.friends = friends;
    this.adapter = new  InviteFriendAdapter(getLayoutInflater());
    this.friendListview.setAdapter(adapter);
    removeRoation();    
    setUpContent();
}

这里是InviteFriendAdapter的代码,它是内部类,它可以看到ArrayList的朋友:

private class InviteFriendAdapter extends BaseAdapter {
    private LayoutInflater inflater;

    public InviteFriendAdapter(LayoutInflater inflater) {
        this.inflater = inflater;
    }

    @Override
    public int getCount() {
        return friends.size();
    }

    @Override
    public Object getItem(int i) {
        return friends.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View myView, ViewGroup vg) {
        FriendHolder frndHolder;
        User user = friends.get(position);


        if (myView == null) {
            myView = this.inflater.inflate(R.layout.invite_friends_event_item, null,false);
            ImageView img= (ImageView) findViewById(R.id.inviteCheckBox);
            frndHolder = new FriendHolder();
            frndHolder.hisImage = (ImageView) findViewById(R.id.inviteCheckBox);
            frndHolder.hisNumber = (TextView) findViewById(R.id.inviteHisNumber);
            frndHolder.hisName = (TextView) findViewById(R.id.inviteHisName);

            myView.setTag(frndHolder);
        } else {
            frndHolder = (FriendHolder) myView.getTag();
        }


        frndHolder.hisName.setText("aa");
        frndHolder.hisNumber.setText("uUU");

        return myView;
    }

}

private static class FriendHolder {
    TextView hisName;
    TextView hisNumber;
    ImageView hisImage;
}

我已经检查了ArrayList的朋友,并且它不是空的; 该程序在适配器getView()处抛出NullPointException,原因是当myView == null - (ImageView)findViewById(R.id.inviteCheckBox)时;返回null

这是项目的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/possibleFriendInviteHolder"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/inviteHisName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_margin="@dimen/possible_his_number_margin"
        android:textSize="@dimen/possible_his_name_text" />

    <TextView
        android:id="@+id/inviteHisNumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/inviteHisName"
        android:layout_margin="@dimen/possible_his_number_margin"
        android:textSize="@dimen/possible_his_number_text" />

    <ImageView
        android:id="@+id/inviteCheckBox"
        android:layout_width="@dimen/smile_pic_width"
        android:layout_height="@dimen/smile_pic_height"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_margin="@dimen/possible_check_box_margin"
        android:contentDescription="@string/image"
        android:scaleType="fitXY" />

</RelativeLayout>

我无法在网上找到解决方案。我已经多次创建了适配器并且运行良好,但现在...... 任何人都可以告诉我为什么findViewById()返回null? 提前谢谢

1 个答案:

答案 0 :(得分:1)

在适配器getView()中,在您膨胀的视图上调用findViewById(),而不是在活动视图层次结构上调用(如果适配器是活动中的非静态内部类)。

在您的代码中,将findViewById()替换为myView.findViewById()