使用Android将自定义适配器设置为listview时获取Null Pointer Exception

时间:2014-04-03 10:51:59

标签: android listview fragment custom-adapter

将自定义适配器设置为 listview 时,我收到Null Pointer Exception。

注意。我正在使用片段

我认为这是问题所在,因为如果我删除它就没有错误:

 lvwVenues.setAdapter(adapter);

这是我当前的代码 VenuesFragment.java

    public class VenuesFragment extends Fragment {
        private static final String TAG = VenuesFragment.class.getName();
        private static AsyncHttpClient client = new AsyncHttpClient();
        private static ArrayList<String> name_array = new ArrayList<String>();
        private static ArrayList<String> address_array = new ArrayList<String>();
        static ListView lvwVenues;
        static MyBaseAdapter adapter;

        public VenuesFragment(){}

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_events, container, false);
            lvwVenues = (ListView) rootView.findViewById(R.id.lvwVenues);
            try 
            {
                String storageG = Storage.readFile(Constants.storage, this.getActivity());
                JSONObject userObj = new JSONObject(storageG);
                getVenues(userObj.getString("authentication_token"), getActivity());
            } catch (Exception e) 
            {
                e.printStackTrace();
            }
            return rootView;    
        }

        public void getVenues(String token, final Context context){
             client =  new AsyncHttpClient();
             client.addHeader("Authorization", "Token token=" + token);
             client.get(Constants.API_URL_STAGING_V1 + "/venues", new AsyncHttpResponseHandler() {
                 @Override
                 public void onStart() {
                 }
                 @Override
                 public void onSuccess(String response) {
                    try { 
                        JSONArray new_array = new JSONArray(response);
                        Log.v(TAG, "VENUES " + new_array);
                        for (int i = 0, count = new_array.length(); i < count; i++) {
                            JSONObject jsonObject = new_array.getJSONObject(i);
                            name_array.add(jsonObject.getString("name").toString());
                            address_array.add(jsonObject.getString("address").toString());
                        }       
                        adapter = new MyBaseAdapter(context, name_array, address_array);


  Log.v(TAG, "ADAPTER " + adapter);

                        lvwVenues.setAdapter(adapter);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                 }

                @Override
                public void onFailure(Throwable error, String content)
                {
                    error.printStackTrace();
                }
             });
            }




    }

fragment_venues.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

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

</LinearLayout>

MyBaseAdapter.java

public class MyBaseAdapter extends ArrayAdapter<String> {


    private final Context context;
    private final ArrayList<String> venue_names_array;
    private final ArrayList<String> venue_address_array;

    public MyBaseAdapter(Context context, ArrayList<String> name_array, ArrayList<String> address_array) {
        super(context, R.layout.row_listitem, name_array);
        this.context = context;
        this.venue_names_array = name_array;
        this.venue_address_array = address_array;
        Log.v("VENUE NAMES", venue_names_array.toString());
        Log.v("VENUE ADDRESS", venue_address_array.toString());
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.row_listitem, parent, false);

        TextView txtName = (TextView) rowView.findViewById(R.id.name);
        TextView txtDescription = (TextView) rowView.findViewById(R.id.address);
        String venue_name = venue_names_array.get(position).toString();
        String venue_address = venue_address_array.get(position).toString();        
        txtName.setText(venue_name);
        txtDescription.setText(venue_address);
        return rowView;
    }



}

1 个答案:

答案 0 :(得分:0)

我通过为片段

添加onViewCreated回调来修复自己的问题

以下是代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState) {
    return inflater.inflate(R.layout.fragment_venues, container, false);    
}

@Override 
public void onViewCreated (View view, Bundle savedInstanceState) {
    lvwVenues = (ListView) view.findViewById(R.id.lvwVenues);
    try 
    {
        // put your codes here
    } catch (Exception e) 
    {
        e.printStackTrace();
    }
}