Я пытаюсь использовать метод вставки из SQLiteDatabase в моей деятельности. Но у меня эта ошибка:
12-08 13: 28: 24.497: ОШИБКА / AndroidRuntime (20051): вызвано: java.lang.NullPointerException
12-08 13: 28: 24.497: ОШИБКА / AndroidRuntime (20051): в android.database.sqlite.SQLiteOpenHelper.getWritableDatabase (SQLiteOpenHelper.java:118)
12-08 13: 28: 24.497: ОШИБКА / AndroidRuntime (20051): на com.psyhclo.RatedCalls.onCreate (RatedCalls.java:42)
Ошибка в этой строке:
this.db = openHelper.getWritableDatabase ();
вот код действия с именем RatedCalls.java :
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();
}
});
}
}
А вот класс Datahelper с именем CallDataHelper.java , где класс 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);
}
}
}