我已经编写了这段代码,通过使用“for”循环在数据库中插入数据,我也希望看到数据库的数据,但是应用程序崩溃了。我找不到它的问题请帮帮我... < / p>
package com.smshah.database;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView t1=(TextView)findViewById(R.id.textView1);
TextView t2=(TextView)findViewById(R.id.textView2);
TextView t3=(TextView)findViewById(R.id.textView3);
SQLiteDatabase db = openOrCreateDatabase("mapdb", MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS maptb (rssi INT(4),xval INT(4),yval INT(4));");
int x=0;
int y=0;
int r=0;
for (int i = 0; i <= 100; i++)
{
db.execSQL("INSERT INTO maptb VALUES("+r+","+x+","+y+");");
x = x + 1;
r = r - 1;
}
Cursor resultSet = db.rawQuery("Select * from maptb",null);
resultSet.moveToFirst();
int rssi = resultSet.getInt(resultSet.getColumnIndex("rssi"));
int xval = resultSet.getInt(resultSet.getColumnIndex("xval"));
int yval = resultSet.getInt(resultSet.getColumnIndex("yval"));
t1.setText(rssi);
t2.setText(xval);
t3.setText(yval);
db.close();
}
}
logcat的
08-04 03:12:17.842: W/ResourceType(13662): No package identifier when getting value for resource number 0x00000000
08-04 03:12:17.852: D/AndroidRuntime(13662): Shutting down VM
08-04 03:12:17.852: W/dalvikvm(13662): threadid=1: thread exiting with uncaught exception (group=0x40c0ea68)
08-04 03:12:17.882: E/AndroidRuntime(13662): FATAL EXCEPTION: main
08-04 03:12:17.882: E/AndroidRuntime(13662): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.smshah.database/com.smshah.database.MainActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x0
08-04 03:12:17.882: E/AndroidRuntime(13662): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
08-04 03:12:17.882: E/AndroidRuntime(13662): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
08-04 03:12:17.882: E/AndroidRuntime(13662): at android.app.ActivityThread.access$600(ActivityThread.java:128)
08-04 03:12:17.882: E/AndroidRuntime(13662): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
08-04 03:12:17.882: E/AndroidRuntime(13662): at android.os.Handler.dispatchMessage(Handler.java:99)
08-04 03:12:17.882: E/AndroidRuntime(13662): at android.os.Looper.loop(Looper.java:137)
08-04 03:12:17.882: E/AndroidRuntime(13662): at android.app.ActivityThread.main(ActivityThread.java:4517)
08-04 03:12:17.882: E/AndroidRuntime(13662): at java.lang.reflect.Method.invokeNative(Native Method)
08-04 03:12:17.882: E/AndroidRuntime(13662): at java.lang.reflect.Method.invoke(Method.java:511)
08-04 03:12:17.882: E/AndroidRuntime(13662): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
08-04 03:12:17.882: E/AndroidRuntime(13662): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
08-04 03:12:17.882: E/AndroidRuntime(13662): at dalvik.system.NativeStart.main(Native Method)
08-04 03:12:17.882: E/AndroidRuntime(13662): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x0
08-04 03:12:17.882: E/AndroidRuntime(13662): at android.content.res.Resources.getText(Resources.java:260)
08-04 03:12:17.882: E/AndroidRuntime(13662): at android.widget.TextView.setText(TextView.java:3680)
08-04 03:12:17.882: E/AndroidRuntime(13662): at com.smshah.database.MainActivity.onCreate(MainActivity.java:47)
08-04 03:12:17.882: E/AndroidRuntime(13662): at android.app.Activity.performCreate(Activity.java:4470)
08-04 03:12:17.882: E/AndroidRuntime(13662): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
08-04 03:12:17.882: E/AndroidRuntime(13662): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
08-04 03:12:17.882: E/AndroidRuntime(13662):
答案 0 :(得分:2)
您的崩溃似乎与数据库没有任何关系。它说没有找到资源异常。你能指出MainActivity第47行的内容吗?您应该设置TextView的内容。
修改强>
您要推送到TextView的是int,文本视图假定这些值是资源标识符而不是字符串。因此,请按照以下步骤使TextViews写入字符串而不是资源标识符。
t1.setText(rssi + "");
t2.setText(xval + "");
t3.setText(yval + "");
答案 1 :(得分:-1)
尝试创建查询功能,不同于 rawQuery ,它更容易阅读。
//return false if something goes wrong public boolean add(int x, int y, int z) { database = connection.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(SQLConnection.COLUMN_X, x); values.put(SQLConnection.COLUMN_Y, y); values.put(SQLConnection.COLUMN_Z, z); return database.insert(SQLConnection.TABLE_MAPXYZ, null, values) != -1 ? true : false; }
也读过,试着:
public List<MapXYZ> getAll() {
List<MapXYZ> mapList = new ArrayList<MapXYZ>();
database = connection.getReadableDatabase();
Cursor cursor = database.query(SQLConnection.TABLE_MAPXYZ, GET_ALL, null, null, null, null, null, null);
if (cursor == null) //something goes wrong
return null;
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
MapXYZ linemap = new MapXYZ();
linemap.setX(cursor.getInt(0));
linemap.setY(cursor.getInt(1));
linemap.setZ(cursor.getInt(2));
// Adding line to the list
mapList.add(linemap);
} while (cursor.moveToNext());
}
return mapList;
}
一些提示:创建最终变量,不要错过表和列名称...... 有关Android Sqlite query reference
的更多信息,请参阅PS:我没有测试这段代码,我自己使用并重命名变量