0

当我尝试从我的 android 应用程序的 listView 中删除一行时,我收到了 ArrayIndexOutOfBoundsException。以下是代码:

 String[] items;
FileInputStream fis;
String FILENAME = "rundata";
String collected;
Context context = this;
int itemPosition;
FileOutputStream fos;
ArrayAdapter<String> adapter;
private static LayoutInflater inflater=null;

 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


}

 @Override
protected void onResume(){
    super.onResume();
    inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    collected = null;

    try {
        fis = openFileInput(FILENAME);
        byte[] dataArray = new byte[fis.available()];
        while(fis.read(dataArray) != -1){
            System.out.println("in while");
            collected = new String(dataArray);
            if(collected.equals("")){
                System.out.println("break");
                break;
            }else{
                System.out.println("continue");
            }
        }
        fis.close();

        if(collected.contains("NEXTLINE")){
            System.out.println("in if next line");
            items = collected.split("NEXTLINE");
        }else{
            System.out.println("in else next line");
            items = new String[0];
        }


        System.out.println("items.length: "+items.length);

        adapter = new ArrayAdapter<String>(this, R.layout.historyrow, items){

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View vi = convertView;

                if (null == vi) {
                    vi = inflater.inflate(R.layout.historyrow, null);
                } else {
                    vi = convertView;
                }

                TextView text=(TextView)vi.findViewById(R.id.historyTxtTitle);

                JSONObject jObject;
                try {
                    jObject = new JSONObject(items[position]);
                    if(jObject.getString("activityMode").equals("mode1")){
                        text.setText("Mode 1");
                    }else if(jObject.getString("activityMode").equals("mode2")){
                        text.setText("Mode 2");
                    }else if(jObject.getString("activityMode").equals("mode3")){
                        text.setText("Mode 3");
                    }else if(jObject.getString("activityMode").equals("mode4")){
                        text.setText("Mode 4");
                    }else{
                        text.setText("ERROR");
                    }

                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    System.out.println("++++++e: "+e);
                }

                return vi;
            }

        };

        setListAdapter(adapter);

当我尝试删除一行时出现以下错误:

 06-28 11:15:10.593: E/AndroidRuntime(26639): FATAL EXCEPTION: main
 06-28 11:15:10.593: E/AndroidRuntime(26639): java.lang.ArrayIndexOutOfBoundsException
 06-28 11:15:10.593: E/AndroidRuntime(26639):   at com.eplinovo.runnoandroid.HistoryViewActivity$1.getView(HistoryViewActivity.java:88)
 06-28 11:15:10.593: E/AndroidRuntime(26639):   at android.widget.AbsListView.obtainView(AbsListView.java:1443)
 06-28 11:15:10.593: E/AndroidRuntime(26639):   at android.widget.ListView.makeAndAddView(ListView.java:1793)
 06-28 11:15:10.593: E/AndroidRuntime(26639):   at android.widget.ListView.fillDown(ListView.java:718)
 06-28 11:15:10.593: E/AndroidRuntime(26639):   at android.widget.ListView.fillSpecific(ListView.java:1350)
 06-28 11:15:10.593: E/AndroidRuntime(26639):   at android.widget.ListView.layoutChildren(ListView.java:1624)
 06-28 11:15:10.593: E/AndroidRuntime(26639):   at android.widget.AbsListView.onLayout(AbsListView.java:1294)
 06-28 11:15:10.593: E/AndroidRuntime(26639):   at android.view.View.layout(View.java:7175)
 06-28 11:15:10.593: E/AndroidRuntime(26639):   at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
 06-28 11:15:10.593: E/AndroidRuntime(26639):   at android.view.View.layout(View.java:7175)
 06-28 11:15:10.593: E/AndroidRuntime(26639):   at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
 06-28 11:15:10.593: E/AndroidRuntime(26639):   at android.view.View.layout(View.java:7175)
 06-28 11:15:10.593: E/AndroidRuntime(26639):   at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
 06-28 11:15:10.593: E/AndroidRuntime(26639):   at android.view.View.layout(View.java:7175)
 06-28 11:15:10.593: E/AndroidRuntime(26639):   at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
 06-28 11:15:10.593: E/AndroidRuntime(26639):   at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
 06-28 11:15:10.593: E/AndroidRuntime(26639):   at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
 06-28 11:15:10.593: E/AndroidRuntime(26639):   at android.view.View.layout(View.java:7175)
 06-28 11:15:10.593: E/AndroidRuntime(26639):   at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
 06-28 11:15:10.593: E/AndroidRuntime(26639):   at android.view.View.layout(View.java:7175)
 06-28 11:15:10.593: E/AndroidRuntime(26639):   at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
 06-28 11:15:10.593: E/AndroidRuntime(26639):   at android.view.View.layout(View.java:7175)
 06-28 11:15:10.593: E/AndroidRuntime(26639):   at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
 06-28 11:15:10.593: E/AndroidRuntime(26639):   at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
 06-28 11:15:10.593: E/AndroidRuntime(26639):   at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
 06-28 11:15:10.593: E/AndroidRuntime(26639):   at android.view.View.layout(View.java:7175)
 06-28 11:15:10.593: E/AndroidRuntime(26639):   at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
 06-28 11:15:10.593: E/AndroidRuntime(26639):   at android.view.View.layout(View.java:7175)
 06-28 11:15:10.593: E/AndroidRuntime(26639):   at android.view.ViewRoot.performTraversals(ViewRoot.java:1146)
 06-28 11:15:10.593: E/AndroidRuntime(26639):   at android.view.ViewRoot.handleMessage(ViewRoot.java:1865)
 06-28 11:15:10.593: E/AndroidRuntime(26639):   at android.os.Handler.dispatchMessage(Handler.java:99)
 06-28 11:15:10.593: E/AndroidRuntime(26639):   at android.os.Looper.loop(Looper.java:130)
 06-28 11:15:10.593: E/AndroidRuntime(26639):   at android.app.ActivityThread.main(ActivityThread.java:3687)
 06-28 11:15:10.593: E/AndroidRuntime(26639):   at java.lang.reflect.Method.invokeNative(Native Method)
 06-28 11:15:10.593: E/AndroidRuntime(26639):   at java.lang.reflect.Method.invoke(Method.java:507)
 06-28 11:15:10.593: E/AndroidRuntime(26639):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
 06-28 11:15:10.593: E/AndroidRuntime(26639):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
 06-28 11:15:10.593: E/AndroidRuntime(26639):   at dalvik.system.NativeStart.main(Native Method)

有人可以帮我吗?提前致谢。

4

1 回答 1

0

因为列表适配器有一个名为的方法getCount- 适配器假定项目计数是常量值,由该方法返回。删除一行后,您应该调用notifyDataSetChanged适配器的方法,因此它将重新计算数组元素的计数。

于 2012-06-28T09:48:01.523 回答