Mike
Mike

Reputation: 6839

Custom Listview force closing

I have created a custom list view which keeps force closing and I am having trouble figuring out why. I think I may have not named one of the view correctly since I was modeling this view off another. But I cant find my mistake yet.

Here is my force close error:

08-30 22:04:44.073    4011-4011/com.beerportfolio.beerportfoliopro E/AndroidRuntime: FATAL EXCEPTION: main
        java.lang.NullPointerException
        at com.example.beerportfoliopro.BreweryLocationInfoAdapter.getView(BreweryLocationInfoAdapter.java:50)
        at android.widget.AbsListView.obtainView(AbsListView.java:2410)
        at android.widget.ListView.makeAndAddView(ListView.java:1963)
        at android.widget.ListView.fillDown(ListView.java:815)
        at android.widget.ListView.fillFromTop(ListView.java:876)
        at android.widget.ListView.layoutChildren(ListView.java:1813)
        at android.widget.AbsListView.onLayout(AbsListView.java:2238)
        at android.view.View.layout(View.java:13900)
        at android.view.ViewGroup.layout(ViewGroup.java:4391)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
        at android.view.View.layout(View.java:13900)
        at android.view.ViewGroup.layout(ViewGroup.java:4391)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
        at android.view.View.layout(View.java:13900)
        at android.view.ViewGroup.layout(ViewGroup.java:4391)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
        at android.view.View.layout(View.java:13900)
        at android.view.ViewGroup.layout(ViewGroup.java:4391)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
        at android.view.View.layout(View.java:13900)
        at android.view.ViewGroup.layout(ViewGroup.java:4391)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2183)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1984)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1221)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4710)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:746)
        at android.view.Choreographer.doCallbacks(Choreographer.java:572)
        at android.view.Choreographer.doFrame(Choreographer.java:538)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:731)
        at android.os.Handler.handleCallback(Handler.java:615)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:155)
        at android.app.ActivityThread.main(ActivityThread.java:5536)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1074)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:841)
        at dalvik.system.NativeStart.main(Native Method)

My BreweryLocationInfoAdapter code:

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.RatingBar;
import android.widget.TextView;
import com.beerportfolio.beerportfoliopro.R;

public class BreweryLocationInfoAdapter extends ArrayAdapter<BreweryLocationData>{

    Context context;
    int layoutResourceId;
    List<BreweryLocationData> data = null;

    public BreweryLocationInfoAdapter(Context context, int layoutResourceId, List<BreweryLocationData> data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        breweryHolder holder = null;

        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new breweryHolder();
            holder.txtBrewery = (TextView)row.findViewById(R.id.beerNameList);
            holder.txtDistance = (TextView)row.findViewById(R.id.beerBreweryNameList);

            row.setTag(holder);
        }
        else
        {
            holder = (breweryHolder)row.getTag();
        }

        BreweryLocationData beer = data.get(position);
        holder.txtBrewery.setText(beer.brewery);
        holder.txtBrewery.setText(beer.distance);


        return row;
    }

    static class breweryHolder
    {
        TextView txtBrewery;
        TextView txtDistance;


    }
}

GetNearbyBreweries code:

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.beerportfolio.beerportfoliopro.R;

public class GetNearbyBreweries extends AsyncTask
        <String, Void, String> {

    Context c;
    private ProgressDialog Dialog;

    public GetNearbyBreweries (Context context)
    {
        c = context;
        Dialog = new ProgressDialog(c);
    }

    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub
        return readJSONFeed(arg0[0]);
    }

    protected void onPreExecute() {
        Dialog.setMessage("Locating Breweries");

        Dialog.setTitle("Loading");
        Dialog.setCancelable(false);
        Dialog.show();
    }

    protected void onPostExecute(String result){
        //decode json here
        try{
            JSONObject json = new JSONObject(result);


            //acces listview
            ListView lv = (ListView) ((Activity) c).findViewById(R.id.topTasteBeers);

            //make array list for beer
            final List<BreweryLocationData> tasteList = new ArrayList<BreweryLocationData>();



            for(int i = 0; i < json.getJSONArray("data").length(); i++) {

                String brewery = json.getJSONArray("data").getJSONObject(i).getJSONObject("brewery").getString("name");
                String id = json.getJSONArray("data").getJSONObject(i).getJSONObject("brewery").getString("id");
                String latitude = json.getJSONArray("data").getJSONObject(i).getString("latitude");
                String longitude = json.getJSONArray("data").getJSONObject(i).getString("longitude");
                String distance = json.getJSONArray("data").getJSONObject(i).getString("distance");

                Toast.makeText(c, "Brewery: " + brewery, Toast.LENGTH_SHORT).show();


                int count = i + 1;


                //create object
                BreweryLocationData tempLocation = new BreweryLocationData(brewery, id, longitude , latitude,distance);

                //add to arraylist
                tasteList.add(tempLocation);


                //add items to listview
                BreweryLocationInfoAdapter adapter1 = new BreweryLocationInfoAdapter(c ,R.layout.toptaste_layout, tasteList);
                lv.setAdapter(adapter1);

                //set up clicks
                lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> arg0, View arg1,
                                            int arg2, long arg3) {
                        ShortBeerInfo o=(ShortBeerInfo)arg0.getItemAtPosition(arg2);

                        String tempID = o.id;
                        String tempBrewID = o.brewery;

                        Toast toast = Toast.makeText(c, tempID, Toast.LENGTH_SHORT);
                        toast.show();

                        //get beer details from id

                        Intent myIntent = new Intent(c, BeerPage2.class);
                        myIntent.putExtra("id", tempID);
                        myIntent.putExtra("breweryID", tempBrewID);
                        c.startActivity(myIntent);


                    }
                });




            }

        }
        catch(Exception e){

        }

        Dialog.dismiss();

    }

    public String readJSONFeed(String URL) {
        StringBuilder stringBuilder = new StringBuilder();
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(URL);
        try {
            HttpResponse response = httpClient.execute(httpGet);
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            if (statusCode == 200) {
                HttpEntity entity = response.getEntity();
                InputStream inputStream = entity.getContent();
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(inputStream));
                String line;
                while ((line = reader.readLine()) != null) {
                    stringBuilder.append(line);
                }
                inputStream.close();
            } else {
                Log.d("JSON", "Failed to download file");
            }
        } catch (Exception e) {
            Log.d("readJSONFeed", e.getLocalizedMessage());
        }
        return stringBuilder.toString();
    }

}

Upvotes: 0

Views: 533

Answers (4)

tkolev
tkolev

Reputation: 1

Don't hold the data in your custom adapter. Always use

BreweryLocationData beer = getItem(position);

to obtain the object that should be represented by the adapter. This way you can avoid issues with the data being modified outside of your adapter.

Upvotes: 0

Senap
Senap

Reputation: 41

Row 50 seems to be

holder.txtBrewery.setText(beer.brewery);

So either the TextView which should have id R.id.beerNameList has another id or beer.brewery is null.

You can solve these type of issues quickly if you use the debugger. Just put a breakpoint at the offending line, run the debugger and it will pause at the given line. You can then see exactly which object is null, allowing you to fix the problem in no time.

Upvotes: 0

Rishabh Srivastava
Rishabh Srivastava

Reputation: 3745

You have not written getCount method in your custom adapter:

public int getCount() {

            return //Something here
        }

Upvotes: 1

Pork &#39;n&#39; Bunny
Pork &#39;n&#39; Bunny

Reputation: 6731

I think for some reason row isn't getting created.

Can you double check by debugging that row != null by the time you arrive set row.setTag(holder).

I can only guess that 1. maybe the context you are using to get your inflater isn't correct, either way I prefer to use the following method

LayoutInflater.from(context).inflate(R.layout.cell, viewGroup, false);

Or, your resourceID is not right..

Upvotes: 1

Related Questions