在onUpdate之外的AppWidgetManager?

时间:2013-12-21 10:52:34

标签: android variables text widget textview

我有一个简单的小部件,我尝试在onUpdate方法之外更新其内容。我创建了:context和appWidgetManager字段变量,并且编译器没有给出任何错误,但是在我声明AppWidgetManager的那两行上我收到了NullPointerException。

有人可以帮助我理解为什么将这些变成现场变量会让NPE仍然存在吗?

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

其余部分代码:

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

}

1 个答案:

答案 0 :(得分:1)

appWidgetManager字段为空,因为您从未为其指定值。