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

标签 java android undefined undefined-reference

在将代码从我的主要 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/

相关文章:

java - 在Java中使用Future的最佳方法

java - Java 中的首选项

java - .nextLine() 直接跳转到下一个输出

iphone - 将 Sencha/JQuery Mobile 部署为 Android/iPhone 中的一键启动应用程序

javascript - 捕获未定义的javascript变量

java - 来自 Java SE 的 JMS 连接

android - map 未在 Map api v2 中加载

javascript - 是否有标准函数来检查 JavaScript 中的 null、未定义或空白变量?

javascript - Angular JS : Accessing a $scope. 变量在 $http.get block 内定义以在另一个 $http.get 中使用

android - android :rotationX and android:rotationY in XML actually work?怎么办