当我在Android应用中运行一个活动时,实例化dbs类如下:
private final DbOpenHelper dbOpen = new DbOpenHelper(MainMonitor.this);
private final SQLiteDatabase db = dbOpen.getWritableDatabase();
private final ContentValues cv = new ContentValues();
它有例外:
07-10 15:33:31.581: E/AndroidRuntime(1142): FATAL EXCEPTION: main
07-10 15:33:31.581: E/AndroidRuntime(1142): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.android.analizer/com.android.analyzer.MainMonitor}: java.lang.NullPointerException
07-10 15:33:31.581: E/AndroidRuntime(1142): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1879)
07-10 15:33:31.581: E/AndroidRuntime(1142): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
07-10 15:33:31.581: E/AndroidRuntime(1142): at android.app.ActivityThread.access$600(ActivityThread.java:122)
07-10 15:33:31.581: E/AndroidRuntime(1142): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
07-10 15:33:31.581: E/AndroidRuntime(1142): at android.os.Handler.dispatchMessage(Handler.java:99)
07-10 15:33:31.581: E/AndroidRuntime(1142): at android.os.Looper.loop(Looper.java:137)
07-10 15:33:31.581: E/AndroidRuntime(1142): at android.app.ActivityThread.main(ActivityThread.java:4340)
07-10 15:33:31.581: E/AndroidRuntime(1142): at java.lang.reflect.Method.invokeNative(Native Method)
07-10 15:33:31.581: E/AndroidRuntime(1142): at java.lang.reflect.Method.invoke(Method.java:511)
07-10 15:33:31.581: E/AndroidRuntime(1142): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-10 15:33:31.581: E/AndroidRuntime(1142): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-10 15:33:31.581: E/AndroidRuntime(1142): at dalvik.system.NativeStart.main(Native Method)
07-10 15:33:31.581: E/AndroidRuntime(1142): Caused by: java.lang.NullPointerException
07-10 15:33:31.581: E/AndroidRuntime(1142): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221)
07-10 15:33:31.581: E/AndroidRuntime(1142): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:157)
07-10 15:33:31.581: E/AndroidRuntime(1142): at com.android.analyzer.MainMonitor.<init>(MainMonitor.java:40)
07-10 15:33:31.581: E/AndroidRuntime(1142): at java.lang.Class.newInstanceImpl(Native Method)
07-10 15:33:31.581: E/AndroidRuntime(1142): at java.lang.Class.newInstance(Class.java:1319)
07-10 15:33:31.581: E/AndroidRuntime(1142): at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
07-10 15:33:31.581: E/AndroidRuntime(1142): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1870)
07-10 15:33:31.581: E/AndroidRuntime(1142): ... 11 more
DbOpenHelper看起来像这样:
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DbOpenHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "analyzer";
private static final int DB_VERSION = 1;
public static final String TABLE_NAME = "network";
public static final String ACTIVITY = "activity";
public static final String CREATE_TABLE_NETWORK = "CREATE TABLE "+TABLE_NAME+"(" +
"_id integer primary key autoincrement, activity integer default 1)";
// DbOpenHelper openHelper = null;
public DbOpenHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
// openHelper = new DbOpenHelper(context);
}
public void onCreate(SQLiteDatabase sqliteDb) {
sqliteDb.execSQL(CREATE_TABLE_NETWORK);
sqliteDb.execSQL("INSERT INTO network(activity) VALUES(1)");
}
public void onUpgrade(SQLiteDatabase sqliteDb, int i, int i2) {
}
// @Override
// public void close() {
// if (openHelper!=null) openHelper.close();
// }
}
问题解决了,但出现了下一个问题。我将插入方法放在DbOpenHelper类中,如下所示:
public void onCreate(SQLiteDatabase sqliteDb) {
sqliteDb.execSQL(CREATE_TABLE_NETWORK);
// sqliteDb.execSQL("INSERT INTO network(activity) VALUES(1)");
ContentValues cv = new ContentValues();
cv.put(ACTIVITY_FIELD, 1);
sqliteDb.insert(DbOpenHelper.TABLE_NAME, null, cv);
}
在Activity中我试图以这种方式更新表:
cv.put(DbOpenHelper.ACTIVITY_FIELD, 0);
// db.insert(DbOpenHelper.TABLE_NAME, null, cv);
db.update(DbOpenHelper.TABLE_NAME, cv, null, null);
An Exception is:
07-11 15:02:47.341: E/AndroidRuntime(933): FATAL EXCEPTION: main
07-11 15:02:47.341: E/AndroidRuntime(933): java.lang.NullPointerException
07-11 15:02:47.341: E/AndroidRuntime(933): at android.database.sqlite.SQLiteStatement.releaseAndUnlock(SQLiteStatement.java:290)
07-11 15:02:47.341: E/AndroidRuntime(933): at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:96)
07-11 15:02:47.341: E/AndroidRuntime(933): at android.database.sqlite.SQLiteDatabase.updateWithOnConflict(SQLiteDatabase.java:1810)
07-11 15:02:47.341: E/AndroidRuntime(933): at android.database.sqlite.SQLiteDatabase.update(SQLiteDatabase.java:1761)
07-11 15:02:47.341: E/AndroidRuntime(933): at com.android.analyzer.MainMonitor$2.onCheckedChanged(MainMonitor.java:99)
07-11 15:02:47.341: E/AndroidRuntime(933): at android.widget.CompoundButton.setChecked(CompoundButton.java:125)
07-11 15:02:47.341: E/AndroidRuntime(933): at android.widget.Switch.setChecked(Switch.java:517)
07-11 15:02:47.341: E/AndroidRuntime(933): at android.widget.Switch.animateThumbToCheckedState(Switch.java:508)
07-11 15:02:47.341: E/AndroidRuntime(933): at android.widget.Switch.stopDrag(Switch.java:498)
07-11 15:02:47.341: E/AndroidRuntime(933): at android.widget.Switch.onTouchEvent(Switch.java:458)
07-11 15:02:47.341: E/AndroidRuntime(933): at android.view.View.dispatchTouchEvent(View.java:5486)
07-11 15:02:47.341: E/AndroidRuntime(933): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1959)
07-11 15:02:47.341: E/AndroidRuntime(933): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1728)
07-11 15:02:47.341: E/AndroidRuntime(933): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1959)
07-11 15:02:47.341: E/AndroidRuntime(933): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1728)
07-11 15:02:47.341: E/AndroidRuntime(933): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1959)
07-11 15:02:47.341: E/AndroidRuntime(933): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1728)
07-11 15:02:47.341: E/AndroidRuntime(933): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1959)
07-11 15:02:47.341: E/AndroidRuntime(933): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1728)
07-11 15:02:47.341: E/AndroidRuntime(933): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1892)
07-11 15:02:47.341: E/AndroidRuntime(933): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1371)
07-11 15:02:47.341: E/AndroidRuntime(933): at android.app.Activity.dispatchTouchEvent(Activity.java:2364)
07-11 15:02:47.341: E/AndroidRuntime(933): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1840)
07-11 15:02:47.341: E/AndroidRuntime(933): at android.view.View.dispatchPointerEvent(View.java:5662)
07-11 15:02:47.341: E/AndroidRuntime(933): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:2863)
07-11 15:02:47.341: E/AndroidRuntime(933): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
07-11 15:02:47.341: E/AndroidRuntime(933): at android.os.Handler.dispatchMessage(Handler.java:99)
07-11 15:02:47.341: E/AndroidRuntime(933): at android.os.Looper.loop(Looper.java:137)
07-11 15:02:47.341: E/AndroidRuntime(933): at android.app.ActivityThread.main(ActivityThread.java:4340)
07-11 15:02:47.341: E/AndroidRuntime(933): at java.lang.reflect.Method.invokeNative(Native Method)
07-11 15:02:47.341: E/AndroidRuntime(933): at java.lang.reflect.Method.invoke(Method.java:511)
07-11 15:02:47.341: E/AndroidRuntime(933): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-11 15:02:47.341: E/AndroidRuntime(933): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-11 15:02:47.341: E/AndroidRuntime(933): at dalvik.system.NativeStart.main(Native Method)
我发现此行有用 -
at com.android.analyzer.MainMonitor$2.onCheckedChanged(MainMonitor.java:99)
似乎没有在dbs中创建表或/和行,这就是它无法自行更新的原因。这个类似拖拽的sqlite有什么问题? =)
答案 0 :(得分:1)
你可能在onCreate中得到一个空指针异常 - 我不明白INSERT INTO网络(活动) - 当然它只是INSERT INTO网络 - 你插入表名
但这不是你打算插入的方式 - 使用这个例子
db = this.getWriteableDatabase();
ContentValues insertValues = new ContentValues();
insertValues.put("Description", "Electricity");
insertValues.put("Amount", 500);
insertValues.put("Trans", 1);
insertValues.put("EntryDate", "04/06/2011");
db.insert("CashData", null, insertValues);
答案 1 :(得分:1)
首先你可以不插入就检查。如果创建了数据库,那么您可以测试插入。 检查数据文件夹,是否创建了数据库。
答案 2 :(得分:1)
Activity
仅在onCreate
致电后创建。所以当你打电话的时候
private final DbOpenHelper dbOpen = new DbOpenHelper(MainMonitor.this);
活动仍未创建。
从db选项中删除final
修饰符,并在onCreate
调用后以单独的方法初始化它。
你应该这样做:
public class MainMonitor extends Activity {
private DbOpenHelper dbOpen;
private SQLiteDatabase db;
private ContentValues cv;
@Override
protected void onResume() {
super.onResume();
initDb();
}
private void initDb() {
dbOpen = new DbOpenHelper(MainMonitor.this);
db = dbOpen.getWritableDatabase();
cv = new ContentValues();
}
//....your code
}
答案 3 :(得分:0)
我相信这是给你带来麻烦的一线:
sqliteDb.execSQL("INSERT INTO network(activity) VALUES(1)");
尝试,
ContentValues v = new ContentValues();
v.put(ACTIVITY, 1);
sqliteDb.insert(TABLE_NAME, null, v);
确保不插入空的ContentValues
对象,否则将抛出异常。
答案 4 :(得分:0)
你的错误就在这一行
sqliteDb.execSQL("INSERT INTO network(activity) VALUES(1)");
因为您尝试引用表格中的activity
列,所以应该用引号括起来。它应该是这样的
sqliteDb.execSQL("INSERT INTO network(\"activity\") VALUES(1)");
或
sqliteDb.execSQL("INSERT INTO network(" + ACTIVITY + ") VALUES(1)");
答案 5 :(得分:0)
public static final String ACTIVITY =“activity”;
我认为您可以尝试使用其他名称更改ACTIVITY字段。 db对象是否为null。 更改DB_VERSION值。