用于创建数据库的类:
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class sqlite extends Activity {
public void sqlitetry(final SQLiteDatabase sqLiteDB) {
String createSql = "CREATE TABLE" +
"(_id integer primary key autoincrement, " +
"name text not null);";
sqLiteDB.execSQL(createSql);
}
public void sqlinit() {
SQLiteDatabase db = openOrCreateDatabase("MyDatabase",MODE_PRIVATE,null);
db.execSQL("CREATE TABLE id note exist MyTable(username Varchar, password Varchar PRIMARY KEY);");
Cursor c = db.rawQuery("Select * from MyTable", null);
c.moveToFirst();
while(c.isAfterLast() == false) {
}
}
}
当我将此代码添加到我的登录和注册确认类并启动活动时,它会强制关闭:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
sqlite as = new sqlite();
SQLiteDatabase sqLiteDB = null;
as.sqlitetry(sqLiteDB);
as.sqlinit();
在我的设备上安装apk后,这将显示在logcat中:
07-18 09:51:39.875: D/AndroidRuntime(25677): Shutting down VM
07-18 09:51:39.875: W/dalvikvm(25677): threadid=1: thread exiting with uncaught exception (group=0x40018578)
07-18 09:51:39.882: E/AndroidRuntime(25677): FATAL EXCEPTION: main
07-18 09:51:39.882: E/AndroidRuntime(25677): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.login1/com.example.login1.Login}: java.lang.NullPointerException
07-18 09:51:39.882: E/AndroidRuntime(25677): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
07-18 09:51:39.882: E/AndroidRuntime(25677): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
07-18 09:51:39.882: E/AndroidRuntime(25677): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-18 09:51:39.882: E/AndroidRuntime(25677): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
07-18 09:51:39.882: E/AndroidRuntime(25677): at android.os.Handler.dispatchMessage(Handler.java:99)
07-18 09:51:39.882: E/AndroidRuntime(25677): at android.os.Looper.loop(Looper.java:130)
07-18 09:51:39.882: E/AndroidRuntime(25677): at android.app.ActivityThread.main(ActivityThread.java:3687)
07-18 09:51:39.882: E/AndroidRuntime(25677): at java.lang.reflect.Method.invokeNative(Native Method)
07-18 09:51:39.882: E/AndroidRuntime(25677): at java.lang.reflect.Method.invoke(Method.java:507)
07-18 09:51:39.882: E/AndroidRuntime(25677): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
07-18 09:51:39.882: E/AndroidRuntime(25677): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
07-18 09:51:39.882: E/AndroidRuntime(25677): at dalvik.system.NativeStart.main(Native Method)
07-18 09:51:39.882: E/AndroidRuntime(25677): Caused by: java.lang.NullPointerException
07-18 09:51:39.882: E/AndroidRuntime(25677): at com.example.login1.sqlite.sqlitetry(sqlite.java:12)
07-18 09:51:39.882: E/AndroidRuntime(25677): at com.example.login1.Login.onCreate(Login.java:25)
07-18 09:51:39.882: E/AndroidRuntime(25677): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-18 09:51:39.882: E/AndroidRuntime(25677): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
07-18 09:51:39.882: E/AndroidRuntime(25677): ... 11 more
我需要在数据库中注册用户的信息并将其拉出来以便用户登录但强制关闭并且我不知道该怎么做。任何人都可以帮助我吗?
答案 0 :(得分:2)
基于logcat,您可以看到此行报告实际错误:
07-18 09:51:39.882: E/AndroidRuntime(25677): Caused by: java.lang.NullPointerException
在该行之后,您将看到2行,它是生成实际错误的代码执行堆栈:
07-18 09:51:39.882: E/AndroidRuntime(25677): at com.example.login1.sqlite.sqlitetry(sqlite.java:12)
07-18 09:51:39.882: E/AndroidRuntime(25677): at com.example.login1.Login.onCreate(Login.java:25)
基于此,您的问题只是因为空值sqLiteDB
:
SQLiteDatabase sqLiteDB = null;
as.sqlitetry(sqLiteDB);
答案 1 :(得分:0)
您的sqlite
课程扩展了活动。此外,您在sqlite中没有onCreate
方法
然后你有了这个
sqlite as = new sqlite();
您需要使用startActivity(new Intent(myintent))
启动活动。
建议你学习活动。
http://developer.android.com/guide/components/activities.html
你也有这个
sqlite as = new sqlite(); //sqlite class extends activity
SQLiteDatabase sqLiteDB = null; // null value
as.sqlitetry(sqLiteDB);
as.sqlinit();
如果它只是一个帮助类,你的sqlite不需要扩展Activity
。
另请参阅本教程
http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/
查看教程@ Youtube By travis
http://www.youtube.com/watch?v=gEg9OdufXmM(我猜测系列112到119)