安卓插入行
在我的活动中,我显示这样的表单:
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是来自 StackTrace 的重要信息:
无论您在 wecDatabasesManager 的 addSeries 方法的第 102 行访问的任何对象都可能为 null。如果您无法快速弄清楚这一点,请发布 addSeries 方法代码
编辑
您将 null 传递给 addSeries,其中它需要 SqliteDatabase 实例
,然后:
db 为 null
This is the important information from your StackTrace:
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
and then:
db is null