致命异常:主java.lang.ArrayIndexOutOfBoundsException:

时间:2014-10-10 04:20:16

标签: java android listview

我在尝试从数据库中显示列表视图中的数据时遇到问题 错误是" 致命例外:主要     java.lang.ArrayIndexOutOfBoundsException:length = 4;索引= 4 " 。我不明白它是什么。

我希望有人可以帮助我 感谢

这是我的代码

  

公共类FragmentMaintain扩展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();
        }
    }
}
     

}

这是我的logcat

  

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

3 个答案:

答案 0 :(得分:1)

如果长度为4,则索引从0开始,max index应为3。

答案 1 :(得分:0)

创建SimpleAdapter时,第4和第5个参数需要大小相同。

因此,第4个参数的字符串将用作填充视图的键,其中map是构造函数的第3个参数。

将视图与键匹配的方式是在第4和第5个参数中指定它们,这些参数分别包含键和包含视图的ID,以在每个数组中具有相同的索引。

See this

答案 2 :(得分:0)

请尝试这种方式,希望这有助于您解决问题。

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});

在适配器初始化时,您缺少在String Array中传递TAG_DESCRIPTION。