gpt4 book ai didi

java - 为什么我无法将第二个 EditText 字段中的值添加到数据库中?

转载 作者:行者123 更新时间:2023-12-01 12:50:46 26 4
gpt4 key购买 nike

我认为自己是 Android 开发世界的新手。最近在使用 SQLite 数据库时,遇到了一个奇怪的问题。运行时我可以查看所有内容。但是当我尝试通过单击“添加”按钮添加内容时,系统就关闭了。这是我的代码。如果您知道请帮忙。非常感激。

感谢大家的帮助。最初的问题已解决,但我仍然对我的代码感到困惑,因为我仍然找不到错误的根源。为什么数据库只返回第一个 EditText 字段的值,而不返回第二个 EditText 字段的值?再次感谢。

sqlhelper

package com.example.sgdriverdiary;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

// A helper class to manage database creation and version management.
public class MySQLiteHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "mydb";
private static final int DB_VERSION = 1;

public static final String TABLE_NAME = "record";
public static final String COL_LITRE = "rLitre";
public static final String COL_DATE = "rDate";
public static final String COL_MILE = "rMile";
private static final String STRING_CREATE = "CREATE TABLE " + TABLE_NAME + " (_id INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_DATE+" DATE, "+COL_MILE+" TEXT, "+COL_LITRE+" TEXT);";
public MySQLiteHelper(Context ontext){
super(ontext, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db){
db.execSQL(STRING_CREATE);
ContentValues cv = new ContentValues(3);
cv.put(COL_LITRE, "6");
cv.put(COL_MILE, "500");
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
cv.put(COL_DATE, dateFormat.format(new Date()));
db.insert(TABLE_NAME, null,cv);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}
}

资源/布局/记录

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.sgdriverdiary.Record$PlaceholderFragment"
android:background="@drawable/layout_border" >

<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>

<EditText
android:id="@+id/mile"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="220dp"
android:ems="10" />

<EditText
android:id="@+id/litre"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/mile"
android:layout_below="@+id/mile"
android:ems="10" >

<requestFocus />
</EditText>

<Button
android:id="@+id/add"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/litre"
android:layout_below="@+id/litre"
android:layout_marginTop="19dp"
android:text="Add New Record" />

</RelativeLayout>

记录.java

package com.example.sgdriverdiary;


import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class Record extends Activity implements View.OnClickListener,AdapterView.OnItemClickListener{
EditText Text1;
EditText Text2;
Button Add;
ListView List;

MySQLiteHelper mHelper;
SQLiteDatabase mDb;
Cursor mCursor;
SimpleCursorAdapter mAdapter;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_record);
Text1 = (EditText)findViewById(R.id.mile);
Text2 = (EditText)findViewById(R.id.litre);
Add = (Button)findViewById(R.id.add);
Add.setOnClickListener(this);
List = (ListView)findViewById(R.id.list);
List.setOnItemClickListener(this);
mHelper = new MySQLiteHelper(this);
}
public void onResumer(){
super.onResume();
mDb = mHelper.getWritableDatabase();
String[] columns = new String[] {"_id", MySQLiteHelper.COL_DATE, MySQLiteHelper.COL_MILE, MySQLiteHelper.COL_LITRE};
mCursor = mDb.query(MySQLiteHelper.TABLE_NAME, columns, null, null, null, null, null);
String[] headers = new String[] {MySQLiteHelper.COL_DATE, MySQLiteHelper.COL_MILE, MySQLiteHelper.COL_LITRE};
mAdapter = new SimpleCursorAdapter(this, android.R.layout.two_line_list_item,mCursor, headers, new int[]{android.R.id.text1, android.R.id.text2});
List.setAdapter(mAdapter);
}
public void onPause(){
super.onPause();
mDb.close();
mCursor.close();
}


public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
mCursor.moveToPosition(position);
String rowId = mCursor.getString(0);
mDb.delete(MySQLiteHelper.TABLE_NAME, "_id = ?", new String[]{rowId});
mCursor.requery();
mAdapter.notifyDataSetChanged();
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues(3);
cv.put(MySQLiteHelper.COL_MILE, Text1.getText().toString());
cv.put(MySQLiteHelper.COL_LITRE, Text2.getText().toString());
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
cv.put(MySQLiteHelper.COL_DATE, dateFormat.format(new Date()));
mDb.insert(MySQLiteHelper.TABLE_NAME, null,cv);
mCursor.requery();
mAdapter.notifyDataSetChanged();
Text1.setText(null);
Text2.setText(null);

}

}

日志车错误

06-15 07:34:50.190: W/dalvikvm(3486): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
06-15 07:34:50.240: E/AndroidRuntime(3486): FATAL EXCEPTION: main
06-15 07:34:50.240: E/AndroidRuntime(3486): java.lang.NullPointerException
06-15 07:34:50.240: E/AndroidRuntime(3486): at com.example.sgdriverdiary.Record.onClick(Record.java:75)
06-15 07:34:50.240: E/AndroidRuntime(3486): at android.view.View.performClick(View.java:3511)
06-15 07:34:50.240: E/AndroidRuntime(3486): at android.view.View$PerformClick.run(View.java:14105)
06-15 07:34:50.240: E/AndroidRuntime(3486): at android.os.Handler.handleCallback(Handler.java:605)
06-15 07:34:50.240: E/AndroidRuntime(3486): at android.os.Handler.dispatchMessage(Handler.java:92)
06-15 07:34:50.240: E/AndroidRuntime(3486): at android.os.Looper.loop(Looper.java:137)
06-15 07:34:50.240: E/AndroidRuntime(3486): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-15 07:34:50.240: E/AndroidRuntime(3486): at java.lang.reflect.Method.invokeNative(Native Method)
06-15 07:34:50.240: E/AndroidRuntime(3486): at java.lang.reflect.Method.invoke(Method.java:511)
06-15 07:34:50.240: E/AndroidRuntime(3486): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-15 07:34:50.240: E/AndroidRuntime(3486): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-15 07:34:50.240: E/AndroidRuntime(3486): at dalvik.system.NativeStart.main(Native Method)

最佳答案

异常是由于 mDb 值为 null 引起的,您刚刚在 onResumer 方法中初始化了它。我认为您在此方法的名称中犯了拼写错误,因此请替换

public void onResumer(){ 

@Override
public void onResume(){

关于java - 为什么我无法将第二个 EditText 字段中的值添加到数据库中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24228566/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com