的OnUpdate()intilized变量是空的Widget类的onReceive [英] onUpdate() intilized variable are null in onReceive of widget class

查看:210
本文介绍了的OnUpdate()intilized变量是空的Widget类的onReceive的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在的OnUpdate()方法初始化一个变量,在那之后我打电话的onReceive()函数,它运行良好,但不能访问varible集的OnUpdate()方法。这是为什么?这些varible是字符串variableand声明公开。我失去了一些东西?

I initialize one variable in an onUpdate() method and after that I call onReceive() function which runs fine but cannot access varible set in onUpdate() method. Why is that? Those varible is string variableand are declared public. Am I missing something?

public class WidgetTest extends AppWidgetProvider {
public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";
public String state;
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) 
{
    Log.e("UPDATE", "Start");   
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
     state="State_update"

 System.out.println(state);// My variable is initilised
    Intent active = new Intent(context, WidgetTest.class);
    active.setAction(ACTION_WIDGET_RECEIVER);       
    PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
    remoteViews.setOnClickPendingIntent(R.id.buttonclick, actionPendingIntent);

    super.onUpdate(context, appWidgetManager, appWidgetIds);
    appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
    Log.e("UPDATE", "End");
}

@Override
public void onReceive(Context context, Intent intent) 
{
   super.onReceive(context, intent);
    Log.e("RECEIVE", "Start 2");
    if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) 
    {

            Log.e("Test", "My State is  "state);//it gives me null point exception;

    }
   Log.e("RECEIVE", "End");


}

在的onReceive状态varible给出零点异常

state varible in onReceive gives null point exception

推荐答案

AppWidgetReceiver ,第的onReceive()将被调用,然后根据收到的行动,它会调用的OnUpdate(...)方法。所以在这里,你在的OnUpdate(),它会的onReceive()后调用初始化状态,这样的状态是的中的onReceive()。

for a AppWidgetReceiver , first onReceive() will be called and then based on the Action received, it will call onUpdate(...) method. so here you are initializing state in onUpdate() which will be called after onReceive(), thus state is null in onReceive().

这篇关于的OnUpdate()intilized变量是空的Widget类的onReceive的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆