私はあなたの助けが必要です。 broadcastreceiverを使用し、そのnoを私のデータベースに保存したいのですが、私がそれを示すときは: - ヌルオブジェクト参照で仮想メソッド 'android.content.Context.getResources()'を呼び出そうとしました。事前に私...ありがとう...Sqliteデータベースにデータを格納する問題
ServiceReceiver.java
public class ServiceReceiver extends BroadcastReceiver {
MyReceiver receiver = new MyReceiver();
@Override
public void onReceive(final Context context, Intent intent) {
TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(new PhoneStateListener(){
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
System.out.println("incomingNumber :" + incomingNumber);
receiver.setData(incomingNumber, context);
}
}, PhoneStateListener.LISTEN_CALL_STATE);
}}
DataBaseHelper.java
public class DataBaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "callinfo.db";
public static final String TABLE_NAME = "misscall";
public static final String COL_1 = "ID";
public static final String COL_2 = "MOBILE";
public DataBaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("create table " + TABLE_NAME +" (ID INTEGER PRIMARY KEY AUTOINCREMENT,MOBILE VARCHAR(20));");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("DROP TABLE IF EXITS"+ TABLE_NAME);
onCreate(sqLiteDatabase);
}
public boolean insertData(String mobile) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2,mobile);
long result = db.insert(TABLE_NAME,null ,contentValues);
if(result == -1)
return false;
else
return true;
}}
MyReceiver.java
public class MyReceiver extends AppCompatActivity {
DataBaseHelper myDB;
public MyReceiver(){
}
public void setData(String incomingNumber, Context context){
myDB = new DataBaseHelper(context);//i think here i get that error
Toast.makeText(context, incomingNumber, Toast.LENGTH_LONG).show();
save(incomingNumber,context);
}
public void save(String number,Context context){
if(number!=null){
AddData(number);
}
}
public void AddData(String number) {
boolean isInserted = myDB.insertData(number);
if(isInserted == true)
Toast.makeText(MyReceiver.this, "Data Inserted", Toast.LENGTH_LONG).show();
else
Toast.makeText(MyReceiver.this,"Data not Inserted", Toast.LENGTH_LONG).show();
} }
エラー明らか
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.content.ContextWrapper.getResources(ContextWrapper.java:90)
at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:74)
at android.support.v7.app.AppCompatActivity.getResources(AppCompatActivity.java:551)
at android.widget.Toast.<init>(Toast.java:122)
at android.widget.Toast.makeText(Toast.java:290)
at com.example.abhijeetsingh.recievecall.MyReceiver.setData(MyReceiver.java:51)
at com.example.abhijeetsingh.recievecall.ServiceReceiver$1.onCallStateChanged(ServiceReceiver.java:37)
at android.telephony.PhoneStateListener$2.handleMessage(PhoneStateListener.java:404)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5951)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
ありがとうございました... – Abhi