安卓插入行

发布于 2024-12-04 19:44:13 字数 3959 浏览 0 评论 0原文

在我的活动中,我显示这样的表单:

    <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}

为什么? 有人可以帮助我吗?

我刚刚更新了代码。

whit my Activity I display a form like this:

    <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>

I have also a method to get this value.

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);
        }
    });
}

In my DatabasesManager class i have this method.

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);
}

But when i try to save a form value i have this error:

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}

Why?
Can someone help me?

I have just update the code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魔法唧唧 2024-12-11 19:44:13

这是来自 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

This is the important information from your 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)

Whatever object(s) your accessing at line 102 in the addSeries method of wecDatabasesManager could potentially be null. Post the addSeries method code if you are unable to quickly figure this one out

EDIT

You're passing a null to addSeries where it's expecting an instance of SqliteDatabase

databaseHelper.addSeries(null, name, 2);

and then:

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

db is null

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文