Android db错误

时间:2012-03-11 07:40:57

标签: android sqlite

我创建了一个应用程序来连接到现有的sqlite数据库,并显示来自数据库的一些基本信息..但它在我的模拟器中意外关闭,我不确定错误是什么意思。 db只有两个表,Information和android_metadata。

我在项目文件中遇到的唯一错误是在AndroidManifest.xml中。它说:“属性minSdkVersion(8)低于项目目标API级别(10)”其他SO帖子说这可以忽略,所以我没有编辑它(它似乎不是它的问题logcat跟踪。)

这是使用db:中的'states'colunn填充listview的活动:

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import healthy.com.sdp.R;
import healthy.com.sdp.db.DataBaseHelper;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class StateListActivity extends Activity {

    private ListView _listView;
    private List<StateDisease> stateNames = new ArrayList<StateDisease>();
    DataBaseHelper myDbHelper;
    AlertDialog.Builder builder ;
    AlertDialog alert;
    Context _context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.state_list);        

        myDbHelper = new DataBaseHelper(this);

        _listView = (ListView) findViewById(R.id.listState);
        _listView.setAdapter(new SingleTextAdapter(getBaseContext(), R.layout.adapter_single_text, stateNames));

        _listView.setOnItemClickListener(clickListener);
        _context = this;
        try {

            myDbHelper.createDataBase();

        } catch (IOException ioe) {

            throw new Error("Unable to create database");

        }

        try {

            myDbHelper.openDataBase();
            Cursor cursor = myDbHelper.executeRawQuery("Select * from Information");


            if(cursor.moveToFirst())
            {
                while (cursor.isAfterLast() == false) 
                {
                    StateDisease sd = new StateDisease();
                    sd._stateId = cursor.getInt(cursor.getColumnIndex("_id"));
                    sd._percentHeartDisease = cursor.getDouble(cursor.getColumnIndex("HeartDisease"));
                    sd._percentObesity = cursor.getDouble(cursor.getColumnIndex("Obesity"));                    
                    sd._stateName = cursor.getString(cursor.getColumnIndex("States"));                  
                    sd._percentLungDisease = cursor.getDouble(cursor.getColumnIndex("LungDisease"));

                    stateNames.add(sd);
                    cursor.moveToNext();
                }
            }


            myDbHelper.close();

        }catch(SQLException sqle){

            throw sqle;

        }
    }

    private OnItemClickListener clickListener = new OnItemClickListener()
    {

        @Override
        public void onItemClick(AdapterView<?> parent, View v, int pos,
                long id) {      

            Intent intent = new Intent(getBaseContext(), DiseaseSelectionActivity.class); 
            Bundle b = new Bundle();
            b.putInt(Constant.STATE_ID, stateNames.get(pos)._stateId);
            b.putString(Constant.STATE_NAME, stateNames.get(pos)._stateName);
            intent.putExtras(b);
            startActivity(intent);
        }

    };
}

这是logcat跟踪:

03-11 07:14:33.013: I/Database(336): sqlite returned: error code = 1, msg = no such table: Information
03-11 07:14:33.013: W/System.err(336): android.database.sqlite.SQLiteException: no such table: Information: , while compiling: Select * from Information
03-11 07:14:33.022: W/System.err(336):  at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
03-11 07:14:33.022: W/System.err(336):  at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
03-11 07:14:33.022: W/System.err(336):  at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
03-11 07:14:33.022: W/System.err(336):  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
03-11 07:14:33.022: W/System.err(336):  at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:49)
03-11 07:14:33.032: W/System.err(336):  at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
03-11 07:14:33.032: W/System.err(336):  at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1356)
03-11 07:14:33.032: W/System.err(336):  at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1324)
03-11 07:14:33.032: W/System.err(336):  at healthy.com.sdp.db.DataBaseHelper.executeRawQuery(DataBaseHelper.java:149)
03-11 07:14:33.032: W/System.err(336):  at healthy.com.sdp.activity.StateListActivity.onCreate(StateListActivity.java:58)
03-11 07:14:33.032: W/System.err(336):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-11 07:14:33.032: W/System.err(336):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-11 07:14:33.032: W/System.err(336):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-11 07:14:33.032: W/System.err(336):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-11 07:14:33.050: W/System.err(336):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-11 07:14:33.050: W/System.err(336):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-11 07:14:33.052: W/System.err(336):  at android.os.Looper.loop(Looper.java:123)
03-11 07:14:33.052: W/System.err(336):  at android.app.ActivityThread.main(ActivityThread.java:3683)
03-11 07:14:33.052: W/System.err(336):  at java.lang.reflect.Method.invokeNative(Native Method)
03-11 07:14:33.052: W/System.err(336):  at java.lang.reflect.Method.invoke(Method.java:507)
03-11 07:14:33.052: W/System.err(336):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-11 07:14:33.052: W/System.err(336):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-11 07:14:33.052: W/System.err(336):  at dalvik.system.NativeStart.main(Native Method)
03-11 07:14:33.052: D/AndroidRuntime(336): Shutting down VM
03-11 07:14:33.052: W/dalvikvm(336): threadid=1: thread exiting with uncaught exception (group=0x40015560)
03-11 07:14:33.072: E/AndroidRuntime(336): FATAL EXCEPTION: main
03-11 07:14:33.072: E/AndroidRuntime(336): java.lang.RuntimeException: Unable to start activity ComponentInfo{healthy.com.sdp/healthy.com.sdp.activity.StateListActivity}: java.lang.NullPointerException
03-11 07:14:33.072: E/AndroidRuntime(336):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-11 07:14:33.072: E/AndroidRuntime(336):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-11 07:14:33.072: E/AndroidRuntime(336):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-11 07:14:33.072: E/AndroidRuntime(336):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-11 07:14:33.072: E/AndroidRuntime(336):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-11 07:14:33.072: E/AndroidRuntime(336):  at android.os.Looper.loop(Looper.java:123)
03-11 07:14:33.072: E/AndroidRuntime(336):  at android.app.ActivityThread.main(ActivityThread.java:3683)
03-11 07:14:33.072: E/AndroidRuntime(336):  at java.lang.reflect.Method.invokeNative(Native Method)
03-11 07:14:33.072: E/AndroidRuntime(336):  at java.lang.reflect.Method.invoke(Method.java:507)
03-11 07:14:33.072: E/AndroidRuntime(336):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-11 07:14:33.072: E/AndroidRuntime(336):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-11 07:14:33.072: E/AndroidRuntime(336):  at dalvik.system.NativeStart.main(Native Method)
03-11 07:14:33.072: E/AndroidRuntime(336): Caused by: java.lang.NullPointerException
03-11 07:14:33.072: E/AndroidRuntime(336):  at healthy.com.sdp.activity.StateListActivity.onCreate(StateListActivity.java:61)
03-11 07:14:33.072: E/AndroidRuntime(336):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-11 07:14:33.072: E/AndroidRuntime(336):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-11 07:14:33.072: E/AndroidRuntime(336):  ... 11 more
03-11 07:14:37.442: I/Process(336): Sending signal. PID: 336 SIG: 9

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您正在执行声明/查询

    Select * from Information

虽然此表“信息”之前尚未创建。 请参阅此link/tutorial以了解如何以更好的方式使用sqlite db