Well I wanted to make an AppWidget, I define the layouts,info for app widget, a broadcast receiver and a service to handle AppWidget.
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this
.getApplicationContext());
int[] allWidgetIds = intent
.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
ComponentName thisWidget = new ComponentName(getApplicationContext(),
SmsSchedulerWidget.class);
RemoteViews remoteViews = new RemoteViews(this.getApplicationContext()
.getPackageName(), R.layout.schdulesms_appwidget_layout);
DatabaseManager dbManager = new DatabaseManager(context.getApplicationContext());
dbManager.open();
String contactNumber = dbManager.fetchContactNumber();
String date = dbManager.fetchDate();
String message = dbManager.fetchMessage();
String status = dbManager.fetchStatus();
remoteViews.setTextViewText(R.id.to_appwidget_saved_data, contactNumber);
remoteViews.setTextViewText(R.id.date_appwidget_saved_data, date);
remoteViews.setTextViewText(R.id.status_appwidget_saved, message);
remoteViews.setTextViewText(R.id.message_appwidgset_saved_data, status);
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
This is my code………….
But in other activities i can easily retrieve database values without any null pointer exception. Please if anybody could point out whats wrong with my code than i will be higly grateful…………….
The log cat errors are:
03-02 17:26:32.174: E/AndroidRuntime(32265): FATAL EXCEPTION: main
03-02 17:26:32.174: E/AndroidRuntime(32265): java.lang.RuntimeException: Unable to start service com.android.braindigit.schedulesms.appWidgetService@4053da30 with Intent { cmp=com.android.braindigit.schedulesms/.appWidgetService (has extras) }: java.lang.NullPointerException
03-02 17:26:32.174: E/AndroidRuntime(32265): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2052)
03-02 17:26:32.174: E/AndroidRuntime(32265): at android.app.ActivityThread.access$2800(ActivityThread.java:117)
03-02 17:26:32.174: E/AndroidRuntime(32265): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:994)
03-02 17:26:32.174: E/AndroidRuntime(32265): at android.os.Handler.dispatchMessage(Handler.java:99)
03-02 17:26:32.174: E/AndroidRuntime(32265): at android.os.Looper.loop(Looper.java:130)
03-02 17:26:32.174: E/AndroidRuntime(32265): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-02 17:26:32.174: E/AndroidRuntime(32265): at java.lang.reflect.Method.invokeNative(Native Method)
03-02 17:26:32.174: E/AndroidRuntime(32265): at java.lang.reflect.Method.invoke(Method.java:507)
03-02 17:26:32.174: E/AndroidRuntime(32265): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-02 17:26:32.174: E/AndroidRuntime(32265): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-02 17:26:32.174: E/AndroidRuntime(32265): at dalvik.system.NativeStart.main(Native Method)
03-02 17:26:32.174: E/AndroidRuntime(32265): Caused by: java.lang.NullPointerException
03-02 17:26:32.174: E/AndroidRuntime(32265): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118)
03-02 17:26:32.174: E/AndroidRuntime(32265): at com.android.braindigit.schedulesms.DatabaseManager.open(DatabaseManager.java:36)
03-02 17:26:32.174: E/AndroidRuntime(32265): at com.android.braindigit.schedulesms.appWidgetService.onStart(appWidgetService.java:41)
03-02 17:26:32.174: E/AndroidRuntime(32265): at android.app.Service.onStartCommand(Service.java:428)
03-02 17:26:32.174: E/AndroidRuntime(32265): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2039)
03-02 17:26:32.174: E/AndroidRuntime(32265): … 10 more
Well I dont know how genuine it is to answer my own question but i thought it would help other people if they have the same problem as me…..So, after banging my head for hours, finally i realised that i was passing a null context.So, i did the following things and it worked….
from the broadcase receiver I passed the following intents to my service:
and in service i did the following things: