【问题标题】:Error in list adapter : Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference [duplicate]列表适配器中的错误:尝试在空对象引用上调用虚拟方法“boolean java.lang.String.equals(java.lang.Object)”[重复]
【发布时间】:2018-01-04 08:15:11
【问题描述】:

我有这个错误:

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;
}
}

【问题讨论】:

  • 分享你的 xml..
  • @Noise Generator 它不是常规 NullPointerException 的重复,它不是在 null 对象上调用方法,所以你错误地将其标记为重复,并指定了错误的帖子
  • @MishaAkopov 那是你的意见。每个 NPE 都是一样的。我厌倦了这么说。对象已被引用,但尚未被实例化。事实就是这样。
  • @Noise Generator 我知道什么是 NPE。 Mohammad Saffarini 没有实例化任何对象,这是 XML 问题。请参阅解决方案,它与 java 代码或创建对象或使它们为 null 无关 - 这是在解析错误的 xml 布局时的内部 Android 异常。
  • ... it was XML problem ... it is internal Android exception while parsing wrong xml layout... 不,这是一个 NPE,正如 logcat 中正确报告的那样:OP 试图使用不存在的对象! view 不是要使用的有效对象!希望现在清楚了。

标签: android android-layout listview nullpointerexception


【解决方案1】:

问题是这样的:

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

&lt;view&gt; 以小写字母开头 :) 你需要&lt;View&gt;

另见:

Explaination here

【讨论】:

  • 谢谢你,Mish,它现在可以工作了,因为这个我很害羞,我没有注意到。 :)
  • @MohammadSaffarini ,这有时会发生 :) 没问题,这是一种体验
【解决方案2】:

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

yourObject.equals("something");

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

if(yourObject != null){

        //your code here
        }

或者:

   try{

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

 }

    }catch(NullPointerException exNull){

// here catch your exception 

}

【讨论】:

    猜你喜欢
    • 2020-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-19
    相关资源
    最近更新 更多