【问题标题】:FATAL EXCEPTION: main Unable to start activity ComponentInfo Caused by java.lang.NullPointerException致命异常:主要无法启动活动 ComponentInfo 由 java.lang.NullPointerException 引起
【发布时间】:2013-12-22 15:21:25
【问题描述】:

在将代码从我的主要活动移动到适配器的过程中,我开始遇到一个问题:The method findViewById(int) is undefined for the type VideosAdapterThe method getResources() is undefined for the type 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;
}

}

【问题讨论】:

    标签: java android undefined undefined-reference


    【解决方案1】:

    你应该使用convertView 而不是v

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

    【讨论】:

    • 谢谢!解决了第一个问题!我仍然有第二个错误:方法 getResources() is undefined for the type new View.OnClickListener(){}
    【解决方案2】:

    你需要:

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

    在 getView 中,它是您正在使用的视图“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()
    

    【讨论】:

    • 或者,如果不需要整个上下文,请考虑只保留对资源的引用:my_resources = context.getResources();
    猜你喜欢
    • 2013-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-02
    • 2019-03-18
    • 2015-06-12
    • 1970-01-01
    相关资源
    最近更新 更多