我已阅读了许多网站和教程,但无法解决我的问题。我有一个应用程序,直到我尝试从我的数据库读取它(它打开它就好了)。我知道它不是正确的做事方式,但我有两个全局字符串,我从主要活动传递到我的DataBaseHelper类(DB_TABLE
和spinnerPipeLengthText
)。当我调用以下代码行来检索特定行中所有列的数据时,我的应用程序崩溃了。
Cursor cursorData = myDataBase.query(CSSTPipeSizingActivity.DB_TABLE, null,
CSSTPipeSizingActivity.spinnerPipeLengthText, null, null, null, null, null);
这是我完整的getData
方法:
public Cursor getData() {
Cursor cursorData = myDataBase.query(CSSTPipeSizingActivity.DB_TABLE, null,
CSSTPipeSizingActivity.spinnerPipeLengthText, null, null, null, null, null);
if (cursorData != null)
cursorData.moveToFirst();
cursorData.moveToFirst();
CSSTPipeSizingActivity.textViewSize15.setText(cursorData.getString(1));
cursorData.moveToNext();
CSSTPipeSizingActivity.textViewSize19.setText(cursorData.getString(1));
cursorData.moveToFirst();
CSSTPipeSizingActivity.textViewSize25.setText(cursorData.getString(1));
cursorData.moveToNext();
CSSTPipeSizingActivity.textViewSize31.setText(cursorData.getString(1));
cursorData.moveToFirst();
CSSTPipeSizingActivity.textViewSize37.setText(cursorData.getString(1));
cursorData.moveToNext();
CSSTPipeSizingActivity.textViewSize46.setText(cursorData.getString(1));
cursorData.moveToNext();
CSSTPipeSizingActivity.textViewSize62.setText(cursorData.getString(1));
cursorData.close();
return cursorData;
} // end method getData
从我的代码中可以看出,我正在尝试获取数据并将其直接输入main.xml
文件中的文本视图。我不知道这是否可能,但这将是我稍后想出来的。现在我需要获取数据。
这是我的logcat:
03-12 19:23:33.587: D/AndroidRuntime(20191): Shutting down VM
03-12 19:23:33.587: W/dalvikvm(20191): threadid=1: thread exiting with uncaught exception (group=0x40015560)
03-12 19:23:33.617: E/AndroidRuntime(20191): FATAL EXCEPTION: main
03-12 19:23:33.617: E/AndroidRuntime(20191): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.skyesmechanical.CSSTPipeSizing/com.skyesmechanical.CSSTPipeSizing.CSSTPipeSizingActivity}: java.lang.NullPointerException
03-12 19:23:33.617: E/AndroidRuntime(20191): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-12 19:23:33.617: E/AndroidRuntime(20191): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-12 19:23:33.617: E/AndroidRuntime(20191): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-12 19:23:33.617: E/AndroidRuntime(20191): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-12 19:23:33.617: E/AndroidRuntime(20191): at android.os.Handler.dispatchMessage(Handler.java:99)
03-12 19:23:33.617: E/AndroidRuntime(20191): at android.os.Looper.loop(Looper.java:123)
03-12 19:23:33.617: E/AndroidRuntime(20191): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-12 19:23:33.617: E/AndroidRuntime(20191): at java.lang.reflect.Method.invokeNative(Native Method)
03-12 19:23:33.617: E/AndroidRuntime(20191): at java.lang.reflect.Method.invoke(Method.java:507)
03-12 19:23:33.617: E/AndroidRuntime(20191): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-12 19:23:33.617: E/AndroidRuntime(20191): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-12 19:23:33.617: E/AndroidRuntime(20191): at dalvik.system.NativeStart.main(Native Method)
03-12 19:23:33.617: E/AndroidRuntime(20191): Caused by: java.lang.NullPointerException
03-12 19:23:33.617: E/AndroidRuntime(20191): at com.skyesmechanical.CSSTPipeSizing.DataBaseHelper.getData(DataBaseHelper.java:173)
03-12 19:23:33.617: E/AndroidRuntime(20191): at com.skyesmechanical.CSSTPipeSizing.CSSTPipeSizingActivity.onCreate(CSSTPipeSizingActivity.java:105)
03-12 19:23:33.617: E/AndroidRuntime(20191): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-12 19:23:33.617: E/AndroidRuntime(20191): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-12 19:23:33.617: E/AndroidRuntime(20191): ... 11 more
感谢您的帮助!
答案 0 :(得分:1)
嗯,你正在关闭光标,然后试图返回它......不会那么好用。关闭光标并不返回任何内容或返回光标。
当你移动第一个,movetonext然后再次移动时,你也会继续迭代前几个条目。
我也相信你的getString需要一些帮助。
以下是我的观点:
titleTxt.setText(eventedit.getString(eventedit
.getColumnIndexOrThrow(AttendanceDB.EVENT_NAME)));
titleTxt
是我之前设置的TextView参考
eventEdit
是我想要从中获取信息(并在之前创建)的光标
AttendanceDB
是我的数据库适配器类
EVENT_NAME
是我在数据库适配器类
修改强>
好的,现在我重新阅读了你的问题,我意识到我正在回答错误的部分。问题是你的查询搞砸了。我认为应该更像这样:
Cursor cursorData = myDataBase.query(CSSTPipeSizingActivity.DB_TABLE, null,
COLUMN_ID + "=" + CSSTPipeSizingActivity.spinnerPipeLengthText, null, null, null, null, null);
您需要告诉查询要搜索所需信息的列(CSSTPipeSizingActivity.spinnerPipeLengthText)。因此,将COLUMN_ID替换为您的列名或变量,您应该好好去。
您真的应该查看信息here,尤其是各种查询方法及其所需参数。
答案 1 :(得分:0)
你可以从你的logcat看到错误是第173行的NullPointerException。在你的代码中没有行号,很难确切知道这个问题是什么,但我的猜测是第173行是cursorData。 moveToFirst()不在if语句中。
考虑cursorData为null的情况 -
检查cursorData是否为null,如果是这种情况,则调用moveToFirst()。但是你在下一行调用moveToFirst();无论cursorData是否为null,都会发生这种情况。
一个常见的设计模式是getData简单地返回游标,以及调用方法来处理提取。这看起来像是:
{ ... if(cursorData!= null) cursorData.moveToFirst(); return cursorData; }
我希望这有帮助!
答案 2 :(得分:0)
首先,您认为可以在IF语句中使用Cursor放置所有代码:
if (cursorData != null){
cursorData.moveToFirst();
CSSTPipeSizingActivity.textViewSize15.setText(cursorData.getString(1));
cursorData.moveToNext();
CSSTPipeSizingActivity.textViewSize19.setText(cursorData.getString(1));
cursorData.moveToFirst();
CSSTPipeSizingActivity.textViewSize25.setText(cursorData.getString(1));
cursorData.moveToNext();
CSSTPipeSizingActivity.textViewSize31.setText(cursorData.getString(1));
cursorData.moveToFirst();
CSSTPipeSizingActivity.textViewSize37.setText(cursorData.getString(1));
cursorData.moveToNext();
CSSTPipeSizingActivity.textViewSize46.setText(cursorData.getString(1));
cursorData.moveToNext();
CSSTPipeSizingActivity.textViewSize62.setText(cursorData.getString(1));
cursorData.close();
}
其次,您可以检查以下代码b / c,然后移动到下一次:
cursorData.moveToNext();
CSSTPipeSizingActivity.textViewSize46.setText(cursorData.getString(1));
cursorData.moveToNext();
CSSTPipeSizingActivity.textViewSize62.setText(cursorData.getString(1));
最后,对所有setText方法使用带有String转换的setText:
CSSTPipeSizingActivity.textViewSize15.setText("" + cursorData.getString(1));