Android App Crashing - 列表视图 - CSV文件

时间:2014-10-07 08:40:13

标签: java android listview csv android-listview

好的,我正在尝试填充列表视图,但在加载时它正在崩溃。

将产品从CSV导入Android Studio中的列表视图。

这是我的CSV https://drive.google.com/file/d/0BycsF7WMf1cYMzBOMk1mZmliWW8/view?usp=sharing

MainActivity:

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

public class MainActivity extends Activity {

    ListViewAdapter itemAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView itemList = (ListView)findViewById(R.id.itemListView);

        itemAdapter = new ListViewAdapter(this, -1);

        itemList.setAdapter(itemAdapter);

    }
}

ListViewAdapter

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class ListViewAdapter extends ArrayAdapter<DataStates> {
    Context contxt;

    public ListViewAdapter(Context context, int testViewResourceID) {
        super(context, testViewResourceID);
        contxt = context;
        loadArrayData();
    }

    @Override
    public View getView (final int pos, View convertView, final ViewGroup parent) {
        TextView mView = (TextView) convertView;

        if(mView == null) {
            mView = new TextView(parent.getContext());
            mView.setTextSize(28);
        }

        return mView;
    }

private void loadArrayData() {
    try {
        InputStream is = contxt.getAssets().open("data.csv");
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        String line;

        while ((line = reader.readLine()) != null) {

            String[] RowData = line.split(",");

            DataStates cur = new DataStates();
            cur.setTitle(RowData[2]);
            cur.setPrice(RowData[3]);
            cur.setDescription(RowData[4]);

            this.add(cur);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

DataStates

import android.net.Uri;

public class DataStates {

    private String title;
    private String price;
    private String description;



    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }


    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }






}

activity_main

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/itemListView"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentStart="true" />
</LinearLayout>

LogCat信息

10-07 21:43:31.241  25819-25819/tk.delacour.projectalpha D/dalvikvm﹕ Late-enabling CheckJNI
10-07 21:43:31.241  25819-25819/tk.delacour.projectalpha D/dalvikvm﹕ Try to disable coredump for pid 25819
10-07 21:43:31.241  25819-25819/tk.delacour.projectalpha D/dalvikvm﹕ Process 25819 nice name: tk.delacour.projectalpha
10-07 21:43:31.241  25819-25819/tk.delacour.projectalpha D/dalvikvm﹕ Extra Options: not specified
10-07 21:43:31.401  25819-25819/tk.delacour.projectalpha D/AndroidRuntime﹕ Shutting down VM
10-07 21:43:31.401  25819-25819/tk.delacour.projectalpha W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x42f05500)
10-07 21:43:31.411  25819-25819/tk.delacour.projectalpha E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{tk.delacour.projectalpha/tk.delacour.projectalpha.MainActivity}: java.lang.ArrayIndexOutOfBoundsException: length=4; index=4
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2342)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
            at android.app.ActivityThread.access$600(ActivityThread.java:148)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:150)
            at android.app.ActivityThread.main(ActivityThread.java:5390)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.ArrayIndexOutOfBoundsException: length=4; index=4
            at tk.delacour.projectalpha.ListViewAdapter.loadArrayData(ListViewAdapter.java:63)
            at tk.delacour.projectalpha.ListViewAdapter.<init>(ListViewAdapter.java:28)
            at tk.delacour.projectalpha.MainActivity.onCreate(MainActivity.java:19)
            at android.app.Activity.performCreate(Activity.java:5303)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2296)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
            at android.app.ActivityThread.access$600(ActivityThread.java:148)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:150)
            at android.app.ActivityThread.main(ActivityThread.java:5390)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:0)

索引从0开始,所以

cur.setDescription(RowData[4]);

将导致NPE,因为在您的CSV中,您只有4列(索引为0,1,2,3)。

您的案例中的描述是3

答案 1 :(得分:0)

好的,你的程序在这一点上失败了:

while ((line = reader.readLine()) != null) {

        String[] RowData = line.split(",");
        DataStates cur = new DataStates();
        cur.setTitle(RowData[2]);
        cur.setPrice(RowData[3]);
        // now there is a ArrayIndexOutOfBoundsException
        cur.setDescription(RowData[4]);

        this.add(cur);
    }

索引从0开始,所以我认为正确的代码是

while ((line = reader.readLine()) != null) {

        String[] RowData = line.split(",");
        if(RowData.length <4)
             continue;
        DataStates cur = new DataStates();
        cur.setTitle(RowData[1]);
        cur.setPrice(RowData[2]);
        cur.setDescription(RowData[3]);

        this.add(cur);
    }

答案 2 :(得分:0)

您需要替换

cur.setTitle(RowData[2]);
cur.setPrice(RowData[3]);
cur.setDescription(RowData[4]);

cur.setTitle(RowData[1]);
cur.setPrice(RowData[2]);
cur.setDescription(RowData[3]);

您在RowData中只有4个值,并且您正尝试访问第5个值。

Caused by: java.lang.ArrayIndexOutOfBoundsException: length=4; index=4

您需要考虑索引0到3.不是1到4。

希望这有帮助。