来自sqlite数据库的achartengine条形图

时间:2016-04-16 19:35:07

标签: android sqlite achartengine

我试图从我的sqlite数据库中的数据创建一个achartengine条形图,但是当我尝试填充条形图的数据集时,我收到以下错误:

Caused by: java.lang.IllegalStateException: Couldn't read row 1, col 1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.

TestAdapter.java

public Cursor getDaysGraphDataY()
{
    try
    {
        String sql ="SELECT data FROM v_data_main WHERE datatype = 'd' ORDER BY CAST(number AS INTEGER) DESC";

        Cursor mCur = mDb.rawQuery(sql, null);
        if (mCur!=null)
        {
            mCur.moveToNext();
        }
        return mCur;
    }
    catch (SQLException mSQLException)
    {
        Log.e(TAG, "getDaysGraphDataY >>"+ mSQLException.toString());
        throw mSQLException;
    }
}

public Cursor getDaysGraphDataX()
{
    try
    {
        String sql ="SELECT number FROM v_data_main WHERE datatype = 'd' ORDER BY CAST(number AS INTEGER) DESC";

        Cursor mCur = mDb.rawQuery(sql, null);
        if (mCur!=null)
        {
            mCur.moveToNext();
        }
        return mCur;
    }
    catch (SQLException mSQLException)
    {
        Log.e(TAG, "getDaysGraphDataX >>"+ mSQLException.toString());
        throw mSQLException;
    }
}

BarChart.java

public class BarGraph{

public Intent getIntent(Context context) 
{   
    // Bar 1
    //int[] y = { 124, 135, 443, 456, 234, 123, 342, 134, 123, 643, 234, 274 };
    //context = context.getApplicationContext();
    TestAdapter mDbHelper = new TestAdapter(context);
    mDbHelper.createDatabase();
    mDbHelper.open();

    Cursor getDataY = mDbHelper.getDaysGraphDataY();
    Cursor getDataX = mDbHelper.getDaysGraphDataX();

    int rowsY = getDataY.getCount();
    int rowsX = getDataX.getCount();

    getDataY.moveToFirst();
    getDataX.moveToFirst();

    CategorySeries series = new CategorySeries("Demo Bar Graph 1");
    for (int i = 0; i < rowsY; i++) {
        series.add(getDataX.getString(i), getDataY.getInt(i));
        getDataY.moveToNext();
        getDataX.moveToNext();
    }
    mDbHelper.close();
    /*
    // Bar 2
    int[] y2 = { 224, 235, 243, 256, 234, 223, 242, 234, 223, 243, 234, 274 };
    CategorySeries series2 = new CategorySeries("Demo Bar Graph 2");
    for (int i = 0; i < y.length; i++) {
        series2.add("Bar " + (i+1), y2[i]);
    }
    */

    XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
    dataset.addSeries(series.toXYSeries());
    //dataset.addSeries(series2.toXYSeries());

    // This is how the "Graph" itself will look like
    XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
    mRenderer.setChartTitle("Demo Graph Title");
    mRenderer.setXTitle("X VALUES");
    mRenderer.setYTitle("Y VALUES");
    mRenderer.setAxesColor(Color.GREEN);
    mRenderer.setLabelsColor(Color.RED);
    // Customize bar 1
    XYSeriesRenderer renderer = new XYSeriesRenderer();
    renderer.setDisplayChartValues(true);
    renderer.setChartValuesSpacing((float) 0.5);
    mRenderer.addSeriesRenderer(renderer);
    // Customize bar 2
    XYSeriesRenderer renderer2 = new XYSeriesRenderer();
    renderer.setColor(Color.CYAN);
    renderer.setDisplayChartValues(true);
    renderer.setChartValuesSpacing((float) 0.5);
    mRenderer.addSeriesRenderer(renderer2);

    Intent intent = ChartFactory.getBarChartIntent(context, dataset,mRenderer, Type.DEFAULT);
    return intent;
}

}

我已尝试按其他问题的建议添加_id列,并卸载应用并重新安装。

这是完整的logcat:

    04-16 17:38:23.625 3334-3334/? E/CursorWindow: Failed to read row 1, column 1 from a CursorWindow which has 30 rows, 1 columns.
04-16 17:38:23.629 3334-3334/? D/AndroidRuntime: Shutting down VM
04-16 17:38:23.630 3334-3334/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                 Process: com.marnistek.serverstats, PID: 3334
                                                 java.lang.IllegalStateException: Could not execute method for android:onClick
                                                     at android.view.View$DeclaredOnClickListener.onClick(View.java:4452)
                                                     at android.view.View.performClick(View.java:5198)
                                                     at android.view.View$PerformClick.run(View.java:21147)
                                                     at android.os.Handler.handleCallback(Handler.java:739)
                                                     at android.os.Handler.dispatchMessage(Handler.java:95)
                                                     at android.os.Looper.loop(Looper.java:148)
                                                     at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                  Caused by: java.lang.reflect.InvocationTargetException
                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                     at android.view.View$DeclaredOnClickListener.onClick(View.java:4447)
                                                     at android.view.View.performClick(View.java:5198) 
                                                     at android.view.View$PerformClick.run(View.java:21147) 
                                                     at android.os.Handler.handleCallback(Handler.java:739) 
                                                     at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                     at android.os.Looper.loop(Looper.java:148) 
                                                     at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                     at java.lang.reflect.Method.invoke(Native Method) 
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                                  Caused by: java.lang.IllegalStateException: Couldn't read row 1, col 1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
                                                     at android.database.CursorWindow.nativeGetString(Native Method)
                                                     at android.database.CursorWindow.getString(CursorWindow.java:438)
                                                     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
                                                     at com.marnistek.serverstats.BarGraph.getIntent(BarGraph.java:36)
                                                     at com.marnistek.serverstats.GraphAChartEngineActivity.barGraphHandler(GraphAChartEngineActivity.java:27)
                                                     at java.lang.reflect.Method.invoke(Native Method) 
                                                     at android.view.View$DeclaredOnClickListener.onClick(View.java:4447) 
                                                     at android.view.View.performClick(View.java:5198) 
                                                     at android.view.View$PerformClick.run(View.java:21147) 
                                                     at android.os.Handler.handleCallback(Handler.java:739) 
                                                     at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                     at android.os.Looper.loop(Looper.java:148) 
                                                     at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                     at java.lang.reflect.Method.invoke(Native Method) 
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

尝试从Cursor中获取原始值(int,long)时发生了这些异常。不幸的是,这两个例外都没有解释这个问题究竟是什么。同样重要的是,这些例外与任何特定的Android API版本无关,也与特定的设备制造商无关。

请点击此链接了解更多详情:
http://blog.tapreason.com/2014/03/02/how-to-resolve-java-lang-illegalstateexception-couldnt-read-row-0-col-y-from-cursorwindow-make-sure-the-cursor-is-initialized-correctly-before-accessing-data-from-it/

答案 1 :(得分:0)

我通过改变我的循环来实现它:

for (int i = 0; i < rowsY; i++) {
    series.add(getDataX.getString(i), getDataY.getInt(i));
    getDataY.moveToNext();
    getDataX.moveToNext();
}

要:

for (int i = 0; i < rows; i++) {
        series.add(getData.getString(getData.getColumnIndex("number")), getData.getInt(getData.getColumnIndex("data")));
        getData.moveToPosition(i);
    }

我还将它更改为仅使用一个游标从db获取我需要的所有列,而不是为每个X轴和Y轴分别使用单独的游标