使用Android SQLiteDatabase insert()方法时出错
我试图在活动中使用SQLiteDatabase中的方法insert。但我有一个错误: 12-08 13:28:24.497:错误/AndroidRuntime(20051):由以下原因引起:java.lang.NullPointerException 12-08 13:28:24.497:ERROR/AndroidRuntime(20051):在android.database.sqlite.SQLiteOpenHelper.getwriteabledatabase(SQLiteOpenHelper.java:118) 12-08 13:28:24.497:ERROR/AndroidRuntime(20051):位于com.psyhclo.RatedCalls.onCreate(RatedCalls.java:42) 错误在这一行: this.db=openHelper.getWritableDatabase() 下面是名为RatedCalls.java的活动的代码:使用Android SQLiteDatabase insert()方法时出错,android,sqlite,Android,Sqlite,我试图在活动中使用SQLiteDatabase中的方法insert。但我有一个错误: 12-08 13:28:24.497:错误/AndroidRuntime(20051):由以下原因引起:java.lang.NullPointerException 12-08 13:28:24.497:ERROR/AndroidRuntime(20051):在android.database.sqlite.SQLiteOpenHelper.getwriteabledatabase(SQLiteOpenHelp
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import com.psyhclo.R;
import com.psyhclo.CallDataHelper.OpenHelper;
import android.app.Activity;
import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.content.ContentValues;
import android.content.Context;
public class RatedCalls extends ListActivity {
private Context context;
private static TextView txtView;
private SQLiteDatabase db;
private CallDataHelper dh = null;
StringBuilder sb = new StringBuilder();
OpenHelper openHelper = new OpenHelper(this.context);
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Cursor cursor = getContentResolver().query(
android.provider.CallLog.Calls.CONTENT_URI, null, null, null,
android.provider.CallLog.Calls.DATE + " DESC ");
dh = new CallDataHelper(this);
db = openHelper.getWritableDatabase();
startManagingCursor(cursor);
int numberColumnId = cursor
.getColumnIndex(android.provider.CallLog.Calls.NUMBER);
int durationId = cursor
.getColumnIndex(android.provider.CallLog.Calls.DURATION);
int contactNameId = cursor
.getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME);
int dateId = cursor.getColumnIndex(android.provider.CallLog.Calls.DATE);
int numTypeId = cursor
.getColumnIndex(android.provider.CallLog.Calls.CACHED_NUMBER_TYPE);
Date dt = new Date();
int hours = dt.getHours();
int minutes = dt.getMinutes();
int seconds = dt.getSeconds();
String currTime = hours + ":" + minutes + ":" + seconds;
ArrayList<String> callList = new ArrayList<String>();
if (cursor.moveToFirst()) {
do {
String contactNumber = cursor.getString(numberColumnId);
String contactName = cursor.getString(contactNameId);
String duration = cursor.getString(durationId);
String callDate = DateFormat.getDateInstance().format(dateId);
String numType = cursor.getString(numTypeId);
ContentValues values = new ContentValues();
values.put("contact_id", 1);
values.put("contact_name", contactName);
values.put("number_type", numType);
values.put("contact_number", contactNumber);
values.put("duration", duration);
values.put("date", callDate);
values.put("current_time", currTime);
values.put("cont", 1);
this.db.insert(CallDataHelper.TABLE_NAME, null, values);
this.dh.insert(1, contactName, numType, contactNumber,
duration, callDate, currTime);
Toast.makeText(getBaseContext(), "Inserted!", Toast.LENGTH_LONG);
callList.add("Contact Number: " + contactNumber
+ "\nContact Name: " + contactName + "\nDuration: "
+ duration + "\nDate: " + callDate);
} while (cursor.moveToNext());
}
setListAdapter(new ArrayAdapter<String>(this, R.layout.listitem,
callList));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(),
((TextView) view).getText(), Toast.LENGTH_SHORT).show();
}
});
}
}
导入java.text.DateFormat;
导入java.util.ArrayList;
导入java.util.Date;
进口com.psyhclo.R;
导入com.psyhclo.CallDataHelper.OpenHelper;
导入android.app.Activity;
导入android.app.ListActivity;
导入android.database.Cursor;
导入android.database.sqlite.SQLiteDatabase;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.ArrayAdapter;
导入android.widget.ListView;
导入android.widget.TextView;
导入android.widget.Toast;
导入android.content.ContentValues;
导入android.content.Context;
公共类RatedCalls扩展了ListActivity{
私人语境;
私有静态文本视图txtView;
专用数据库数据库;
private CallDataHelper dh=null;
StringBuilder sb=新的StringBuilder();
OpenHelper OpenHelper=新的OpenHelper(this.context);
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Cursor Cursor=getContentResolver().query(
android.provider.CallLog.Calls.CONTENT_URI,null,null,null,
android.provider.CallLog.Calls.DATE+“DESC”);
dh=新的CallDataHelper(此);
db=openHelper.getWritableDatabase();
开始管理游标(游标);
int numberColumnId=光标
.getColumnIndex(android.provider.CallLog.Calls.NUMBER);
int durationId=游标
.getColumnIndex(android.provider.CallLog.Calls.DURATION);
int contactNameId=游标
.getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME);
int dateId=cursor.getColumnIndex(android.provider.CallLog.Calls.DATE);
int numTypeId=游标
.getColumnIndex(android.provider.CallLog.Calls.CACHED\u NUMBER\u类型);
日期dt=新日期();
int hours=dt.getHours();
int minutes=dt.getMinutes();
int seconds=dt.getSeconds();
字符串currTime=hours+“:”+minutes+“:”+seconds;
ArrayList callList=新的ArrayList();
if(cursor.moveToFirst()){
做{
String contactNumber=cursor.getString(numberColumnId);
String contactName=cursor.getString(contactNameId);
String duration=cursor.getString(durationId);
字符串callDate=DateFormat.getDateInstance().format(日期ID);
String numType=cursor.getString(numTypeId);
ContentValues=新的ContentValues();
值。put(“联系人id”,1);
值。输入(“联系人姓名”,联系人姓名);
value.put(“数字类型”,numType);
值。输入(“联系人号码”,联系人号码);
值。put(“持续时间”,持续时间);
值。put(“日期”,callDate);
值。put(“当前时间”,currTime);
价值。put(“cont”,1);
this.db.insert(CallDataHelper.TABLE_NAME,null,value);
this.dh.insert(1,contactName,numType,contactNumber,
持续时间、通话日期、当前时间);
Toast.makeText(getBaseContext(),“Inserted!”,Toast.LENGTH\u LONG);
添加(“联系人号码:”+联系人号码
+\n联系人名称:“+contactName+”\n联系人:
+持续时间+“\n日期:”+callDate);
}while(cursor.moveToNext());
}
setListAdapter(新阵列适配器)(此,R.layout.listitem,
呼叫列表);
ListView lv=getListView();
lv.setTextFilterEnabled(真);
setOnItemClickListener(新的android.widget.AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父级、视图、,
内部位置,长id){
Toast.makeText(getApplicationContext(),
((TextView)view.getText(),Toast.LENGTH_SHORT.show();
}
});
}
}
下面是名为CallDataHelper.java的Datahelper类,其中OpenHelper类被声明为一个子类
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
public class CallDataHelper {
private static final String DATABASE_NAME = "calls.db";
private static final int DATABASE_VERSION = 1;
protected static final String TABLE_NAME = "contact_data";
private Context context;
private SQLiteDatabase db;
public CallDataHelper(Context context) {
this.context = context;
OpenHelper openHelper = new OpenHelper(this.context);
this.db = openHelper.getWritableDatabase();
}
public boolean insert(Integer cId, String cName, String numType,
String cNum, String dur, String date, String currTime) {
this.db.execSQL("insert into "
+ TABLE_NAME
+ " (contact_id, contact_name, number_type, contact_number, duration, date, current_time, cont) "
+ " values( " + cId + ", " + cName + ", " + numType + ", "
+ cNum + ", " + dur + ", " + date + ", " + currTime + ", 1)");
return true;
}
public void update(String word) {
this.db.execSQL("UPDATE words SET cont = cont + 1 WHERE (word= ?)",
new String[] { word });
}
public void deleteAll() {
this.db.delete(TABLE_NAME, null, null);
}
public boolean select(String wrd) {
String word;
Cursor cursor = this.db.query(TABLE_NAME, new String[] { "word" },
"word like ?", new String[] { wrd }, null, null, null);
if (cursor.moveToFirst()) {
do {
word = cursor.getString(0);
} while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
return true;
} else {
return false;
}
}
public List<String> selectAll() {
List<String> list = new ArrayList<String>();
Cursor cursor = this.db.query(TABLE_NAME, new String[] { "word" },
null, null, null, null, "cont desc");
if (cursor.moveToFirst()) {
do {
list.add(cursor.getString(0).toUpperCase());
} while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
return list;
}
public static class OpenHelper extends SQLiteOpenHelper {
OpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE "
+ TABLE_NAME
+ "(id INTEGER PRIMARY KEY AUTOINCREMENT, contact_id INTEGER, contact_name VARCHAR(50), number_type VARCHAR(50), contact_number VARCHAR(50), duration TIME, date DATE, current_time TIME, cont INTEGER)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w("RatedCalls Database",
"Upgrading database, this will drop tables and recreate.");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
}
导入android.content.ContentValues;
导入android.content.Context;
导入android.database.Cursor;
导入android.database.sqlite.SQLiteDatabase;
导入android.database.sqlite.SQLiteOpenHelper;
导入android.database.sqlite.SQLiteStatement;
导入android.util.Log;
导入java.util.ArrayList;
导入java.util.List;
公共类CallDataHelper{
私有静态最终字符串数据库\u NAME=“calls.db”;
私有静态最终int数据库_VERSION=1;
受保护的静态最终字符串表\u NAME=“contact\u data”;
私人语境;
专用数据库数据库;
公共CallDataHelper(上下文){
this.context=上下文;
OpenHelper OpenHelper=新的OpenHelper(this.context);
this.db=openHelper.getWritableDatabase();
}
公共布尔插入(整数cId、字符串cName、字符串numType、,
字符串cNum、字符串dur、字符串日期、字符串currTime){
this.db.execSQL(“插入到”
+表名称
+(联系人id、联系人姓名、号码类型、联系人号码、持续时间、日期、当前时间、续)
+“值(“+cId+”、“+cName+”、“+numType+”、”
+cNum+”、“+dur+”、“+date+”、“+currTime+”,1)”;
返回true;
}
公共无效更新(字符串字){
this.db.execSQL(“更新单词集cont=cont+1,其中(单词=?)”,
新字符串[]{word});
}
public void deleteAll(){
这个是.db.delete
private Context context;
OpenHelper openHelper = new OpenHelper(this.context);
db = openHelper.getWritableDatabase();
dh = new CallDataHelper(this);
db = dh.getWritableDatabase();
OpenHelper openHelper = new OpenHelper(this.context);
OpenHelper openHelper;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
openHelper = new OpenHelper(this);