自定义Listview强制关闭

时间:2013-08-31 02:15:50

标签: android android-listview android-adapter

我创建了一个自定义列表视图,可以保持关闭状态,但我无法弄清楚原因。我想我可能没有正确地命名其中一个视图,因为我正在将该视图建模为另一个视图。但是我找不到我的错误。

这是我的强制关闭错误:

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)

我的BreweryLocationInfoAdapter代码:

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代码:

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();
    }

}

4 个答案:

答案 0 :(得分:1)

我认为由于某种原因,行没有被创建。

你可以通过调试那行来仔细检查!=到达set row.setTag(holder)时的null。

我只能猜测1.也许您用来获取inflater的上下文不正确,无论哪种方式我更喜欢使用以下方法

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

或者,您的resourceID不对..

答案 1 :(得分:1)

您还没有在自定义适配器中编写getCount方法:

public int getCount() {

            return //Something here
        }

答案 2 :(得分:0)

第50行似乎是

holder.txtBrewery.setText(beer.brewery);

因此,应该具有标识R.id.beerNameList的TextView具有另一个标识或beer.brewery为空。

如果使用调试器,则可以快速解决这些类型的问题。只需在违规行放置断点,运行调试器,它将在给定行暂停。然后,您可以确切地查看哪个对象是null,从而可以立即解决问题。

答案 3 :(得分:0)

不要将数据保存在自定义适配器中。始终使用

BreweryLocationData beer = getItem(position);

获取应该由适配器表示的对象。这样,您可以避免在适配器外部修改数据的问题。