关于空对象引用CustomListAdapter的Android notifyDataSetChanged()'

时间:2015-04-21 22:07:47

标签: java android list nullpointerexception adapter

我遇到了问题!我有这个代码:

HorairesActivity.java:

package activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonObjectRequest;
import com.example.pierre.tan.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

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

import adapter.CustomListAdapterHoraires;
import app.AppController;
import model.Horaires;


public class HorairesActivity extends ActionBarActivity {
private Toolbar mToolbar;
private ListView listView3;
private List<Horaires> horairesList = new ArrayList<Horaires>();
private CustomListAdapterHoraires adapter;
private static final String TAG = MainActivity.class.getSimpleName();
private SwipeRefreshLayout swipeLayout3;
final Random rnd = new Random();
private String configGrade;



public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.activity_temps, container, false);

    View headerView = getLayoutInflater().inflate(
            R.layout.view_list_item_header, listView3, false);

    // Inflate the layout for this fragment
    return rootView;


}


@Override
public void onCreate (
        final Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);


    Intent intents = getIntent();

    String id = intents.getStringExtra("libelle");
    setContentView(R.layout.activity_horaires);
    View searchContainer = findViewById(R.id.search_container);
    final EditText toolbarSearchView = (EditText) findViewById(R.id.search);
    ImageView searchClearButton = (ImageView) findViewById(R.id.search_clear);
    searchClearButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            toolbarSearchView.setText("");
        }
    });
    searchContainer.setVisibility(View.GONE);

    View headerView = getLayoutInflater().inflate(
            R.layout.view_list_item_header, listView3, false);


    final ImageView img = (ImageView) headerView.findViewById(R.id.imageView);
    // I have 3 images named img_0 to img_2, so...
    final String str = "img_" + rnd.nextInt(3);
    img.setImageDrawable
            (
                    getResources().getDrawable(getResourceID(str, "drawable",
                            getApplicationContext()))
            );




    listView3 = (ListView) findViewById(R.id.list_horaires);
    listView3.addHeaderView(headerView);
    TextView t = (TextView) findViewById(R.id.headertext);
    t.setText(id);



    listView3.setOnScrollListener(new AbsListView.OnScrollListener() {

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {

        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem,
                             int visibleItemCount, int totalItemCount) {

            View headerView = view.findViewById(R.id.header);

            final float mTop = -headerView.getTop();
            float height = headerView.getHeight();
            if (mTop > height) {
                // ignore
                return;
            }
            View imgView = headerView.findViewById(R.id.header);
            imgView.setTranslationY(mTop / 2f);

        }
    });

    mToolbar = (Toolbar) findViewById(R.id.toolbar);


    setSupportActionBar(mToolbar);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);


    Intent intent = getIntent();

    final String url = "" + "https://open.tan.fr/ewp/horairesarret.json/" + intent.getExtras().getString("id") + "/" + intent.getExtras().getString("ligne") + "/" + intent.getExtras().getString("sens") + "";

    Toast.makeText(getApplicationContext(), intent.getExtras().getString("text"), Toast.LENGTH_LONG).show();
    Log.d("Test", url);


    listView3 = (ListView) findViewById(R.id.list_horaires);

    // movieList is an empty array at this point.















    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
            url, null, new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            Log.d(TAG, response.toString());

            try {
                // Parsing json object response
                // response will be a json object


                Horaires horaires = new Horaires();







                ArrayList<String> listdata = new ArrayList<String>();

                JSONArray configJsonArray = response.getJSONArray("horaires");
                for(int configIterator = 0; configIterator < configJsonArray.length(); configIterator++){
                    JSONObject innerConfigObj = configJsonArray.getJSONObject(configIterator);
                     configGrade = innerConfigObj.getString("heure");


                    JSONArray courseJsonArray = innerConfigObj.getJSONArray("passages");
                    for(int courseIterator = 0; courseIterator < courseJsonArray.length(); courseIterator++){

                        listdata.add(courseJsonArray.get(courseIterator).toString());

                    }

                }

                horaires.setHeure(configGrade);
                horaires.setPassages(listdata);

                horairesList.add(horaires);




                adapter.notifyDataSetChanged();


            } catch (JSONException e) {
                e.printStackTrace();
                Toast.makeText(getApplicationContext(),
                        "Error: " + e.getMessage(),
                        Toast.LENGTH_LONG).show();
            }

        }


    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(TAG, "Error: " + error.getMessage());
            Toast.makeText(getApplicationContext(),
                    error.getMessage(), Toast.LENGTH_SHORT).show();
            // hide the progress dialog

        }
    });





    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(jsonObjReq);



    swipeLayout3 = (SwipeRefreshLayout) findViewById(R.id.container3);
    swipeLayout3.setColorScheme(android.R.color.holo_blue_bright,
            android.R.color.holo_green_light,
            android.R.color.holo_orange_light,
            android.R.color.holo_red_light);


    swipeLayout3.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

        public void onRefresh() {
            Toast.makeText(getApplication(), "Rechargement...", Toast.LENGTH_SHORT).show();


            horairesList.clear();





            adapter.notifyDataSetChanged();

            JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
                    url, null, new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    Log.d(TAG, response.toString());

                    try {
                        // Parsing json object response
                        // response will be a json object


                        Horaires horaires = new Horaires();







                        ArrayList<String> listdata = new ArrayList<String>();

                        JSONArray configJsonArray = response.getJSONArray("horaires");
                        for(int configIterator = 0; configIterator < configJsonArray.length(); configIterator++){
                            JSONObject innerConfigObj = configJsonArray.getJSONObject(configIterator);
                            configGrade = innerConfigObj.getString("heure");


                            JSONArray courseJsonArray = innerConfigObj.getJSONArray("passages");
                            for(int courseIterator = 0; courseIterator < courseJsonArray.length(); courseIterator++){

                                listdata.add(courseJsonArray.get(courseIterator).toString());

                            }

                        }

                        horaires.setHeure(configGrade);
                        horaires.setPassages(listdata);

                        horairesList.add(horaires);




                        adapter.notifyDataSetChanged();


                    } catch (JSONException e) {
                        e.printStackTrace();
                        Toast.makeText(getApplicationContext(),
                                "Error: " + e.getMessage(),
                                Toast.LENGTH_LONG).show();
                    }

                }


            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d(TAG, "Error: " + error.getMessage());
                    Toast.makeText(getApplicationContext(),
                            error.getMessage(), Toast.LENGTH_SHORT).show();
                    // hide the progress dialog

                }
            });

            AppController.getInstance().addToRequestQueue(jsonObjReq);

            swipeLayout3.setRefreshing(false);


        }
    });


}


protected final static int getResourceID
        (final String resName, final String resType, final Context ctx)
{
    final int ResourceID =
            ctx.getResources().getIdentifier(resName, resType,
                    ctx.getApplicationInfo().packageName);
    if (ResourceID == 0)
    {
        throw new IllegalArgumentException
                (
                        "No resource string found with name " + resName
                );
    }
    else
    {
        return ResourceID;
    }
}

public void onActivityCreated(Bundle savedInstanceState) {

    adapter = new CustomListAdapterHoraires(getParent(), horairesList);
    listView3.setAdapter(adapter);




}



}

我有CustomListAdapterHoraires.java:

package adapter;


import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.example.pierre.tan.R;

import java.util.List;

import app.AppController;
import model.Horaires;


public class CustomListAdapterHoraires extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<Horaires> horaireItems;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();


public CustomListAdapterHoraires(Activity activity, List<Horaires> horaireItems) {
    this.activity = activity;
    this.horaireItems = horaireItems;
}

@Override
public int getCount() {
    return horaireItems.size();
}

@Override
public Object getItem(int location) {
    return horaireItems.get(location);
}

@Override
public long getItemId(int position) {
    return position;
}


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


    if (inflater == null)
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null)
        convertView = inflater.inflate(R.layout.list_rowtemps, null);

    if (imageLoader == null)
        imageLoader = AppController.getInstance().getImageLoader();

    TextView heure = (TextView) convertView.findViewById(R.id.heure);
    TextView passage = (TextView) convertView.findViewById(R.id.passage);



    // getting movie data for the row
   Horaires m = horaireItems.get(position);


    // title
    heure.setText(m.getHeure());


    String ligneStr = "";
    for (String str : m.getPassages()) {
        ligneStr += str + ", ";
    }
    ligneStr = ligneStr.length() > 0 ? ligneStr.substring(0,
            ligneStr.length() - 2) : ligneStr;
    passage.setText(ligneStr);




    return convertView;


}


}

当我测试时,我已经记录了这个:

  

java.lang.NullPointerException:尝试调用虚方法'void   在null上的adapter.CustomListAdapterHoraires.notifyDataSetChanged()'   对象参考

我不知道问题出在哪里。

1 个答案:

答案 0 :(得分:0)

在执行任何请求之前将此代码移至onCreate,并将getParent更改为this

public void onActivityCreated(Bundle savedInstanceState) {

    adapter = new CustomListAdapterHoraires(this, horairesList);
    listView3.setAdapter(adapter);

}