Home » Android » java.lang.ArrayIndexOutOfBoundsException: length=1; index=1

java.lang.ArrayIndexOutOfBoundsException: length=1; index=1

Posted by: admin November 1, 2017 Leave a comment

Questions:
ArrayList<HashMap<String, String>> food_list;
ListView listView;
JSONArray foodArray = null;
ListAdapter test;



void menu_list_setting(final String name){
    new Thread(){
        @Override
        public void run() {
            super.run();

            try{
                PHPRequest request = new PHPRequest("http://114.70.93.130/mnu/restaurant_menu.php");
                String result = request.PhP_food_menu(name);

                try{
                    JSONObject jsonObject = new JSONObject(result);
                    foodArray = jsonObject.getJSONArray("result");

                    for(int i = 0; i<foodArray.length(); i++){
                        HashMap<String, String> food = new HashMap<String, String>();

                        JSONObject c = foodArray.getJSONObject(i);

                        food.put("food_name",c.getString("food_name"));
                        food.put("price",c.getString("price"));

                        food_list.add(food);
                    }

                    if(food_list.size() == 0){
                        Toast.makeText(getApplication(),"메뉴를 불러오지 못했습니다.",Toast.LENGTH_SHORT).show();
                    }else{
                        setting_handler.sendEmptyMessage(0);
                    }

                }catch (JSONException e){
                    e.printStackTrace();
                }
            }catch (MalformedURLException e){
                e.printStackTrace();
            }
        }
    }.start();
}

Handler setting_handler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        ListAdapter adapter = new SimpleAdapter(MenuActivity.this, food_list, R.layout.item_menu,
                new String[] {"food_name, price"},
                new int[] {R.id.Menu_item_name_text, R.id.Menu_item_price_text});

        listView = (ListView)findViewById(R.id.Menu_list);
        listView.setAdapter(adapter);

    }
};

java.lang.ArrayIndexOutOfBoundsException: length=1; index=1

I get an error when I try to get two data through php and print to ListView.
help me.

I am working on receiving and printing the values from the database during Android development.
I have an error, but we can not do it.

Answers: