【问题标题】:Function in SQLite connection class causing Runtime ExceptionSQLite 连接类中的函数导致运行时异常
【发布时间】:2017-12-02 05:34:19
【问题描述】:

我有 DataBase Table,其中我有 COULMNS KEY_ID(INT)EMAIL(TEXT)PASSWORD(TEXT)FIRST_NAME(TEXT)LAST_NAME(TEXT)

我在 SqliteConnection 类中有一个函数:

public String Well(String el,String pl)
{
    SQLiteDatabase pox =this.getReadableDatabase();
    String qr="SELECT "+FIRST_NAME+" FROM "+TABLE_NAME+" WHERE "+EMAIL+" = "+el+" AND "+PASSWORD+" = "+pl;
    Cursor cur =pox.rawQuery(qr,null);
    String itemname =  cur.getString(cur.getColumnIndex(FIRST_NAME));
    pox.close();
    cur.close();
    return  itemname;

}

在上述函数中,我传递了EMAIL as e1PASSWORD as p1 并尝试获取与EMAIL and PASSWORD. 匹配的用户的FIRST_NAME
当我在任何其他活动中调用此函数时,它会导致java.lang.NullPointerException 异常。这个功能有问题吗?

LogCat

12-02 11:02:36.943 2368-2368/? W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/graphics/drawable/Icon;)
12-02 11:02:36.943 2368-2368/? I/dalvikvm: Could not find method android.widget.ImageView.setImageIcon, referenced from method android.support.v7.widget.AppCompatImageView.setImageIcon
12-02 11:02:36.943 2368-2368/? W/dalvikvm: VFY: unable to resolve virtual method 16525: Landroid/widget/ImageView;.setImageIcon (Landroid/graphics/drawable/Icon;)V
12-02 11:02:36.943 2368-2368/? D/dalvikvm: VFY: replacing opcode 0x6f at 0x0000
12-02 11:02:37.073 1559-1574/system_process W/ActivityManager: Launch timeout has expired, giving up wake lock!
12-02 11:02:38.343 1670-1681/com.android.inputmethod.latin W/Binder: Caught a RuntimeException from the binder stub implementation.
                                                                     java.lang.NullPointerException
                                                                         at android.inputmethodservice.IInputMethodWrapper.setSessionEnabled(IInputMethodWrapper.java:280)
                                                                         at com.android.internal.view.IInputMethod$Stub.onTransact(IInputMethod.java:129)
                                                                         at android.os.Binder.execTransact(Binder.java:404)
                                                                         at dalvik.system.NativeStart.run(Native Method)
12-02 11:02:38.343 1559-1713/system_process W/InputMethodManagerService: Got RemoteException sending setActive(false) notification to pid 2323 uid 10055
12-02 11:02:38.353 2368-2368/com.example.sony.sqloperations I/Choreographer: Skipped 83 frames!  The application may be doing too much work on its main thread.
12-02 11:02:40.533 1559-1573/system_process I/ActivityManager: Displayed com.example.sony.sqloperations/.MainActivity: +4s448ms (total +13s453ms)
12-02 11:03:00.013 1616-1616/com.android.systemui D/EGL_emulation: eglMakeCurrent: 0xb8f0e180: ver 2 0
12-02 11:03:00.093 1616-1616/com.android.systemui D/dalvikvm: GC_FOR_ALLOC freed 20284K, 67% free 10469K/30824K, paused 50ms, total 68ms

【问题讨论】:

  • 与问题分享您的崩溃日志
  • @Prem Logcat 添加
  • 分享您的 android.widget.ImageView.setImageIcon 代码
  • @IntelliJAmiya 您认为该功能正确吗? ,请告诉我
  • 我看不出它是如何导致您解释的问题的。 Well 方法有一个缺陷,它没有移动光标以指向一行。最初检索时的游标位于第一行之前。要定位,您可以使用if(cur.moveToFirst()) { String itemname = cur.getString(cur.getColumnIndex(FIRST_NAME));}

标签: android sqlite


【解决方案1】:

首先,在 SELECT 语句中使用 SINGLE 引号。

  "SELECT  _FIRST_NAME FROM " + TABLE_NAME +" WHERE FIELD_NAME='"_FIELD_VALUE "' AND FIELD_NAME_TWO='" _FIELD_VALUE_TWO  "'";

然后

 if (cursor.moveToFirst()) 
 {
     String itemname = cur.getString(0); // Or (cur.getColumnIndex(YOUR_FIELD_NAME));     
 }

【讨论】:

  • 我解决了它,原因是查询格式不正确,尽管我接受了你的答案,因为它与我解决的一样
猜你喜欢
  • 2018-03-24
  • 1970-01-01
  • 2014-03-17
  • 2017-09-03
  • 1970-01-01
  • 1970-01-01
  • 2022-01-12
  • 2019-07-04
  • 2010-09-14
相关资源
最近更新 更多