尝试在Android SQLite数据库上插入时出现NullPointerException

时间:2010-12-08 13:27:44

标签: android sqlite

嘿我正在尝试插入从CallLog.Calls获取的数据。但考虑到LogCat的说法,它在这条线上返回了一个NUllPointerException:

this.dh.insert(1, contactName, numType, contactNumber, duration, callDate, currTime);

这是错误:

12-08 11:18:16.377: ERROR/AndroidRuntime(16418): Caused by: java.lang.NullPointerException
12-08 11:18:16.377: ERROR/AndroidRuntime(16418):     at com.psyhclo.RatedCalls.onCreate(RatedCalls.java:58)

以下是 CallDataHelper.java

类中的方法插入
public boolean insert(Integer cId, String cName, String numType,
String cNum, String dur, String date, String currTime) {
this.db.execSQL("insert into "
+ TABLE_NAME
+ "(id, contact_id, contact_name, number_type, contact_number, duration, date, current_time, cont) "
+ "values( ? ," + cId + ", " + cName + ", " + numType + ", "
+ cNum + ", " + dur + ", " + date + ", " + currTime + ", ? )");
return true;  
}

以下是我尝试插入数据的 RatedCalls.java 类的代码

package com.psyhclo;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;

import com.psyhclo.R;

import android.app.Activity;
import android.app.ListActivity;
import android.database.Cursor;
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;

public class RatedCalls extends ListActivity {

  private static TextView txtView;
  private CallDataHelper dh;
  StringBuilder sb = new StringBuilder();

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

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

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

   }
   });  
   }
   }

1 个答案:

答案 0 :(得分:2)

CallDataHelper内初始化onCreate()以获取Activity的上下文。

这是一个例子。

private CallDataHelper dh = null;

@Override
protected void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    // All your other stuff

    dh = new CallDataHelper(this);

    // and so on ...

    dh.insert( ....)

}