从出错的Andr​​oid SQLite的检索数据 [英] Error retrieving data from Sqlite Android

查看:146
本文介绍了从出错的Andr​​oid SQLite的检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个强制关闭,当我试图从源码数据库检索数据,并将其设置为文本的意见。这里的获取方法:

 公开的ArrayList<对象>的getRow(){        ArrayList的<对象>名单=新的ArrayList<对象>();
        光标光标;
        的String [] =列{PRIMARY_KEY,COLUMN_NAME,COLUMN_EMAIL,COLUMN_PASSWORD,COLUMN_PASSWORD};        光标= sp.query(TABLE_NAME,列,NULL,NULL,NULL,NULL,NULL);
                INT i_id = cursor.getColumnIndex(PRIMARY_KEY);
        INT i_name = cursor.getColumnIndex(列);
        INT i_contact = cursor.getColumnIndex(COLUMN_CONTACT);
        INT i_email = cursor.getColumnIndex(COLUMN_EMAIL);
        INT i_pass = cursor.getColumnIndex(COLUMN_PASSWORD);
        为(cursor.moveToFirst();!cursor.isAfterLast(); cursor.moveToNext())
        {                list.add(cursor.getLong(i_id));
                list.add(cursor.getString(i_name));
                list.add(cursor.getString(i_contact));
                list.add(cursor.getString(i_email));
                list.add(cursor.getString(i_pass));        }
        返回列表;
    }
}

和这里是code到的setText,请帮帮我,我想千方百计,表不获取创建和插入的值,但取不工作。

 私人无效的GetTable(){info.open();
    尝试{    ArrayList的<对象>清单= info.getRow();    AccBalanceTV.setText((字符串)list.get(1));
    AccEmailTV.setText((字符串)list.get(2));
    AccContactTV.setText((字符串)list.get(3));
    AccVehTV.setText((字符串)list.get(4));
    }
    赶上(例外五)
    {
        Toast.makeText(getApplicationContext(),将String.valueOf(E),Toast.LENGTH_LONG).show();
    }
    最后{
        info.close();
    }
    }

logcat的:

  03-09 21:09:46.180:D / dalvikvm(10971):GC_FOR_ALLOC释放57K,免费12245K / 12931K 6%,暂停10毫秒,11毫秒总
03-09 21:09:46.185:I / dalvikvm堆(10971):成长堆(frag的情况下),以16.032MB为3686416字节分配
03-09 21:09:46.195:D / dalvikvm(10971):GC_CONCURRENT释放1K,5%的游离15844K / 16583K,暂停1毫秒+ 1毫秒,11毫秒总
03-09 21:09:46.250:D / dalvikvm(10971):GC_FOR_ALLOC释放< 1K,免费16873K / 17671K 5%,暂停9ms的,9ms的总
03-09 21:09:46.250:I / dalvikvm堆(10971):成长堆(frag的情况下),以18.815MB为1865972字节分配
03-09 21:09:46.270:D / dalvikvm(10971):GC_CONCURRENT释放< 1K,免费18695K / 19527K 5%,11毫秒暂停2MS +,总21ms
03-09 21:09:46.315:D / libEGL(10971):加载/system/lib/egl/libEGL_mali.so
03-09 21:09:46.320:D / libEGL(10971):加载/system/lib/egl/libGLESv1_CM_mali.so
03-09 21:09:46.320:D / libEGL(10971):加载/system/lib/egl/libGLESv2_mali.so
03-09 21:09:46.325:D /(10971):设备驱动程序API匹配
03-09 21:09:46.325:D /(10971):设备驱动程序API版本:10
03-09 21:09:46.325:D /(10971):用户空间API版本:10
03-09 21:09:46.325:D /(10971):马里:版本=的Linux r2p4-02rel0 BUILD_DATE =周二10月16日15时37分十三秒KST 2012
03-09 21:09:46.350:D / OpenGLRenderer(10971):启用调试模式0
03-09 21:09:48.205:D / dalvikvm(10971):GC_FOR_ALLOC释放1041K,10%免费17741K / 19527K,暂停9ms的,9ms的总
03-09 21:09:48.210:I / dalvikvm堆(10971):成长堆(frag的情况下),以21.545MB为3840016字节分配
03-09 21:09:48.220:D / dalvikvm(10971):GC_CONCURRENT释放2K,8%的免费21489K / 23303K,暂停1毫秒+ 1毫秒,12毫秒总
03-09 21:09:48.415:E / SpannableStringBuilder(10971):SPAN_EXCLUSIVE_EXCLUSIVE跨度不能有长度为零
03-09 21:09:48.415:E / SpannableStringBuilder(10971):SPAN_EXCLUSIVE_EXCLUSIVE跨度不能有长度为零
03-09 21:09:56.660:D / AbsListView(10971):获取MotionRecognitionManager
03-09 21:09:57.990:D / GestureDetector(10971):表面触摸事件] mSweepDown假,mLRSDCnt:-1 mTouchCnt:3 mFalseSizeCnt:0
03-09 21:09:58.135:D / AndroidRuntime(10971):关闭VM
03-09 21:09:58.135:W / dalvikvm(10971):主题ID = 1:螺纹未捕获的异常(组= 0x411cb2a0)退出
03-09 21:09:58.140:E / AndroidRuntime(10971):致命异常:主要
03-09 21:09:58.140:E / AndroidRuntime(10971):了java.lang.RuntimeException:无法实例活动ComponentInfo {com.miniproject.parkme / com.miniproject.parkme.Account}:显示java.lang.NullPointerException
03-09 21:09:58.140:E / AndroidRuntime(10971):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2024)
03-09 21:09:58.140:E / AndroidRuntime(10971):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
03-09 21:09:58.140:E / AndroidRuntime(10971):在android.app.ActivityThread.access $ 600(ActivityThread.java:140)
03-09 21:09:58.140:E / AndroidRuntime(10971):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1227)
03-09 21:09:58.140:E / AndroidRuntime(10971):在android.os.Handler.dispatchMessage(Handler.java:99)
03-09 21:09:58.140:E / AndroidRuntime(10971):在android.os.Looper.loop(Looper.java:137)
03-09 21:09:58.140:E / AndroidRuntime(10971):在android.app.ActivityThread.main(ActivityThread.java:4898)
03-09 21:09:58.140:E / AndroidRuntime(10971):在java.lang.reflect.Method.invokeNative(本机方法)
03-09 21:09:58.140:E / AndroidRuntime(10971):在java.lang.reflect.Method.invoke(Method.java:511)
03-09 21:09:58.140:E / AndroidRuntime(10971):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1008)
03-09 21:09:58.140:E / AndroidRuntime(10971):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
03-09 21:09:58.140:E / AndroidRuntime(10971):在dalvik.system.NativeStart.main(本机方法)
03-09 21:09:58.140:E / AndroidRuntime(10971):致:显示java.lang.NullPointerException
03-09 21:09:58.140:E / AndroidRuntime(10971):在android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:228)
03-09 21:09:58.140:E / AndroidRuntime(10971):在android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
03-09 21:09:58.140:E / AndroidRuntime(10971):在android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
。03-09 21:09:58.140:E / AndroidRuntime(10971):在com.miniproject.parkme.databaseManager<&初始化GT;(databaseManager.java:40)
。03-09 21:09:58.140:E / AndroidRuntime(10971):在com.miniproject.parkme.Account<&初始化GT;(Account.java:12)
03-09 21:09:58.140:E / AndroidRuntime(10971):在java.lang.Class.newInstanceImpl(本机方法)
03-09 21:09:58.140:E / AndroidRuntime(10971):在java.lang.Class.newInstance(Class.java:1319)
03-09 21:09:58.140:E / AndroidRuntime(10971):在android.app.Instrumentation.newActivity(Instrumentation.java:1068)
03-09 21:09:58.140:E / AndroidRuntime(10971):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2015)
03-09 21:09:58.140:E / AndroidRuntime(10971):... 11个
03-09 21:10:07.220:I /流程(10971):发送信号。 PID:10971 SIG:9


解决方案

从您的LogCat中:

 产生的原因:显示java.lang.NullPointerException
    ...
    在com.miniproject.parkme.databaseManager<&初始化GT;(databaseManager.java:40)
    在com.miniproject.parkme.Account<&初始化GT;(Account.java:12)

看起来要初始化信息具有无效语境。结果
你不能做到这一点,它会导致一个NullPointerException异常后:<击>

 私人DBHelper信息=新DBHelper(本);


你必须等到活动的背景下是有效的:

 私人DBHelper信息;保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    信息=新DBHelper(本);

I'm getting a force close when I try to retrieve data from sqlite db and set it to text views. here's the fetch method:

public ArrayList<Object> getRow() {

        ArrayList<Object> list= new ArrayList<Object>();
        Cursor cursor;
        String[] columns={PRIMARY_KEY,COLUMN_NAME,COLUMN_EMAIL,COLUMN_PASSWORD,COLUMN_PASSWORD};



        cursor= sp.query(TABLE_NAME,columns,null,null,null,null,null);
                int i_id=cursor.getColumnIndex(PRIMARY_KEY);
        int i_name=cursor.getColumnIndex(COLUMN_NAME);
        int i_contact=cursor.getColumnIndex(COLUMN_CONTACT);
        int i_email=cursor.getColumnIndex(COLUMN_EMAIL);
        int i_pass=cursor.getColumnIndex(COLUMN_PASSWORD);


        for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext())
        {

                list.add(cursor.getLong(i_id));
                list.add(cursor.getString(i_name));
                list.add(cursor.getString(i_contact));
                list.add(cursor.getString(i_email));
                list.add(cursor.getString(i_pass));

        }   


        return list;
    }


}

and here is the code to settext, please help me, I tried everything possible, the table does get created and the values are inserted, but fetching doesnt work.

private void getTable() {



info.open();
    try{

    ArrayList<Object> list= info.getRow();

    AccBalanceTV.setText((String)list.get(1));
    AccEmailTV.setText((String)list.get(2));
    AccContactTV.setText( (String)list.get(3));
    AccVehTV.setText((String)list.get(4));
    }
    catch(Exception e)
    {
        Toast.makeText(getApplicationContext(), String.valueOf(e), Toast.LENGTH_LONG).show();
    }
    finally{
        info.close();
    }
    }

Logcat:

03-09 21:09:46.180: D/dalvikvm(10971): GC_FOR_ALLOC freed 57K, 6% free 12245K/12931K, paused 10ms, total 11ms
03-09 21:09:46.185: I/dalvikvm-heap(10971): Grow heap (frag case) to 16.032MB for 3686416-byte allocation
03-09 21:09:46.195: D/dalvikvm(10971): GC_CONCURRENT freed 1K, 5% free 15844K/16583K, paused 1ms+1ms, total 11ms
03-09 21:09:46.250: D/dalvikvm(10971): GC_FOR_ALLOC freed <1K, 5% free 16873K/17671K, paused 9ms, total 9ms
03-09 21:09:46.250: I/dalvikvm-heap(10971): Grow heap (frag case) to 18.815MB for 1865972-byte allocation
03-09 21:09:46.270: D/dalvikvm(10971): GC_CONCURRENT freed <1K, 5% free 18695K/19527K, paused 11ms+2ms, total 21ms
03-09 21:09:46.315: D/libEGL(10971): loaded /system/lib/egl/libEGL_mali.so
03-09 21:09:46.320: D/libEGL(10971): loaded /system/lib/egl/libGLESv1_CM_mali.so
03-09 21:09:46.320: D/libEGL(10971): loaded /system/lib/egl/libGLESv2_mali.so
03-09 21:09:46.325: D/(10971): Device driver API match
03-09 21:09:46.325: D/(10971): Device driver API version: 10
03-09 21:09:46.325: D/(10971): User space API version: 10 
03-09 21:09:46.325: D/(10971): mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Tue Oct 16 15:37:13 KST 2012 
03-09 21:09:46.350: D/OpenGLRenderer(10971): Enabling debug mode 0
03-09 21:09:48.205: D/dalvikvm(10971): GC_FOR_ALLOC freed 1041K, 10% free 17741K/19527K, paused 9ms, total 9ms
03-09 21:09:48.210: I/dalvikvm-heap(10971): Grow heap (frag case) to 21.545MB for 3840016-byte allocation
03-09 21:09:48.220: D/dalvikvm(10971): GC_CONCURRENT freed 2K, 8% free 21489K/23303K, paused 1ms+1ms, total 12ms
03-09 21:09:48.415: E/SpannableStringBuilder(10971): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
03-09 21:09:48.415: E/SpannableStringBuilder(10971): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
03-09 21:09:56.660: D/AbsListView(10971): Get MotionRecognitionManager
03-09 21:09:57.990: D/GestureDetector(10971): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 3 mFalseSizeCnt:0
03-09 21:09:58.135: D/AndroidRuntime(10971): Shutting down VM
03-09 21:09:58.135: W/dalvikvm(10971): threadid=1: thread exiting with uncaught exception (group=0x411cb2a0)
03-09 21:09:58.140: E/AndroidRuntime(10971): FATAL EXCEPTION: main
03-09 21:09:58.140: E/AndroidRuntime(10971): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.miniproject.parkme/com.miniproject.parkme.Account}: java.lang.NullPointerException
03-09 21:09:58.140: E/AndroidRuntime(10971):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2024)
03-09 21:09:58.140: E/AndroidRuntime(10971):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
03-09 21:09:58.140: E/AndroidRuntime(10971):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
03-09 21:09:58.140: E/AndroidRuntime(10971):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
03-09 21:09:58.140: E/AndroidRuntime(10971):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-09 21:09:58.140: E/AndroidRuntime(10971):    at android.os.Looper.loop(Looper.java:137)
03-09 21:09:58.140: E/AndroidRuntime(10971):    at android.app.ActivityThread.main(ActivityThread.java:4898)
03-09 21:09:58.140: E/AndroidRuntime(10971):    at java.lang.reflect.Method.invokeNative(Native Method)
03-09 21:09:58.140: E/AndroidRuntime(10971):    at java.lang.reflect.Method.invoke(Method.java:511)
03-09 21:09:58.140: E/AndroidRuntime(10971):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
03-09 21:09:58.140: E/AndroidRuntime(10971):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
03-09 21:09:58.140: E/AndroidRuntime(10971):    at dalvik.system.NativeStart.main(Native Method)
03-09 21:09:58.140: E/AndroidRuntime(10971): Caused by: java.lang.NullPointerException
03-09 21:09:58.140: E/AndroidRuntime(10971):    at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:228)
03-09 21:09:58.140: E/AndroidRuntime(10971):    at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
03-09 21:09:58.140: E/AndroidRuntime(10971):    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
03-09 21:09:58.140: E/AndroidRuntime(10971):    at com.miniproject.parkme.databaseManager.<init>(databaseManager.java:40)
03-09 21:09:58.140: E/AndroidRuntime(10971):    at com.miniproject.parkme.Account.<init>(Account.java:12)
03-09 21:09:58.140: E/AndroidRuntime(10971):    at java.lang.Class.newInstanceImpl(Native Method)
03-09 21:09:58.140: E/AndroidRuntime(10971):    at java.lang.Class.newInstance(Class.java:1319)
03-09 21:09:58.140: E/AndroidRuntime(10971):    at android.app.Instrumentation.newActivity(Instrumentation.java:1068)
03-09 21:09:58.140: E/AndroidRuntime(10971):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2015)
03-09 21:09:58.140: E/AndroidRuntime(10971):    ... 11 more
03-09 21:10:07.220: I/Process(10971): Sending signal. PID: 10971 SIG: 9

解决方案

From your LogCat:

Caused by: java.lang.NullPointerException
    ...
    at com.miniproject.parkme.databaseManager.<init>(databaseManager.java:40)
    at com.miniproject.parkme.Account.<init>(Account.java:12)

It looks like you are initializing info with an invalid Context.
You cannot do this, it will cause a NullPointerException later:

private DBHelper info = new DBHelper(this);

You must wait until the Activity's Context is valid:

private DBHelper info;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    info = new DBHelper(this);

这篇关于从出错的Andr​​oid SQLite的检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆