我在 onUpdate() 方法中初始化一个变量,然后调用 onReceive() 函数,该函数运行良好但无法访问 onUpdate() 方法中的变量集。这是为什么?这些变量是字符串变量并且被声明为公共的。我错过了什么吗?
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 中的状态变量给出空点异常