无法在RemoteView中获取数据库值

时间:2012-03-02 11:18:25

标签: android android-appwidget

我想创建一个AppWidget,我定义布局,app小部件的信息,广播接收器和处理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);

这是我的代码.............

但在其他活动中,我可以轻松检索数据库值,而不会出现任何空指针异常。如果有人能够指出我的代码有什么问题,我会非常感激................

日志cat错误是:

03-02 17:26:32.174:E / AndroidRuntime(32265):致命异常:主要 03-02 17:26:32.174:E / AndroidRuntime(32265):了java.lang.RuntimeException:无法启动意图服务com.android.braindigit.schedulesms.appWidgetService@4053da30 {CMP = com.android.braindigit.schedulesms / .appWidgetService(有额外内容)}:java.lang.NullPointerException 03-02 17:26:32.174:E / AndroidRuntime(32265):在android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2052) 03-02 17:26:32.174:E / AndroidRuntime(32265):在android.app.ActivityThread.access $ 2800(ActivityThread.java:117) 03-02 17:26:32.174:E / AndroidRuntime(32265):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:994) 03-02 17:26:32.174:E / AndroidRuntime(32265):在android.os.Handler.dispatchMessage(Handler.java:99) 03-02 17:26:32.174:E / AndroidRuntime(32265):在android.os.Looper.loop(Looper.java:130) 03-02 17:26:32.174:E / AndroidRuntime(32265):在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):引起:java.lang.NullPointerException 03-02 17:26:32.174:E / AndroidRuntime(32265):在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):在android.app.Service.onStartCommand(Service.java:428) 03-02 17:26:32.174:E / AndroidRuntime(32265):在android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2039) 03-02 17:26:32.174:E / AndroidRuntime(32265):... 10 more

1 个答案:

答案 0 :(得分:0)

嗯,我不知道回答我自己的问题是多么真实,但我认为如果他们和我有同样的问题会帮助其他人.....所以,在敲了几个小时之后,我终于明白了我正在传递一个空的上下文。所以,我做了以下事情并且它起作用....

从广泛的接收器我通过以下意图到我的服务:

    Intent intent = new Intent(context.getApplicationContext(),appWidgetService.class);
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,allWidgetIds);
    intent.putExtra("contactNumber", contactNumber);
    intent.putExtra("date", date);
    intent.putExtra("message",message);
    intent.putExtra("status", status);
    //update the widgets via the service

在服务中我做了以下事情:

    String contactNumber = intent.getStringExtra("contactNumber");
    String date = intent.getStringExtra("date");
    String message =intent.getStringExtra("message");
    String status = intent.getStringExtra("status");


    for (int widgetId : allWidgetIds) {
        RemoteViews remoteViews = new RemoteViews(this
                .getApplicationContext().getPackageName(),
                R.layout.schdulesms_appwidget_layout);



        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);