在设备上使用 sqlite 数据库时出现 NullPointerException,但模拟器工作正常

NullPointerException when using sqlite database on device but emulator works fine

提问人:Moustafa Asaad 提问时间:11/23/2019 最后编辑:JacksMoustafa Asaad 更新时间:11/24/2019 访问量:107

问:

我正在尝试使用预定义的数据库 SQLite 开发一个字典应用程序,这是它崩溃时的日志。

2019-11-23 08:26:40.432 16132-16349/offline.english.dictionary W/CrashlyticsCore: Cannot send reports. Settings are unavailable.
2019-11-23 08:26:40.433 16132-16132/offline.english.dictionary E/UncaughtException: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.database.CursorWindow.getNumRows()' on a null object reference
        at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:162)
        at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:149)
        at android.support.v4.widget.CursorAdapter.getCount(CursorAdapter.java:217)
        at android.widget.AutoCompleteTextView$PopupDataSetObserver$1.run(AutoCompleteTextView.java:1416)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7073)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)

这是SQLITECURSOR.java,它显示错误行:162

catch (RuntimeException ex) {
        closeWindow();
        throw ex;
    }
}

根据崩溃日志,这是SQLiteCursor.java:149

 mCount = mQuery.fillWindow(mWindow, requiredPos, requiredPos, true);

根据崩溃日志,这是 CursorAdapter.java:217

private final Runnable updateRunnable = new Runnable() {
        @Override
        public void run() {
            final AutoCompleteTextView textView = mViewReference.get();
            if (textView == null) {
                return;
            }
            final ListAdapter adapter = textView.mAdapter;
            if (adapter == null) {
                return;
            }
            textView.updateDropDownForFilter(adapter.getCount());
        }
    };
  }
}

这是处理程序.java:873,99 根据崩溃日志

 private static void handleCallback(Message message) {
    message.callback.run();
}
public void dispatchMessage(Message msg) {
    if (msg.callback != null) {
        handleCallback(msg);
    } else {
        if (mCallback != null) {
            if (mCallback.handleMessage(msg)) {
                return;
            }
        }
        handleMessage(msg);
    }
}

这是Looper.java:214,根据崩溃报告。

if (logSlowDispatch) {
            showSlowLog(slowDispatchThresholdMs, dispatchStart, dispatchEnd, "dispatch", msg);
        }

        if (logging != null) {
            logging.println("<<<<< Finished to " + msg.target + " " + msg.callback);
        }
java android sqlite nullpointerexception

评论

0赞 Kaushik Burkule 11/23/2019
一个最小的可重复的例子将使它易于消化。从大量代码开始,与问题无关的东西并不那么有趣
0赞 Arunachalam k 11/23/2019
请看这个问题,这可能会对你有所帮助 stackoverflow.com/a/32333895/10239870
0赞 Moustafa Asaad 11/23/2019
对不起打扰你,但我添加了这段代码,因为它不仅在我的 android 设备上有效,也可能是因为它植根但在其他设备上有效,所以我发现 stackoverflow 是我唯一寻求帮助的地方,谢谢
0赞 blackapps 11/23/2019
我们希望您找到 null 指针,以便防止该 null 指针异常。然后我们可以进一步讨论。

答: 暂无答案