I am working on a project and want to read call logs and write them to file and send it to server. My code for reading call logs in working fine in a separate project. But it causes NullPointerException when integrates code with original project. All permissions are verified. here is my code
BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true));
buf.append("Testing ");
buf.newLine();
sb = new StringBuffer();
Uri str = CallLog.Calls.CONTENT_URI;
str.toString();
Log.e("TAG", str.toString());
@SuppressWarnings("deprecation")
Cursor managedCursor =managedQuery(CallLog.Calls.CONTENT_URI, null,
null, null, null);
Log.e("TAG", "in while " + ++i);
int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
sb.append("Call Details :");
Logcat shows this error 05-15 16:17:14.107: E/AndroidRuntime(20715): FATAL EXCEPTION: main 05-15 16:17:14.107: E/AndroidRuntime(20715): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.atu_proto/com.example.atu_proto.MenuList}: java.lang.NullPointerException
05-15 16:17:14.107: E/AndroidRuntime(20715): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2313)
05-15 16:17:14.107: E/AndroidRuntime(20715): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365)
05-15 16:17:14.107: E/AndroidRuntime(20715): at android.app.ActivityThread.access$600(ActivityThread.java:156)
05-15 16:17:14.107: E/AndroidRuntime(20715): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
05-15 16:17:14.107: E/AndroidRuntime(20715): at android.os.Handler.dispatchMessage(Handler.java:99)
05-15 16:17:14.107: E/AndroidRuntime(20715): at android.os.Looper.loop(Looper.java:153)
05-15 16:17:14.107: E/AndroidRuntime(20715): at android.app.ActivityThread.main(ActivityThread.java:5336)
05-15 16:17:14.107: E/AndroidRuntime(20715): at java.lang.reflect.Method.invokeNative(Native Method)
05-15 16:17:14.107: E/AndroidRuntime(20715): at java.lang.reflect.Method.invoke(Method.java:511)
05-15 16:17:14.107: E/AndroidRuntime(20715): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
05-15 16:17:14.107: E/AndroidRuntime(20715): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
05-15 16:17:14.107: E/AndroidRuntime(20715): at dalvik.system.NativeStart.main(Native Method)
05-15 16:17:14.107: E/AndroidRuntime(20715): Caused by: java.lang.NullPointerException
05-15 16:17:14.107: E/AndroidRuntime(20715): at android.content.ContextWrapper.getContentResolver(ContextWrapper.java:99)
05-15 16:17:14.107: E/AndroidRuntime(20715): at android.app.Activity.managedQuery(Activity.java:1751)
05-15 16:17:14.107: E/AndroidRuntime(20715): at com.example.atu_proto.CallLogsRetrieve.getCallDetails(CallLogsRetrieve.java:89)
05-15 16:17:14.107: E/AndroidRuntime(20715): at com.example.atu_proto.MenuList.onCreate(MenuList.java:78)
05-15 16:17:14.107: E/AndroidRuntime(20715): at android.app.Activity.performCreate(Activity.java:5122)
05-15 16:17:14.107: E/AndroidRuntime(20715): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
05-15 16:17:14.107: E/AndroidRuntime(20715): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2277) 05-15 16:17:14.107: E/AndroidRuntime(20715): ... 11 more
05-15 16:17:14.107: E/AndroidRuntime(20715): Caused by: java.lang.NullPointerException
05-15 16:17:14.107: E/AndroidRuntime(20715): at android.content.ContextWrapper.getContentResolver(ContextWrapper.java:99)
...
05-15 16:17:14.107: E/AndroidRuntime(20715): at com.example.atu_proto.CallLogsRetrieve.getCallDetails(CallLogsRetrieve.java:89)
05-15 16:17:14.107: E/AndroidRuntime(20715): at com.example.atu_proto.MenuList.onCreate(MenuList.java:78)
This suggests you're calling another activity from your MenuList
activity, likely creating the other activity with new
.
Never instantiate activities with new
.
In this case it seems CallLogsRetrieve
should not be an activity and you could pass in an Activity
reference as an argument to methods that need it.