空指针异常列表视图 android

Nullpointerexception listview android

我正在尝试在列表视图中显示字符串数组。该应用程序崩溃并提示空指针异常,但它没有指定出现空指针的行。

ReportHistoryFragment:-

    package com.appdev.ngoapp;

import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.parse.FindCallback;
import com.parse.GetCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;

import java.util.Iterator;
import java.util.List;

/**
 * Created by Adarsh on 01-02-2015.
 */
public class ReportHistoryFragment extends Fragment {

    View reportHistory;
    ParseUser parseUser;
    ParseQuery<ParseObject> parseQuery,pq;
    ParseObject parseObject;
    String[] title;
    Context context;
    ArrayAdapter<String> stringArrayAdapter;
    ListView listView;
    int size,i=0;
    ProgressDialog progressDialog;
    Iterator<ParseObject> iterator;

    public ReportHistoryFragment(Context context) {
        this.context=context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        reportHistory = inflater.inflate(R.layout.report_history_layout,container,false);
        listView = (ListView) reportHistory.findViewById(R.id.list_history);
        return reportHistory;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        parseUser = ParseUser.getCurrentUser();

        progressDialog = ProgressDialog.show(context,"Downloading Data","Please wait...",true);
        progressDialog.setCancelable(false);
        downloadData();
    }

    public void downloadData() {
        parseQuery = ParseQuery.getQuery("Reports");
        parseQuery.whereEqualTo("user",parseUser);
        parseQuery.findInBackground(new FindCallback<ParseObject>() {
            @Override
            public void done(List<ParseObject> parseObjects, ParseException e) {
                if (e == null) {
                    iterator = parseObjects.iterator();
                    size = parseObjects.size();
                    title = new String[size];
                    i=0;
                    while (iterator.hasNext()) {
                        parseObject = iterator.next();
                        pq = ParseQuery.getQuery("Reports");
                        Log.v("ID",parseObject.getObjectId());
                        pq.getInBackground(parseObject.getObjectId(), new GetCallback<ParseObject>() {
                            @Override
                            public void done(ParseObject object, ParseException e) {
                                if (e == null) {
                                    title[i] = object.getString("title");
                                    i++;
                                } else {
                                    e.printStackTrace();
                                }
                                if(i==size-1) {
                                    stringArrayAdapter = new ArrayAdapter<String>(context,android.R.layout.simple_list_item_1,title);
                                    listView.setAdapter(stringArrayAdapter);
                                }
                            }
                        });
                    }
                } else {
                    e.printStackTrace();

                }
            }
        });
    }
    }

report_history_layout.xml

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

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/list_history">

    </ListView>

</RelativeLayout>

Logcat

02-03 20:34:07.780  18717-18717/com.appdev.ngoapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.appdev.ngoapp, PID: 18717
    java.lang.NullPointerException
            at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
            at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
            at android.widget.AbsListView.obtainView(AbsListView.java:2255)
            at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
            at android.widget.ListView.onMeasure(ListView.java:1175)
            at android.view.View.measure(View.java:16521)
            at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
            at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
            at android.view.View.measure(View.java:16521)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at android.view.View.measure(View.java:16521)
            at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:851)
            at android.view.View.measure(View.java:16521)
            at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
            at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
            at android.view.View.measure(View.java:16521)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at android.view.View.measure(View.java:16521)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1450)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
            at android.view.View.measure(View.java:16521)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at android.view.View.measure(View.java:16521)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1450)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
            at android.view.View.measure(View.java:16521)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2552)
            at android.view.View.measure(View.java:16521)
            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1915)
            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1109)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1291)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5603)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
            at android.view.Choreographer.doCallbacks(Choreographer.java:574)
            at android.view.Choreographer.doFrame(Choreographer.java:544)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5146)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
            at dalvik.system.NativeStart.main(Native Method)

检查您是否真的在标题数组中放入了任何内容。然后检查stringArrayAdapter是否正常初始化。