2010-12-08 15 views
0

ちょっと、CallLog.Callsから取得したデータを挿入しようとしています。ここでAndroid SQLiteデータベースに挿入しようとすると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

ですが、それはLogCatが言うことを考えると、この行でNullPointerExceptionがreturng
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

あなたのコードを正しく読み解くために質問を再フォーマットしてください。また、どの行のコードが58行目で識別されていますか? – McStretch

+0

改造されました。そして、行は this.dh.insert(1、contactName、numType、contactNumber、duration、callDate、currTime)です。 – rogcg

+1

どこでも 'dh'を初期化していないので、NullPointerException – ccheneson

答えて

2

あなたActivityのコンテキストを得るためにあなたのonCreate()内のあなたのCallDataHelperを初期化します。

ここは例です。

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

} 
+0

@ccheneson:右。ありがとうございました。急いで入力しました。 :) –

関連する問題