首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >onUpdate之外的AppWidgetManager?

onUpdate之外的AppWidgetManager?
EN

Stack Overflow用户
提问于 2013-12-21 18:52:34
回答 1查看 416关注 0票数 1

我有一个简单的小部件,我尝试在onUpdate方法之外更新它的内容。我设置了: context和AppWidgetManager字段变量,编译器没有给出错误信息,但我在声明appWidgetManager的那两行代码中收到了一个NullPointerException。

有没有人能帮我解释一下为什么把这些变量变成字段变量还能产生NPE呢?

代码语言:javascript
复制
private void ref(int z) {

    views.setTextViewText(R.id.igePrint, ige[z].azIge);

    AppWidgetManager manager = AppWidgetManager.getInstance(context);
    manager.updateAppWidget(thisWidget, views);
    //Log.e("<<check value>>", "the value of z is: " + z);
}

以及其余的一些代码:

代码语言:javascript
复制
   package com.example.napiige;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.TextView;

public class MainActivity extends AppWidgetProvider {

    RemoteViews views;
    String azIge;   
    String igeBack;
    static MainActivity[] ige = new MainActivity[3];
    int x;
    public static ComponentName thisWidget;

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            x = (Integer)msg.obj;   
            ref(x);
        }       

       };

    Context context;


    @Override
     public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

        views = new RemoteViews(context.getPackageName(), R.layout.example_appwidget);
        thisWidget = new ComponentName(context, MainActivity.class);

        final int N = appWidgetIds.length;
        // Perform this loop procedure for each App Widget that belongs to this provider
        for (int i=0; i<N; i++) {
        int appWidgetId = appWidgetIds[i];

        String ig1 = context.getString(R.string.ige1);
        String ig2 = context.getString(R.string.ige2);
        String ig3 = context.getString(R.string.ige3);

        ige = new MainActivity[3];

        ige[0] = new MainActivity();
        ige[1] = new MainActivity();
        ige[2] = new MainActivity();

        ige[0].azIge = ig1;
        ige[1].azIge = ig2;
        ige[2].azIge = ig3;


        Runnable myRunnable1 = new Task(); 
        Thread myThread = new Thread(myRunnable1);
        myThread.start();

        appWidgetManager.updateAppWidget(thisWidget, views);

        }
    }

    private void ref(int z) {

        views.setTextViewText(R.id.igePrint, ige[z].azIge);

        AppWidgetManager manager = AppWidgetManager.getInstance(context);
        manager.updateAppWidget(thisWidget, views);
        //Log.e("<<check value>>", "the value of z is: " + z);

    }


    class Task implements Runnable {
    @Override
        public void run() {             
            for (int i = 0; i < 3; i++) {                   
                try {
                Thread.sleep(3000); 
                // refreshing the text inside the loop calling the above refreshTextView method

                int data = i;

                Message msg = handler.obtainMessage(1, (int)data);
                handler.sendMessage(msg);

                } catch (InterruptedException e) {
                    e.printStackTrace();
           }
        }
     }
  }

}
EN

回答 1

Stack Overflow用户

发布于 2013-12-21 19:15:03

这里的appWidgetManager字段是空的,因为您从未给它赋值。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20718207

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档