android插入行

时间:2011-09-13 22:31:44

标签: android sqlite insert row

使用我的活动我会显示如下表格:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="@+id/textView1" android:layout_height="wrap_content"
    android:layout_width="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/add_series" android:layout_gravity="center_horizontal"></TextView>
<EditText android:layout_height="wrap_content"
    android:layout_width="match_parent" android:id="@+id/series_name">
    <requestFocus></requestFocus>
</EditText>
<Button android:layout_height="wrap_content" android:id="@+id/button_save"
    android:layout_width="match_parent" android:text="@string/save"></Button>
</LinearLayout>

我还有一种获取此值的方法。

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add_series);

    final wecDatabasesManager databaseHelper = new wecDatabasesManager(this);

    final Button button_save = (Button) findViewById(R.id.button_save);
    button_save.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            final EditText series_name = (EditText) findViewById(R.id.series_name);  
            String name = series_name.getText().toString();

            databaseHelper.addSeries(null, name, 2);
        }
    });
}

在我的DatabasesManager类中,我有这个方法。

public void addSeries(SQLiteDatabase db, String name, int publisher_id)
{
    ContentValues v = new ContentValues();
    v.put(wecSeriesTable.NAME, name);
    v.put(wecSeriesTable.PUBLISHER_ID, publisher_id);
    db.insert(wecSeriesTable.TABLE_NAME, null, v);
}

但是当我尝试保存表单值时,我有这个错误:

09-13 22:16:57.751: WARN/dalvikvm(2811): threadid=1: thread exiting with uncaught exception (group=0x40015560)
09-13 22:16:57.761: ERROR/AndroidRuntime(2811): FATAL EXCEPTION: main
09-13 22:16:57.761: ERROR/AndroidRuntime(2811): java.lang.NullPointerException
09-13 22:16:57.761: ERROR/AndroidRuntime(2811):     at zepod.whatelsecomics.databases.wecDatabasesManager.addSeries(wecDatabasesManager.java:102)
09-13 22:16:57.761: ERROR/AndroidRuntime(2811):     at zepod.whatelsecomics.wecSeriesAddActivity$1.onClick(wecSeriesAddActivity.java:25)
09-13 22:16:57.761: ERROR/AndroidRuntime(2811):     at android.view.View.performClick(View.java:2485)
09-13 22:16:57.761: ERROR/AndroidRuntime(2811):     at android.view.View$PerformClick.run(View.java:9080)
09-13 22:16:57.761: ERROR/AndroidRuntime(2811):     at android.os.Handler.handleCallback(Handler.java:587)
09-13 22:16:57.761: ERROR/AndroidRuntime(2811):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-13 22:16:57.761: ERROR/AndroidRuntime(2811):     at android.os.Looper.loop(Looper.java:123)
09-13 22:16:57.761: ERROR/AndroidRuntime(2811):     at android.app.ActivityThread.main(ActivityThread.java:3647)
09-13 22:16:57.761: ERROR/AndroidRuntime(2811):     at java.lang.reflect.Method.invokeNative(Native Method)
09-13 22:16:57.761: ERROR/AndroidRuntime(2811):     at java.lang.reflect.Method.invoke(Method.java:507)
09-13 22:16:57.761: ERROR/AndroidRuntime(2811):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-13 22:16:57.761: ERROR/AndroidRuntime(2811):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-13 22:16:57.761: ERROR/AndroidRuntime(2811):     at dalvik.system.NativeStart.main(Native Method)
09-13 22:16:57.781: WARN/ActivityManager(68):   Force finishing activity zepod.whatelsecomics/.wecSeriesAddActivity
09-13 22:16:58.321: WARN/ActivityManager(68): Activity pause timeout for HistoryRecord{4076bc68 zepod.whatelsecomics/.wecSeriesAddActivity}

为什么呢? 有人能帮助我吗?

我刚刚更新了代码。

1 个答案:

答案 0 :(得分:1)

这是StackTrace的重要信息:

09-13 22:16:57.761: ERROR/AndroidRuntime(2811): java.lang.NullPointerException

09-13 22:16:57.761: ERROR/AndroidRuntime(2811):     at zepod.whatelsecomics.databases.wecDatabasesManager.addSeries(wecDatabasesManager.java:102)

您在wecDatabasesManager的addSeries方法中第102行访问的任何对象都可能为null。如果您无法快速解决这个问题,请发布addSeries方法代码

修改

您将null传递给addSeries,它期望SqliteDatabase的实例

databaseHelper.addSeries(null, name, 2);

然后:

public void addSeries(SQLiteDatabase db, String name, int publisher_id)
{
    ...
    db.insert(wecSeriesTable.TABLE_NAME, null, v);
}

db为null