使用默认的ArrayAdapter构建ListView时抛出异常NullPointerException

本人Android新手在看第一行代码,最后一个大程序的时候出现了NullPointerException,希望大家能够帮忙解决一下,谢谢

代码如下:

ChooseAreaActivity.java

package com.example.asuka.coolweather.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.asuka.coolweather.R;
import com.example.asuka.coolweather.db.CoolWeatherDB;
import com.example.asuka.coolweather.model.City;
import com.example.asuka.coolweather.model.County;
import com.example.asuka.coolweather.model.Province;
import com.example.asuka.coolweather.util.HttpCallbackListener;
import com.example.asuka.coolweather.util.HttpUtil;
import com.example.asuka.coolweather.util.Utility;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by asuka on 15-10-7.
 */
public class ChooseAreaActivity extends Activity {

    public static final int LEVEL_PROVINCE = 0;

    public static final int LEVEL_CITY = 1;

    public static final int LEVEL_COUNTY = 2;

    private ProgressDialog progressDialog;
    private TextView titleText;
    private ListView listView;
    private ArrayAdapter<String> adapter;
    private CoolWeatherDB coolWeatherDB;
    private List<String> dataList = new ArrayList<String>();

    /**
     * 省列表
     */
    private List<Province> provinceList;

    /**
     * 市列表
     */
    private List<City> cityList;

    /**
     * 县列表
     */
    private List<County> countyList;

    /**
     * 选中的省份
     */
    private Province selectedProvince;

    /**
     * 选中的城市
     */
    private City selectedCity;

    /**
     * 当前选中的级别
     */
    private int currentLevel;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.choose_area);
        listView = (ListView) findViewById(R.id.list_view);
        titleText = (TextView) findViewById(R.id.title_text);
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, dataList);
        listView.setAdapter(adapter);
        coolWeatherDB = CoolWeatherDB.getInstance(this);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?>arg0, View view, int index, long arg3) {
                if (currentLevel == LEVEL_PROVINCE) {
                    selectedProvince = provinceList.get(index);
                    queryCities();
                } else if (currentLevel == LEVEL_CITY) {
                    selectedCity = cityList.get(index);
                    queryCounties();
                }
            }
        });
        queryProvinces();
    }

    /**
     * 查询全国所有的省,优先从数据库查询,如果没有查询到再去服务器上查询
     */
    private void queryProvinces() {
        provinceList = coolWeatherDB.loadProvinces();
        if (provinceList.size() > 0) {
            dataList.clear();
            for (Province province : provinceList) {
                dataList.add(province.getProvinceName());
            }
            adapter.notifyDataSetChanged();
            listView.setSelection(0);
            titleText.setText("China");
            currentLevel = LEVEL_PROVINCE;
        } else {
            queryFromServer(null, "province");
        }
    }

    /**
     * 查询选中省所内所有的市,优先从数据库查询,如果没有查询到再去服务器上查询
     */
    private void queryCities() {
        cityList = coolWeatherDB.loadCities(selectedProvince.getId());
        if (cityList.size() > 0) {
            dataList.clear();
            for (City city : cityList) {
                dataList.add(city.getCityName());
            }
            adapter.notifyDataSetChanged();
            listView.setSelection(0);
            titleText.setText(selectedProvince.getProvinceName());
            currentLevel = LEVEL_CITY;
        } else {
            queryFromServer(selectedProvince.getProvinceCode(), "city");
        }
    }

    /**
     * 查询选中市内所有的县,有先从数据库查询,如果没有查询到再去服务器上查询
     */
    private void queryCounties() {
        countyList = coolWeatherDB.loadCounties(selectedCity.getId());
        if (countyList.size() > 0) {
            dataList.clear();
            for (County county : countyList) {
                dataList.add(county.getCountyName());
            }
            adapter.notifyDataSetChanged();
            listView.setSelection(0);
            titleText.setText(selectedCity.getCityName());
            currentLevel = LEVEL_COUNTY;
        } else {
            queryFromServer(selectedCity.getCityCode(), "county");
        }
    }

    /**
     * 根据传入的代号和类型服务器上查询省市县数据
     */
    private void queryFromServer(final String code, final String type) {
        String address;
        if (!TextUtils.isEmpty(code)) {
            address = "http://www.weather.com.cn/data/list3/city" + code + ".xml";
        } else {
            address = "http://www.weather.com.cn/data/list3/city.xml";
        }
        showProgressDialog();
        HttpUtil.sendHttpRequest(address, new HttpCallbackListener() {
            @Override
            public void onFinish(String response) {
                boolean result = false;
                if ("province".equals(type)) {
                    result = Utility.handleProvinceResponse(coolWeatherDB, response);
                } else if ("city".equals(type)) {
                    result = Utility.handleCitiesResponse(coolWeatherDB, response, selectedCity.getId());
                } else if ("county".equals(type)) {
                    result = Utility.handleCountiesResponse(coolWeatherDB, response, selectedCity.getId());
                }
                if (result) {
                    //通过runOnUiThread方法回到主线程逻辑处理
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                           closeProgressDialog();
                            if ("province".equals(type)) {
                                queryProvinces();
                            } else if ("city".equals(type)) {
                                queryCities();
                            }
                        }
                    });
                }

            }

            @Override
            public void onError(Exception e) {
                //通过runOnUiThread()方法回到主线程处理逻辑
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        closeProgressDialog();
                        Toast.makeText(ChooseAreaActivity.this, "加载失败", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }

    /**
     * 显示进度对话框
     */
    private void showProgressDialog() {
        if (progressDialog == null) {
            progressDialog = new ProgressDialog(this);
            progressDialog.setMessage("正在加载..");
            progressDialog.setCanceledOnTouchOutside(false);
        }
        progressDialog.show();
    }

    /**
     * 关闭进度对话框
     */
    private void closeProgressDialog() {
        if (progressDialog != null) {
            progressDialog.dismiss();
        }
    }

    /**
     * 捕获Back按键,根据当前的级别来判断,此时应该返回市列表、省列表、还是直接退出
     */
    @Override
    public void onBackPressed() {
        if (currentLevel == LEVEL_COUNTY) {
            queryCities();
        } else if (currentLevel == LEVEL_CITY) {
            queryProvinces();
        } else {
            finish();
        }
    }
}

choose_area.xml

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#484E61">

        <TextView
            android:id="@+id/title_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:textColor="#fff"
            android:textSize="24sp"/>
    </RelativeLayout>

    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </ListView>

</LinearLayout>

logcat提示的错误信息

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
            at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:401)
            at android.widget.ArrayAdapter.getView(ArrayAdapter.java:369)
            at android.widget.AbsListView.obtainView(AbsListView.java:2346)
            at android.widget.ListView.makeAndAddView(ListView.java:1875)
            at android.widget.ListView.fillSpecific(ListView.java:1354)
            at android.widget.ListView.layoutChildren(ListView.java:1674)
            at android.widget.AbsListView.onLayout(AbsListView.java:2148)
            at android.view.View.layout(View.java:16630)
            at android.view.ViewGroup.layout(ViewGroup.java:5437)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
            at android.view.View.layout(View.java:16630)
            at android.view.ViewGroup.layout(ViewGroup.java:5437)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
            at android.view.View.layout(View.java:16630)
            at android.view.ViewGroup.layout(ViewGroup.java:5437)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
            at android.view.View.layout(View.java:16630)
            at android.view.ViewGroup.layout(ViewGroup.java:5437)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
            at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2678)
            at android.view.View.layout(View.java:16630)
            at android.view.ViewGroup.layout(ViewGroup.java:5437)
            at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2171)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1931)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
            at android.view.Choreographer.doCallbacks(Choreographer.java:670)
            at android.view.Choreographer.doFrame(Choreographer.java:606)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
阅读 10.5k
4 个回答

错误提示信息告诉你问题出在这:

at android.widget.ArrayAdapter.getView(ArrayAdapter.java:369)

你需要了解默认的getView方法的实现:

在adapter的构造方法中指定的布局(android.R.layout.simple_list_item_1)是Android SDK 提供的预定义布局资源。该布局拥有一个TextView根元素。
而默认的ArrayAdapter.getView(...)实现方法依赖于toString()方法。它首先生成布局视图,然后找到指定位置的对象并对其调用toString()方法,最后得到字符串信息并传递给TextView。
《Android权威编程指南 P157,158》

所以,我猜问题应该是你传给Adapter的dataList包含null对象。
你应该着重检查更改adapter数据的代码,比如:

dataList.add(); // 确定添加的不是null吗?
adapter.notifyDataSetChanged();

我也遇到同样的问题,楼上的说的很对,按照楼上说的解决了

手动还原了一下你出现的情况,

 List<String> list = new ArrayList<String>();
        String a = null;
        list.add(a);

打印出的错误日志和你出现的问题是一样的,也就是说dataList里面出现的null值
应该是后面的数据库和服务器拉取数据时有null值被加进去了,建议加一个数据检测
判断一下取回的数据是否为null值,如果是的话赋一个缺省值或者"".

新手上路,请多包涵

我也出现一样的问题。我的具体情况是,可以选择省,选择市,到县的时候闪退。错误原因在于API放回的关于县的数据中解析不对,一行代码写错了。应该是county.setCountyName(array[1]);写成了county.setCountyCode(array[1]);

推荐问题
宣传栏