1

i have problem when im trying to show data in list view from my database the error is "FATAL EXCEPTION: main java.lang.ArrayIndexOutOfBoundsException: length=4; index=4" . i dont understand what is it.

i hope somebody can help me thanks

this is my code

public class FragmentMaintain extends Fragment {

ListView list;
TextView mid;
TextView pid;
TextView status;
TextView description;
TextView note;
ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>();
//URL to get JSON Array
private static String url = "http://10.0.2.2/skripsi/json/testing.php";

//JSON Node Names
private static final String TAG_OS = "maintenance";
private static final String TAG_MID = "mid";
private static final String TAG_PID = "pid";
private static final String TAG_STATUS = "status";
private static final String TAG_DESCRIPTION = "description";
private static final String TAG_NOTE = "note";
JSONArray maintenance = null;
Button Addnotedata;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    Log.d("ZZZ", "ada di oncreateView maintain");
    super.onCreate(savedInstanceState);
    View rootView = inflater.inflate(R.layout.fragment_maintain, container, false);

    new JSONParse().execute();
    return rootView;


}


private class JSONParse extends AsyncTask<String, String, JSONObject> {
    private ProgressDialog pDialog;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mid = (TextView) getActivity().findViewById(R.id.mid);
        pid = (TextView) getActivity().findViewById(R.id.pid);
        status = (TextView) getActivity().findViewById(R.id.status);
        description = (TextView) getActivity().findViewById(R.id.description);
        note = (TextView) getActivity().findViewById(R.id.note);

        pDialog = new ProgressDialog(getActivity());
        pDialog.setMessage("Getting Data ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }
    @Override
    protected JSONObject doInBackground(String... args) {
        //String pid = getArguments().getString("pid");
        //List<NameValuePair> params = new ArrayList<NameValuePair>();
        //params.add(new BasicNameValuePair("pid", pid));
        JSONParser jParser = new JSONParser();
        JSONObject json = jParser.makeHttpRequestWithoutParams(url);
        //Log.d("TES", pid+", "+ json.toString());
        return json;
    }
    @Override
    protected void onPostExecute(JSONObject json) {
        pDialog.dismiss();
        try {
            // Getting JSON Array from URL
            maintenance = json.getJSONArray(TAG_OS);
            for(int i = 0; i < maintenance.length(); i++){
                JSONObject c = maintenance.getJSONObject(i);
                // Storing  JSON item in a Variable
                String mid = c.getString(TAG_MID);
                String pid = c.getString(TAG_PID);
                String status = c.getString(TAG_STATUS);
                String description = c.getString(TAG_DESCRIPTION);
                String note = c.getString(TAG_NOTE);
                // Adding value HashMap key => value
                HashMap<String, String> map = new HashMap<String, String>();
                map.put(TAG_MID, mid);
                map.put(TAG_PID, pid);
                map.put(TAG_STATUS, status);
                map.put(TAG_DESCRIPTION, description);
                map.put(TAG_NOTE, note);
                oslist.add(map);
                list=(ListView) getActivity().findViewById(R.id.list);
                ListAdapter adapter = new SimpleAdapter(getActivity(), oslist,
                        R.layout.list_maintain,
                        new String[] { TAG_MID,TAG_PID, TAG_STATUS, TAG_NOTE}, new int[] {
                        R.id.mid,R.id.pid, R.id.status, R.id.description, R.id.note});
                list.setAdapter(adapter);
                list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                                            int position, long id) {
                        Toast.makeText(getActivity(), "You Clicked at " + oslist.get(+position).get("pid"), Toast.LENGTH_SHORT).show();
                    }
                });
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

}

and this my logcat

10-10 00:10:50.575 2004-2004/com.example.blackcustomzier.skripsi E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.ArrayIndexOutOfBoundsException: length=4; index=4 at android.widget.SimpleAdapter.bindView(SimpleAdapter.java:160) at android.widget.SimpleAdapter.createViewFromResource(SimpleAdapter.java:126) at android.widget.SimpleAdapter.getView(SimpleAdapter.java:114) at android.widget.AbsListView.obtainView(AbsListView.java:2177) at android.widget.ListView.makeAndAddView(ListView.java:1840) at android.widget.ListView.fillDown(ListView.java:675) at android.widget.ListView.fillFromTop(ListView.java:736) at android.widget.ListView.layoutChildren(ListView.java:1655) at android.widget.AbsListView.onLayout(AbsListView.java:2012) at android.view.View.layout(View.java:14289) at android.view.ViewGroup.layout(ViewGroup.java:4562) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525) at android.widget.LinearLayout.onLayout(LinearLayout.java:1434) at android.view.View.layout(View.java:14289) at android.view.ViewGroup.layout(ViewGroup.java:4562) at android.widget.FrameLayout.onLayout(FrameLayout.java:448) at android.view.View.layout(View.java:14289)

4
  • Look at your Adapter instantiation. The arrays for the data keys and the View IDs are different sizes. Commented Oct 10, 2014 at 4:27
  • What is +position in oslist.get(+position).get("pid"); ? and ur String[] and int[] is not matching Commented Oct 10, 2014 at 4:27
  • FragmentMaintain is not listed in your stack trace. You need to post the code that causing the problem. Commented Oct 10, 2014 at 4:33
  • I think you missing TAG_DESCRIPTION in String Array. Commented Oct 10, 2014 at 4:47

3 Answers 3

1

If the length is 4, max index should be 3 as the index starts from 0.

Sign up to request clarification or add additional context in comments.

1 Comment

This should be comment.
0

Try this way,hope this will help you to solve your problem.

ListAdapter adapter = new SimpleAdapter(getActivity(), oslist,R.layout.list_maintain,new String[] {TAG_MID,TAG_PID,TAG_STATUS,TAG_DESCRIPTION,TAG_NOTE}, new int[] { R.id.mid,R.id.pid, R.id.status, R.id.description, R.id.note});

You are missing to pass TAG_DESCRIPTION in String Array at adapter initialization.

Comments

0

When you create SimpleAdapter the 4th and 5th arguments need to be same size.

So strings of 4th argument will be used as keys to fill view with value of the map which is 3rd argument of constructor.

And the way matching a view with a key is specifying them in 4th and 5th arguments which are containing keys and containing ids of views respectively, to have same index in each array.

See this

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.