这是我收到的错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setOnItemClickListener(android.widget.AdapterView$OnItemClickListener)' on a null object reference
为什么我不能这样做?我已粘贴自定义View
课程中存在的Adapter
班级:
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.notifications_listview_item, null);
holder = new ViewHolder();
holder.username = (TextView)convertView.findViewById(R.id.username);
holder.notificationText = (TextView)convertView.findViewById(R.id.notificationText);
holder.time = (TextView)convertView.findViewById(R.id.time);
holder.profilePicture = (ImageView) (convertView.findViewById(R.id.profilePicture));
holder.notificationsIcon = (ImageView) (convertView.findViewById(R.id.notificationsIcon));
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
final ParseObject notifications = mNotifications.get(position);
Date createdAt = notifications.getCreatedAt();
long now = new Date().getTime();
String convertedDate = DateUtils.getRelativeTimeSpanString(createdAt.getTime(), now, DateUtils.SECOND_IN_MILLIS).toString();
holder.username.setText(notifications.getString(ParseConstants.KEY_SENDER_NAME));
holder.notificationText.setText(notifications.getString(ParseConstants.KEY_NOTIFICATION_TEXT));
holder.time.setText(convertedDate);
holder.profilePicture = (ImageView)convertView.findViewById(R.id.profilePicture);
if (notifications.getString(ParseConstants.KEY_NOTIFICATION_TYPE).equals(ParseConstants.TYPE_LIKE)) {
holder.notificationsIcon.setImageResource(R.drawable.ic_action_like_feed_full);
}
if (notifications.getString(ParseConstants.KEY_NOTIFICATION_TYPE).equals(ParseConstants.TYPE_CYCLE)) {
holder.notificationsIcon.setImageResource(R.drawable.ic_not_saved);
}
if (notifications.getString(ParseConstants.KEY_NOTIFICATION_TYPE).equals(ParseConstants.TYPE_EDIT)) {
holder.notificationsIcon.setImageResource(R.drawable.ic_action_edit_feed);
}
holder.username.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startGalleryActivity(notifications.getString(ParseConstants.KEY_SENDER_ID));
}
});
holder.profilePicture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startGalleryActivity(notifications.getString(ParseConstants.KEY_SENDER_ID));
}
});
holder.notificationsIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext, FeedActivitySingle.class);
intent.putExtra(FeedActivitySingle.EXTRA_DESIGN, notifications.getString(ParseConstants.KEY_SENDER_FEED_OBJECT_ID));
mContext.startActivity(intent);
}
});
ListView listView = (ListView)convertView.findViewById(android.R.id.list);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(mContext, FeedActivitySingle.class);
intent.putExtra(FeedActivitySingle.EXTRA_DESIGN, notifications.getString(ParseConstants.KEY_SENDER_FEED_OBJECT_ID));
mContext.startActivity(intent);
}
});
return convertView;
}
我想在适配器中的一个onItemClickListener
项目上设置listView
。为什么我不这样做?我已经定义了listView
:
ListView listView = (ListView)convertView.findViewById(android.R.id.list);
我该怎么办?所有常规ClickListener都可以工作,但我希望用户能够在适配器本身中选择listView
项。
编辑(答案):
问题解决了!只需将onItemClickListener
放在主要活动onCreate
初始化自定义Adapter
的方法中。
ListView listView = (ListView)findViewById(android.R.id.list);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "Voila! It works.", Toast.LENGTH_SHORT).show();
List<ParseObject> notifications = adapter.getNotifications();
}
});
答案 0 :(得分:2)
findViewById
方法查找子项:
查找具有给定ID的子视图。如果此视图具有给定的id,请返回此视图。
renderer.shadowMap.enabled = true;
是ListView
的父级,因此convertView
返回null。
为什么不在{1}}或findViewById
中设置要创建布局的AdapterView.OnItemClickListener
?
答案 1 :(得分:0)
convertView变量是listView中的一行,因为这个方法是findViewById。
convertView.findViewById()
始终返回null。
如果你想这样做,你可以这样做。
convertView.setOnClicListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=newIntent(mContext,FeedActivitySingle.class);
intent.putExtra(FeedActivitySingle.EXTRA_DESIGN, notifications.getString(ParseConstants.KEY_SENDER_FEED_OBJECT_ID));
mContext.startActivity(intent);
}
});