使用onArticleSelected传递数据是强制关闭我的应用程序

时间:2014-06-26 15:17:48

标签: android listview android-fragments

我有一个对象列表,它们会进入列表视图。当单击listview中的项目时,我想要一个新的片段打开来自新片段中我的对象的数据。当我点击listview中的项目时,我收到此错误:

06-26 11:05:47.564  14384-14384/com.beerportfolio.beerportfoliopro E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.beerportfolio.beerportfoliopro, PID: 14384
    java.lang.NullPointerException
            at com.beerportfolio.beerportfoliopro.GetLocationsJSON$1.onItemClick(GetLocationsJSON.java:132)
            at android.widget.AdapterView.performItemClick(AdapterView.java)
            at android.widget.AbsListView.performItemClick(AbsListView.java)
            at android.widget.AbsListView$PerformClick.run(AbsListView.java)
            at android.widget.AbsListView$3.run(AbsListView.java)
            at android.os.Handler.handleCallback(Handler.java)
            at android.os.Handler.dispatchMessage(Handler.java)
            at android.os.Looper.loop(Looper.java)
            at android.app.ActivityThread.main(ActivityThread.java)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
            at dalvik.system.NativeStart.main(Native Method)

发生崩溃的代码:

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

    Context c;
    private ProgressDialog Dialog;

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

    //****************code for on click
    OnArticleSelectedListener listenerBeer;
    public interface OnArticleSelectedListener{
        public void onArticleSelected(LocationData d);


    }
    public void setOnArticleSelectedListener(OnArticleSelectedListener listener){
        this.listenerBeer = listener;


    }
    //******************end code for onClick

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

    protected void onPreExecute() {
        Dialog.setMessage("Searching For Brewery Locations");

        Dialog.setTitle("Loading");
        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.locationlist);


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

            String phone;
            String hours;

            //get json items
            for(int i = 0; i < json.getJSONObject("data").getJSONArray("locations").length(); i++) {

                String name = json.getJSONObject("data").getJSONArray("locations").getJSONObject(i).getString("name");
                Log.d("location" , name);
                try {
                    phone = json.getJSONObject("data").getJSONArray("locations").getJSONObject(i).getString("phone");
                }
                catch(JSONException e){
                    phone = "N/A";
                }
                String lat = json.getJSONObject("data").getJSONArray("locations").getJSONObject(i).getString("latitude");
                String longitude = json.getJSONObject("data").getJSONArray("locations").getJSONObject(i).getString("longitude");
                String open = json.getJSONObject("data").getJSONArray("locations").getJSONObject(i).getString("openToPublic");


                try {
                    hours = json.getJSONObject("data").getJSONArray("locations").getJSONObject(i).getString("hoursOfOperation");
                }
                catch (JSONException e){
                    hours = "N/A";
                }

                String type = json.getJSONObject("data").getJSONArray("locations").getJSONObject(i).getString("locationTypeDisplay");


                //todo: create location object
                LocationData thisBeer = new LocationData(name, phone, lat, longitude, open, hours, type);

                //add beer to list
                beerList.add(thisBeer);


            }

            //update listview
            LocationAdapter adapter1 = new LocationAdapter(c ,R.layout.listview_item_row, beerList);
            lv.setAdapter(adapter1);

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

                    LocationData beerInfo = beerList.get(arg2);

                    //************************Launch listener interface
                    listenerBeer.onArticleSelected(beerInfo);

                }
            });




        }
        catch(Exception e){

        }

        Dialog.dismiss();

    }

上述代码中发生崩溃的第132行是:

listenerBeer.onArticleSelected(beerInfo);

听众的其余部分在这里:

public class BreweryLocations extends Fragment implements GetLocationsJSON.OnArticleSelectedListener {

    String beerId = "";
    Dialog dialog = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        //set layout here
        View v = inflater.inflate(R.layout.brewerylocation_page, container, false);
        setHasOptionsMenu(true);



        //get user information
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
        String userName = prefs.getString("userName", null);
        String userID = prefs.getString("userID", null);


        //Todo: code for retrieving brewery location
        String breweryId = prefs.getString("breweryID", null);
        String url2 = "myURL";


        //call async task now!
        new GetLocationsJSON(getActivity()).execute(url2);


        Log.d("location" , url2);

        // Inflate the layout for this fragment
        return v;

    }

    @Override
    public void onArticleSelected(LocationData d){

        //code to execute on click
        Fragment Fragment_one;
        FragmentManager man= getFragmentManager();
        FragmentTransaction tran = man.beginTransaction();

        //adds beer data to shared prefs for beer tabs
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());

        SharedPreferences.Editor editor = preferences.edit();

        editor.putString("name",d.name);
        editor.putString("hours",d.hours);
        editor.putString("lat",d.lat);
        editor.putString("lng",d.lng);
        editor.putString("phone",d.phone);

        editor.commit();
        Fragment_one = new BeerTabs();

        tran.replace(R.id.main, Fragment_one);//tran.
        tran.addToBackStack(null);
        tran.commit();

    }







}

1 个答案:

答案 0 :(得分:1)

看起来你的listenerBeer为null。

尝试更改

//call async task now! new GetLocationsJSON(getActivity()).execute(url2);

GetLocationsJSON locationJson = new GetLocationJSON(getActivity());
locationJson.setOnArticleSelectedListener(this);
locationJson.execute(url2);