gpt4 book ai didi

java - 致命异常 : main Unable to start activity ComponentInfo Caused by java. lang.NullPointerException

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

在将代码从我的主要 Activity 移动到适配器的过程中,我开始遇到一个问题:对于 VideosAdapter 类型未定义方法 findViewById(int)方法 getResources( ) 对于 new View.OnClickListener(){} 类型未定义

我找到了这个解决方案:

The method findViewById(int) is undefined for the type

但是当我尝试更改此设置时:

fav_up_btn1 = (Button) findViewById(R.id.fav_up_btn1);

对此:

fav_up_btn1 = (Button) v.findViewById(R.id.fav_up_btn1);

我最终得到的是:

v cannot be resolved

并且我不确定在我的情况下应该如何声明 v 。

JAVA:

public class VideosAdapter extends BaseAdapter {
// The list of videos to display
List<Video> videos;
// An inflator to use when creating rows
private LayoutInflater mInflater;
Button fav_up_btn1;
Button fav_dwn_btn1;

/**
* @param context this is the context that the list will be shown in - used to create new list rows
* @param videos this is a list of videos to display
*/
public VideosAdapter(Context context, List<Video> videos) {
this.videos = videos;
this.mInflater = LayoutInflater.from(context);
}

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

@Override
public Object getItem(int position) {
return videos.get(position);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// If convertView wasn't null it means we have already set it to our list_item_user_video so no need to do it again
if(convertView == null){
// This is the layout we are using for each row in our list
// anything you declare in this layout can then be referenced below
convertView = mInflater.inflate(R.layout.list_item_user_video, parent, false);
}
// We are using a custom imageview so that we can load images using urls
// For further explanation see: http://blog.blundell-apps.com/imageview-with-loading-spinner/
UrlImageView thumb = (UrlImageView) convertView.findViewById(R.id.userVideoThumbImageView);

TextView title = (TextView) convertView.findViewById(R.id.userVideoTitleTextView);
fav_up_btn1 = (Button) findViewById(R.id.fav_up_btn1);

fav_up_btn1.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
boolean favIsUp = fav_up_btn1
.getBackground()
.getConstantState()
.equals(getResources().getDrawable(
R.drawable.fav_up_btn1).getConstantState());
// set the background
fav_up_btn1
.setBackgroundResource(favIsUp ? R.drawable.fav_dwn_btn1
: R.drawable.fav_up_btn1);
}
});

// Get a single video from our list
final Video video = videos.get(position);
// Set the image for the list item
thumb.setImageDrawable(video.getThumbUrl());
// Set the title for the list item
title.setText(video.getTitle());

return convertView;
}

}

最佳答案

您需要:

fav_up_btn1 = (Button) convertView.findViewById(R.id.fav_up_btn1);

在 getView 中,它是您正在使用的 View “convertView”中的元素。在本例中,该布局是 list_item_user_video.xml

您说您在 getResources() 方面也遇到了问题。该方法需要上下文(当您在 Activity 中使用它时,该上下文是隐式的,因为 Activity 就是上下文)。然而,您的适配器中的情况并非如此。

您已经在适配器构造函数中接受上下文,因此请保留对其的引用。有一个类级别变量:

Context my_context;

在构造函数中设置:

public VideosAdapter(Context context, List<Video> videos) {
this.videos = videos;
this.mInflater = LayoutInflater.from(context);
my_context = context;
}

然后您可以使用适配器内调用 getResources

my_context.getResources()

关于java - 致命异常 : main Unable to start activity ComponentInfo Caused by java. lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20404172/

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