实现适配器时,我收到错误java.lang.NullPointerException

时间:2017-09-17 15:32:31

标签: java android

当我运行代码时,我得到以下错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference at com.example.android.quakereport.EarthquakeAdapter.getView(EarthquakeAdapter.java:28)

以下是来自EarthquakeAdapter.java的代码,它实现了适配器

package com.example.android.quakereport;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.List;

public class EarthquakeAdapter extends ArrayAdapter<Earthquake>{

    public EarthquakeAdapter(Context context, List<Earthquake> earthquakes) {
        super(context, 0, earthquakes);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){

        View listItemView = convertView;
        if(listItemView != null) {
            listItemView = LayoutInflater.from(getContext()).inflate(
                    R.layout.earthquake_list_item, parent, false);
        }

        Earthquake currentEarthquake = getItem(position);

        TextView magnitudeView = (TextView) listItemView.findViewById(R.id.magnitude);
        magnitudeView.setText(currentEarthquake.getMagnitude());

        TextView locationView = (TextView) listItemView.findViewById(R.id.location);
        locationView.setText(currentEarthquake.getLocation());

        TextView dateView = (TextView) listItemView.findViewById(R.id.date);
        dateView.setText(currentEarthquake.getDate());

        return  listItemView;
    }
}

以下是Earthquack.java的代码,它实现了listview所需的数据结构。

package com.example.android.quakereport;

public class Earthquake {
    private String mMagnitude;

    private String mLocation;

    private String mDate;

    public Earthquake(String magnitude, String location, String date){



      mMagnitude = magnitude;
        mLocation = location;
        mDate = date;
    }

    public String getMagnitude(){ return mMagnitude;}

    public String getLocation(){ return mLocation;}

    public String getDate(){ return mDate;}
}

这是EarthquackActivity.java的代码,相当于mainActivity.java

package com.example.android.quakereport;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import java.util.ArrayList;

public class EarthquakeActivity extends AppCompatActivity {

    public static final String LOG_TAG = EarthquakeActivity.class.getName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.earthquake_activity);

        // Create a fake list of earthquake locations.
        ArrayList<Earthquake> earthquakes = new ArrayList<>();
        earthquakes.add(new Earthquake("8.9", "San Francisco", "Feb 2,2010"));
        earthquakes.add(new Earthquake("8.2", "Paris",         "March 5,2011"));
        earthquakes.add(new Earthquake("8.1","Cape Town","May 22,2013"));
        earthquakes.add(new Earthquake("9.9","Italy","Dec 4,2013"));
        earthquakes.add(new Earthquake("5.9","India","Jan 12,2012"));
        earthquakes.add(new Earthquake("7.9","New york","June 5,2015"));
        earthquakes.add(new Earthquake("3.9","Germany","July 1,2012"));

        // Find a reference to the {@link ListView} in the layout
        ListView earthquakeListView = (ListView) findViewById(R.id.weather_list);

        EarthquakeAdapter adapter = new EarthquakeAdapter(this, earthquakes);

        // Set the adapter on the {@link ListView}
        // so the list can be populated in the user interface
        earthquakeListView.setAdapter(adapter);
    }
}

earthquake_activity.xml代码

<?xml version="1.0" encoding="utf-8"?>
<!-- Layout for a list of earthquakes -->
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/weather_list"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

和earthquake_list_item.xml的代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools = "http://schemas.android.com/tools"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">

    <TextView
        android:id="@+id/magnitude"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        tools:text="8.9"/>

    <TextView
        android:id="@+id/location"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        tools:text="San Francisco, CA"/>


    <TextView
        android:id="@+id/date"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        tools:text="March 6, 2010"/>

</LinearLayout>

感谢。

2 个答案:

答案 0 :(得分:0)

convertview getView内部null方法if(listItemView != null)可能会导致崩溃。然后它将跳过componentDidMount()条件并且没有布局将被充气因此,NPE将发生。 因此,当它的null&amp;根据您的使用案例扩展适当的布局

答案 1 :(得分:0)

if(listItemView != null)替换为if(listItemView == null)

相关问题