列表适配器出错:尝试在空对象引用上调用虚方法'boolean java.lang.String.equals(java.lang.Object)'

时间:2018-01-04 08:15:11

标签: android android-layout listview nullpointerexception

我有这个错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean 
java.lang.String.equals(java.lang.Object)' on a null object reference 

Binary XML file line #0: Attempt to invoke virtual method 'boolean 
java.lang.String.equals(java.lang.Object)' on a null object reference

我看到很多代码示例,但没有区别。

错误发生在这一行:

row = inflater.inflate(R.layout.list_item, parent, false);

有什么建议吗?

@Override
public View getView(int position, View row, ViewGroup parent){
    DataHandler handler = new DataHandler();
    try {
       if (row == null) {
         LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         row = inflater.inflate(R.layout.list_item, parent, false);
         handler.poster = (ImageView) row.findViewById(R.id.movieImage);
         handler.title = (TextView) row.findViewById(R.id.movieTitle);
         handler.rating = (TextView) row.findViewById(R.id.movieRating);
         row.setTag(handler);
        } else {
            handler = (DataHandler) row.getTag();
        }

        MovieDataProvider dataProvider;
        dataProvider = (MovieDataProvider) this.getItem(position);
        handler.poster.setImageResource(dataProvider.getMovie_poster());
        handler.title.setText(dataProvider.getMovie_title());
        handler.rating.setText(dataProvider.getMovie_rating());
    }
    catch (Exception ex){
        Toast.makeText(getContext(),ex.getMessage(),Toast.LENGTH_LONG).show();
    }
    return row;
}

XML(list_item)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="80dp"
android:background="#000">
<ImageView
    android:id="@+id/movieImage"
    android:layout_width="80dp"
    android:layout_height="75dp"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:contentDescription=""
    android:src="@drawable/movie_0" />

<TextView
    android:id="@+id/movieTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="10dp"
    android:layout_toEndOf="@+id/movieImage"
    android:text="movie name"
    android:textColor="#ff2e5f"
    android:textSize="16sp"
    android:textStyle="bold" />

<TextView
    android:id="@+id/movieRating"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/movieTitle"
    android:layout_marginLeft="10dp"
    android:layout_toEndOf="@+id/movieImage"
    android:text="Rating"
    android:textColor="@android:color/holo_blue_dark"
    android:textSize="12sp"
    android:typeface="sans" />

<view
    android:layout_width="match_parent"
    android:layout_height="2dp"
    android:background="@android:color/background_light"
    android:layout_below="@+id/movieImage">
</view>
</RelativeLayout>

适配器的整个代码

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;



public class MovieAdapter extends ArrayAdapter {
public MovieAdapter(@NonNull Context context, int resource) {
    super(context, resource);
}

List list = new ArrayList();


static class DataHandler{
    ImageView poster;
    TextView title;
    TextView rating;
}

public void add(Object object){
    super.add(object);
    list.add(object);
}

public int getCount(){
    return this.list.size();
}

public Object getItem(int position){
    return this.list.get(position);
}


@Override
public View getView(int position, View row, ViewGroup parent){
    DataHandler handler = new DataHandler();

    if (row == null) {
         LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         row = inflater.inflate(R.layout.list_item, parent, false);
         handler.poster = (ImageView) row.findViewById(R.id.movieImage);
         handler.title = (TextView) row.findViewById(R.id.movieTitle);
         handler.rating = (TextView) row.findViewById(R.id.movieRating);
         row.setTag(handler);
        } else {
            handler = (DataHandler) row.getTag();
        }

    MovieDataProvider dataProvider;
    dataProvider = (MovieDataProvider) this.getItem(position);
    handler.poster.setImageResource(dataProvider.getMovie_poster());
    handler.title.setText(dataProvider.getMovie_title());
    handler.rating.setText(dataProvider.getMovie_rating());

    return row;
}
}

2 个答案:

答案 0 :(得分:3)

问题在于:

<view
    android:layout_width="match_parent"
    android:layout_height="2dp"
    android:background="@android:color/background_light"
    android:layout_below="@+id/movieImage">
</view>

<view>以小写字母开头:)您需要<View>

另见:

Explaination here

答案 1 :(得分:0)

您尝试使用等于的对象是nll,这就是您收到错误的原因:

yourObject.equals("something");

yourObject为null。如果你为它赋值,它就会消失,或者你可以先检查它是否为空或者使用try catch block,这里有一个例子:

if(yourObject != null){

        //your code here
        }

OR:

   try{

    if(yourObject.equals("something")){

 }

    }catch(NullPointerException exNull){

// here catch your exception 

}