Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法从ArrayList填充ListView_Java_Android_Json_Listview_Arraylist - Fatal编程技术网

Java 无法从ArrayList填充ListView

Java 无法从ArrayList填充ListView,java,android,json,listview,arraylist,Java,Android,Json,Listview,Arraylist,ChooseCategory.java public class ChooseCategory extends ListActivity { private ListView lv; //ArrayAdapter<FoodStores> arrayAdapter; private ArrayList<FoodStores> storefoodList; private

ChooseCategory.java

        public class ChooseCategory extends ListActivity {
            private ListView lv;
            //ArrayAdapter<FoodStores> arrayAdapter;
            private ArrayList<FoodStores> storefoodList;
        private String URL_STORES = "http://10.0.2.2/get_stores.php";
         @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                 lv = (ListView)findViewById(R.id.list);
                 storefoodList = new ArrayList<FoodStores>();
                 new GetFoodStores().execute();

        }

    private class GetFoodStores extends AsyncTask<Void,Void,Void> {
            @Override
            protected void onPreExecute(){
                super.onPreExecute();
            }

            @Override
            protected Void doInBackground(Void... arg0) {
                ServiceHandlerFood jsonParserFood = new ServiceHandlerFood();
                String json = jsonParserFood.makeServiceCall(URL_STORES, ServiceHandlerFood.GET);
                Log.e("Response: ", " > " + json);
                if(json != null){
                    try{
                        JSONObject jsonObj = new JSONObject(json);
                        if(jsonObj != null){
                            JSONArray storeListFood = jsonObj.getJSONArray("storelistfood");
                            for(int i = 0; i < storeListFood.length(); i++){
                                JSONObject storeFoodObj = (JSONObject) storeListFood.get(i);
                                FoodStores foodStores = new FoodStores(storeFoodObj.getInt("id"),storeFoodObj.getString("STORENAME"));
                                storefoodList.add(foodStores);

                            }
                        }
                    }catch(JSONException e){
                        e.printStackTrace();
                    }
                }else{
                    Log.e("JSON Data", "No data received from server");
                }
                return null;
            }
@Override
        protected void onPostExecute(Void result){
            super.onPostExecute(result);
            populateListView();
        }
    }
喜欢

ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this,R.layout.restaurant_list,labels);
setListAdapter(listAdapter);
并确保您的
ListView
id是
android:id=“@android:id/list”


转到

在从特定索引获取值之前,尝试检查storefoodList数据。
java.lang.NullPointerException
            at call.rocket.user.callarocket.ChooseCategory.populateListView
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this,R.layout.restaurant_list,labels);
setListAdapter(listAdapter);
 lv = (ListView)findViewById(R.id.list);