从sqlite中提取一些价值

时间:2011-08-27 14:40:53

标签: android sqlite

我的sqlite有问题。我想从我的数据库(sqlite)获得一些价值(x,y)

这是我的代码:

 Cursor notesCursor = mDbHelper.fetchAllNotes();
        startManagingCursor(notesCursor);       

        double x, y ;

        try {
            x = notesCursor.getDouble(notesCursor.getColumnIndexOrThrow(NotesDbAdapter.KEY_ROWID));
        } catch (NumberFormatException e) {
          // TODO
          return;
        }
        try {
            y = notesCursor.getDouble(notesCursor.getColumnIndexOrThrow(NotesDbAdapter.KEY_RESULT));
        } catch (NumberFormatException e) {
          // TODO
          return;
        }
        mCurrentSeries.add(x, y);
        if (mChartView != null) {
          mChartView.repaint();
        }

来自ID的Y和来自Result的X.但它不起作用,我错过了什么?我真的需要帮助,谢谢

我改变了我的代码:

Cursor notesCursor = mDbHelper.fetchAllNotes();
        startManagingCursor(notesCursor);       
        double x, y;

        try {
            notesCursor.moveToFirst();
            x = notesCursor.getDouble(notesCursor.getColumnIndexOrThrow(NotesDbAdapter.KEY_ROWID));
        } catch (NumberFormatException e) {
          e.printStackTrace();
          return;
        }
        try {
            notesCursor.moveToFirst();          
            y = notesCursor.getDouble(notesCursor.getColumnIndexOrThrow(NotesDbAdapter.KEY_RESULT));
        } catch (NumberFormatException e) {
          e.printStackTrace();
          return;
        }

但我仍然有错误,请帮我解决这个问题,谢谢

我的日志猫:

08-28 01:01:55.776: ERROR/AndroidRuntime(669): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.achartengine.chartdemo.demo/org.achartengine.chartdemo.demo.XYChartBuilder}: java.lang.NullPointerException
08-28 01:01:55.776: ERROR/AndroidRuntime(669):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
08-28 01:01:55.776: ERROR/AndroidRuntime(669):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-28 01:01:55.776: ERROR/AndroidRuntime(669):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-28 01:01:55.776: ERROR/AndroidRuntime(669):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-28 01:01:55.776: ERROR/AndroidRuntime(669):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-28 01:01:55.776: ERROR/AndroidRuntime(669):     at android.os.Looper.loop(Looper.java:123)
08-28 01:01:55.776: ERROR/AndroidRuntime(669):     at android.app.ActivityThread.main(ActivityThread.java:4627)
08-28 01:01:55.776: ERROR/AndroidRuntime(669):     at java.lang.reflect.Method.invokeNative(Native Method)
08-28 01:01:55.776: ERROR/AndroidRuntime(669):     at java.lang.reflect.Method.invoke(Method.java:521)
08-28 01:01:55.776: ERROR/AndroidRuntime(669):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-28 01:01:55.776: ERROR/AndroidRuntime(669):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-28 01:01:55.776: ERROR/AndroidRuntime(669):     at dalvik.system.NativeStart.main(Native Method)
08-28 01:01:55.776: ERROR/AndroidRuntime(669): Caused by: java.lang.NullPointerException
08-28 01:01:55.776: ERROR/AndroidRuntime(669):     at org.achartengine.chartdemo.demo.XYChartBuilder.onCreate(XYChartBuilder.java:108)
08-28 01:01:55.776: ERROR/AndroidRuntime(669):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-28 01:01:55.776: ERROR/AndroidRuntime(669):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
08-28 01:01:55.776: ERROR/AndroidRuntime(669):     ... 11 more
嗯,我已经改变了我的代码,就像这样:

Cursor notesCursor = mDbHelper.fetchAllNotes();
        if (notesCursor != null) {
            startManagingCursor(notesCursor);
        } else {
           System.err.println("Cursor is null");
        }   


    double x, y;

    try {
        notesCursor.moveToFirst();
        x = notesCursor.getDouble(notesCursor.getColumnIndexOrThrow(NotesDbAdapter.KEY_ROWID));
    } catch (NumberFormatException e) {
      e.printStackTrace();
      return;
    }
    try {
        y = notesCursor.getDouble(notesCursor.getColumnIndexOrThrow(NotesDbAdapter.KEY_RESULT));
    } catch (NumberFormatException e) {
      e.printStackTrace();
      return;
    }
    mCurrentSeries.add(x, y);
    if (mChartView != null) {
      mChartView.repaint();
    }

但它仍然没有用,我错过了什么吗?谢谢你的帮助

完整代码:

package chart.android.research;

import java.io.File;
import java.io.FileOutputStream;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.chartdemo.demo.R;
import org.achartengine.model.SeriesSelection;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import org.achartengine.tools.PanListener;
import org.achartengine.tools.ZoomEvent;
import org.achartengine.tools.ZoomListener;

import android.app.Activity;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

public class XYChart extends Activity {
  public static final String TYPE = "type";

  private XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset();

  private XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();

  private XYSeries mCurrentSeries;

  private XYSeriesRenderer mCurrentRenderer;

  private String mDateFormat;

  private Button mAdd;

  private EditText mX;

  private EditText mY;

  private GraphicalView mChartView;

  private int index = 0;

  private NotesDbAdapter mDbHelper;

  @Override
  protected void onRestoreInstanceState(Bundle savedState) {
    super.onRestoreInstanceState(savedState);
    mDataset = (XYMultipleSeriesDataset) savedState.getSerializable("dataset");
    mRenderer = (XYMultipleSeriesRenderer) savedState.getSerializable("renderer");
    mCurrentSeries = (XYSeries) savedState.getSerializable("current_series");
    mCurrentRenderer = (XYSeriesRenderer) savedState.getSerializable("current_renderer");
    mDateFormat = savedState.getString("date_format");
  }

  @Override
  protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putSerializable("dataset", mDataset);
    outState.putSerializable("renderer", mRenderer);
    outState.putSerializable("current_series", mCurrentSeries);
    outState.putSerializable("current_renderer", mCurrentRenderer);
    outState.putString("date_format", mDateFormat);
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.xy_chart);
    mX = (EditText) findViewById(R.id.xValue);
    mY = (EditText) findViewById(R.id.yValue);
    mRenderer.setApplyBackgroundColor(true);
    mRenderer.setBackgroundColor(Color.argb(100, 50, 50, 50));
    mRenderer.setAxisTitleTextSize(16);
    mRenderer.setChartTitleTextSize(20);
    mRenderer.setLabelsTextSize(15);
    mRenderer.setLegendTextSize(15);
    mRenderer.setMargins(new int[] { 20, 30, 15, 0 });
    mRenderer.setZoomButtonsVisible(true);
    mRenderer.setPointSize(10);

    mAdd = (Button) findViewById(R.id.add);
        XYSeries series = new XYSeries("Kenaikan Gula Darah");
        mDataset.addSeries(series);
        mCurrentSeries = series;
        XYSeriesRenderer renderer = new XYSeriesRenderer();
        mRenderer.addSeriesRenderer(renderer);
        renderer.setPointStyle(PointStyle.CIRCLE);
        renderer.setFillPoints(true);
        mCurrentRenderer = renderer;
        setSeriesEnabled(true);

        Cursor notesCursor = mDbHelper.fetchAllNotes();
        if (notesCursor != null) {
            startManagingCursor(notesCursor);
        } else {
           System.err.println("Cursor is null");
        }      
        double x, y;

        try {
            notesCursor.moveToFirst();
            x = notesCursor.getDouble(notesCursor.getColumnIndexOrThrow(NotesDbAdapter.KEY_ROWID));
        } catch (NumberFormatException e) {
          e.printStackTrace();
          return;
        }
        try {
            y = notesCursor.getDouble(notesCursor.getColumnIndexOrThrow(NotesDbAdapter.KEY_RESULT));
        } catch (NumberFormatException e) {
          e.printStackTrace();
          return;
        }
        mCurrentSeries.add(x, y);
        if (mChartView != null) {
          mChartView.repaint();
        }

  }

  @Override
  protected void onResume() {
    super.onResume();
    if (mChartView == null) {
      LinearLayout layout = (LinearLayout) findViewById(R.id.chart);
      mChartView = ChartFactory.getLineChartView(this, mDataset, mRenderer);
      mRenderer.setClickEnabled(true);
      mRenderer.setSelectableBuffer(100);
      mChartView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          SeriesSelection seriesSelection = mChartView.getCurrentSeriesAndPoint();
          double[] xy = mChartView.toRealPoint(0);
          if (seriesSelection == null) {
            Toast.makeText(XYChartBuilder.this, "No chart element was clicked", Toast.LENGTH_SHORT)
                .show();
          } else {
            Toast.makeText(
                XYChartBuilder.this,
                "Chart element in series index " + seriesSelection.getSeriesIndex()
                    + " data point index " + seriesSelection.getPointIndex() + " was clicked"
                    + " closest point value X=" + seriesSelection.getXValue() + ", Y=" + seriesSelection.getValue()
                    + " clicked point value X=" + (float) xy[0] + ", Y=" + (float) xy[1], Toast.LENGTH_SHORT).show();
          }
        }
      });
      mChartView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
          SeriesSelection seriesSelection = mChartView.getCurrentSeriesAndPoint();
          if (seriesSelection == null) {
            Toast.makeText(XYChartBuilder.this, "No chart element was long pressed",
                Toast.LENGTH_SHORT);
            return false; // no chart element was long pressed, so let something
            // else handle the event
          } else {
            Toast.makeText(XYChartBuilder.this, "Chart element in series index "
                + seriesSelection.getSeriesIndex() + " data point index "
                + seriesSelection.getPointIndex() + " was long pressed", Toast.LENGTH_SHORT);
            return true; // the element was long pressed - the event has been
            // handled
          }
        }
      });
      mChartView.addZoomListener(new ZoomListener() {
        public void zoomApplied(ZoomEvent e) {
          String type = "out";
          if (e.isZoomIn()) {
            type = "in";
          }
          System.out.println("Zoom " + type + " rate " + e.getZoomRate());
        }

        public void zoomReset() {
          System.out.println("Reset");
        }
      }, true, true);
      mChartView.addPanListener(new PanListener() {
        public void panApplied() {
          System.out.println("New X range=[" + mRenderer.getXAxisMin() + ", " + mRenderer.getXAxisMax()
              + "], Y range=[" + mRenderer.getYAxisMax() + ", " + mRenderer.getYAxisMax() + "]");
        }
      });
      layout.addView(mChartView, new LayoutParams(LayoutParams.FILL_PARENT,
          LayoutParams.FILL_PARENT));
      boolean enabled = mDataset.getSeriesCount() > 0;
      setSeriesEnabled(enabled);
    } else {
      mChartView.repaint();
    }
  }

  private void setSeriesEnabled(boolean enabled) {
    mX.setEnabled(enabled);
    mY.setEnabled(enabled);
    mAdd.setEnabled(enabled);
  }
}

1 个答案:

答案 0 :(得分:0)

在从Cursor获取任何值之前,您错过了moveToFirst()方法。 添加e.printStackTrace();所有的阻止块(而不是TODO),你会看到真正的原因。

顺便说一下,方法startManagingCursor()已弃用。

好的,现在试试吧 更改此代码

Cursor notesCursor = mDbHelper.fetchAllNotes();
startManagingCursor(notesCursor);

要 Cursor notesCursor = mDbHelper.fetchAllNotes();

if (notesCursor != null) {
    startManagingCursor(notesCursor);
} else {
   System.err.println("Cursor is null");
}

并确保mDbHelper也不为空。