[英]How do I get rid of a java.lang.NullPointerException error?
[英]I get the error java.lang.NullPointerException when implementing Adapters
当我运行代码时,出现以下错误:
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);
}
}
抗震代码.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"/>
并为地震代码_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>
谢谢。
当您的getView
方法中的convertview
为null
时,可能会崩溃。 然后它将跳过if(listItemView != null)
条件,并且不会夸大任何布局,因此将发生NPE。 因此,请尝试处理其为null的情况,并根据您的用例扩大适当的布局
将if(listItemView != null)
替换为if(listItemView == null)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.