I wrote a method in MainActivity and when i call it from same activity (main) it run correctly but when i call it from another class i get this error
i write this program for android.
Logcat output:
04-22 19:53:11.895 16739-16739/mohammadi.behnam.lstcall I/##Contactadder##﹕ +989333346529
04-22 19:53:11.895 16739-16739/mohammadi.behnam.lstcall D/AndroidRuntime﹕ Shutting down VM
04-22 19:53:11.895 16739-16739/mohammadi.behnam.lstcall W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41d0bc08)
04-22 19:53:11.895 16739-16739/mohammadi.behnam.lstcall E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: mohammadi.behnam.lstcall, PID: 16739
java.lang.NullPointerException
at android.content.ContextWrapper.getContentResolver(ContextWrapper.java:105)
at mohammadi.behnam.lstcall.MainActivity.Contactadder(MainActivity.java:236)
at mohammadi.behnam.lstcall.IncomingCall$MyPhoneStateListener.onCallStateChanged(IncomingCall.java:57)
at android.telephony.PhoneStateListener$2.handleMessage(PhoneStateListener.java:461)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5635)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
public void Contactadder(String incomingNumber)
{
Log.i("##Contactadder##", incomingNumber);
String RawcID="";
String[] projectionPhones = {
ContactsContract.Data.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.RAW_CONTACT_ID,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.LABEL
};
ContentResolver cr = this.getContentResolver();
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projectionPhones,
ContactsContract.CommonDataKinds.Phone.NUMBER +"=?", new String[]{incomingNumber}, null);
while(pCur.moveToNext())
{
RawcID=pCur.getString(1);
}
ContentValues values = new ContentValues();
values.clear();
values.put(ContactsContract.CommonDataKinds.Phone.RAW_CONTACT_ID, RawcID);
getContentResolver().insert(ContactsContract.Data.CONTENT_URI
, values);
}
service class that i wanna call that method:
MainActivity ma=new MainActivity();
ma.Contactadder("+9183331111");