gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 12:50:41 25 4
gpt4 key购买 nike

我已经创建了 CreateEvent Activity

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

在 CreateEvent Activity 中,我想创建 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 friend ,它不为空;程序在适配器 getView() 处抛出 NullPointException ,原因是当 myView == null - (ImageView) findViewById(R.id.inviteCheckBox); 时返回空

这是项目的 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?预先感谢您

最佳答案

在适配器 getView() 中,在您扩充的 View 上调用 findViewById(),而不是在 Activity View 层次结构上调用(如果适配器是非静态的) Activity 中的内部类)。

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

关于java - Android - findViewById(int id) 始终返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24233184/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com