无法从CursorWindow读取第0行col -1。在从中访问数据之前,请确保正确初始化Cursor

时间:2014-06-05 08:57:39

标签: java android csv

 public class ThirdActivity extends Activity {
        ListView listView ;
        String TABLE;
        String MAKE;
        String MODEL;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.table_main);
            Bundle extras = getIntent().getExtras();
            if (extras != null) {
                 TABLE= extras.getString("TABLE_NAME");
                 MODEL = extras.getString("CHILD_VALUE");
                 MAKE = extras.getString("PARENT_VALUE");
            }

            //LinearLayout LLayout = (LinearLayout) findViewById(R.id.table);
            TableLayout t1;
            TableLayout tl = (TableLayout) findViewById(R.id.main_table);
            TableRow tr_head = new TableRow(this);
            tr_head.setId(10);
            tr_head.setBackgroundColor(Color.GRAY);
            tr_head.setLayoutParams(new LayoutParams(
            LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT));

            TextView S_No = new TextView(this);
            S_No.setId(20);
            S_No.setText("S.No");
            S_No.setTextColor(Color.WHITE);
            S_No.setPadding(5, 5, 5, 5);
            tr_head.addView(S_No);// add the column to the table row here

            TextView Fitment = new TextView(this);
            Fitment.setId(21);// define id that must be unique
            Fitment.setText("Fitment Location"); // set the text for the header 
            Fitment.setTextColor(Color.WHITE); // set the color
            Fitment.setPadding(5, 5, 5, 5); // set the padding (if required)
            tr_head.addView(Fitment); // add the column to the table row here

            TextView Part_No = new TextView(this);
            Part_No.setId(22);
            Part_No.setText("Part.No");
            Part_No.setTextColor(Color.WHITE);
            Part_No.setPadding(5, 5, 5, 5);
            tr_head.addView(Part_No);// add the column to the table row here

            TextView Nrb_No = new TextView(this);
            Nrb_No.setId(23);// define id that must be unique
            Nrb_No.setText("NRB.No"); // set the text for the header 
            Nrb_No.setTextColor(Color.WHITE); // set the color
            Nrb_No.setPadding(5, 5, 5, 5); // set the padding (if required)
            tr_head.addView(Nrb_No);

            TextView Dimension = new TextView(this);
            Dimension.setId(24);// define id that must be unique
            Dimension.setText("Dimension"); // set the text for the header 
            Dimension.setTextColor(Color.WHITE); // set the color
            Dimension.setPadding(5, 5, 5, 5); // set the padding (if required)
            tr_head.addView(Dimension);

            TextView No_Off = new TextView(this);
            No_Off.setId(25);// define id that must be unique
            No_Off.setText("No_Off"); // set the text for the header 
            No_Off.setTextColor(Color.WHITE); // set the color
            No_Off.setPadding(5, 5, 5, 5); // set the padding (if required)
            tr_head.addView(No_Off);

            tl.addView(tr_head, new TableLayout.LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));

            DataBaseHelper databasehelper = new DataBaseHelper(this);
            SQLiteDatabase sqlitedatabase = databasehelper.getWritableDatabase();
            Cursor c = sqlitedatabase.rawQuery("SELECT Fitment Location,Part_No,NRB_No,Dimensions,No_Off,Company FROM "+TABLE+ 
                    " WHERE Make='"+MAKE+"' AND Model = '"+MODEL+"'" , null);
           String k = "";
           int count=0;
           if (c != null ) {
               if  (c.moveToFirst()) {
                     do {
                        //String firstName = c.getString(c.getColumnIndex(""));
                        String fitment = c.getString(c.getColumnIndex("Fitment Location"));
                        String part_no = c.getString(c.getColumnIndex("Part_No"));
                        String nrb_no = c.getString(c.getColumnIndex("NRB_No"));
                        String dimension = c.getString(c.getColumnIndex("Dimensions"));
                        String no_off = c.getString(c.getColumnIndex("No_Off"));
                        String company = c.getString(c.getColumnIndex("Company"));
                        //results.add("" + firstName + ",Age: " + age);
                        //k += firstName + " "+ age;
                        TableRow tr = new TableRow(this);
                        if(count%2!=0) tr.setBackgroundColor(Color.GRAY);
                        tr.setId(100+count);
                        tr.setLayoutParams(new LayoutParams(
                        LayoutParams.FILL_PARENT,
                        LayoutParams.WRAP_CONTENT));

                        //Create two columns to add as table data
                         // Create a TextView to add S.No
                        TextView S_No1 = new TextView(this);
                        S_No1.setId(200+count); 
                        S_No1.setText(count+1);
                        S_No1.setPadding(2, 0, 5, 0);
                        S_No1.setTextColor(Color.WHITE);
                        tr.addView(S_No1);

                        TextView Fitment1 = new TextView(this);
                        Fitment1.setId(300+count);// define id that must be unique
                        Fitment1.setText(fitment); // set the text for the header 
                        Fitment1.setTextColor(Color.WHITE); // set the color
                        Fitment1.setPadding(2, 0, 5, 0); // set the padding (if required)
                        tr_head.addView(Fitment1); // add the column to the table row here

                        TextView Part_No1 = new TextView(this);
                        Part_No1.setId(400+count);
                        Part_No1.setText(part_no);
                        Part_No1.setTextColor(Color.WHITE);
                        Part_No1.setPadding(2, 0, 5, 0);
                        tr_head.addView(Part_No1);// add the column to the table row here

                        TextView Nrb_No1 = new TextView(this);
                        Nrb_No1.setId(500+count);// define id that must be unique
                        Nrb_No1.setText(nrb_no); // set the text for the header 
                        Nrb_No1.setTextColor(Color.WHITE); // set the color
                        Nrb_No1.setPadding(2, 0, 5, 0); // set the padding (if required)
                        tr_head.addView(Nrb_No1);

                        TextView Dimension1 = new TextView(this);
                        Dimension1.setId(600+count);// define id that must be unique
                        Dimension1.setText(dimension); // set the text for the header 
                        Dimension1.setTextColor(Color.WHITE); // set the color
                        Dimension1.setPadding(2, 0, 5, 0); // set the padding (if required)
                        tr_head.addView(Dimension1);

                        TextView No_Off1 = new TextView(this);
                        No_Off1.setId(700+count);// define id that must be unique
                        No_Off1.setText(no_off); // set the text for the header 
                        No_Off1.setTextColor(Color.WHITE); // set the color
                        No_Off1.setPadding(2, 0, 5, 0); // set the padding (if required)
                        tr_head.addView(No_Off1);

                        // finally add this to the table row
                        tl.addView(tr, new TableLayout.LayoutParams(
                                                LayoutParams.FILL_PARENT,
                                                LayoutParams.WRAP_CONTENT));
                                       count++;
                     }while (c.moveToNext());
               } 
          }

        } 
    }

我的LOGCAT是:

06-05 05:33:08.662: E/AndroidRuntime(2983): FATAL EXCEPTION: main
06-05 05:33:08.662: E/AndroidRuntime(2983): Process: com.example.nrbapp, PID: 2983
06-05 05:33:08.662: E/AndroidRuntime(2983): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nrbapp/com.example.nrbapp.ThirdActivity}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
06-05 05:33:08.662: E/AndroidRuntime(2983):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-05 05:33:08.662: E/AndroidRuntime(2983):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-05 05:33:08.662: E/AndroidRuntime(2983):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-05 05:33:08.662: E/AndroidRuntime(2983):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-05 05:33:08.662: E/AndroidRuntime(2983):     at android.os.Handler.dispatchMessage(Handler.java:102)
06-05 05:33:08.662: E/AndroidRuntime(2983):     at android.os.Looper.loop(Looper.java:136)
06-05 05:33:08.662: E/AndroidRuntime(2983):     at android.app.ActivityThread.main(ActivityThread.java:5017)
06-05 05:33:08.662: E/AndroidRuntime(2983):     at java.lang.reflect.Method.invokeNative(Native Method)
06-05 05:33:08.662: E/AndroidRuntime(2983):     at java.lang.reflect.Method.invoke(Method.java:515)
06-05 05:33:08.662: E/AndroidRuntime(2983):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-05 05:33:08.662: E/AndroidRuntime(2983):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-05 05:33:08.662: E/AndroidRuntime(2983):     at dalvik.system.NativeStart.main(Native Method)
06-05 05:33:08.662: E/AndroidRuntime(2983): Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
06-05 05:33:08.662: E/AndroidRuntime(2983):     at android.database.CursorWindow.nativeGetString(Native Method)
06-05 05:33:08.662: E/AndroidRuntime(2983):     at android.database.CursorWindow.getString(CursorWindow.java:434)
06-05 05:33:08.662: E/AndroidRuntime(2983):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
06-05 05:33:08.662: E/AndroidRuntime(2983):     at com.example.nrbapp.ThirdActivity.onCreate(ThirdActivity.java:133)
06-05 05:33:08.662: E/AndroidRuntime(2983):     at android.app.Activity.performCreate(Activity.java:5231)
06-05 05:33:08.662: E/AndroidRuntime(2983):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-05 05:33:08.662: E/AndroidRuntime(2983):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-05 05:33:08.662: E/AndroidRuntime(2983):     ... 11 more

2 个答案:

答案 0 :(得分:0)

方法roundTwoDecimal返回null,您在toString变量null上运行weight_kg

labelWEIGHT.setText(weight_kg.toString());

答案 1 :(得分:0)

你写下面的代码我觉得这完全错了,也许你忘了写一些代码:

Cursor cursor = null;
Integer count=0;
while (cursor.moveToNext())

你将光标设置为null,然后你试图获得值格式,我认为这会返回NPE,

如果这没有帮助你指出第65行

//更新

您无法将列创建为名称Fitment Location,您无法在列名中使用空格。您收到此错误,因为您的表未正确创建