onReceive中的Android小部件remoteviews空值

onReceive中的Android小部件remoteviews空值,android,android-widget,Android,Android Widget,我是android编程新手,我花了几个小时试图弄明白这一点,但仍然无法让它工作。我正在创建一个有两个按钮的小部件。一个按钮可以正常工作(调用一个活动的按钮),但另一个用来更改图片的按钮(如切换键)不起作用。我得到一个NullPointerException。我已经跟踪了这个问题,当它被传递到onReceive时,RemoteView似乎是空的 public class Widget extends AppWidgetProvider { boolean status = false; priva

我是android编程新手,我花了几个小时试图弄明白这一点,但仍然无法让它工作。我正在创建一个有两个按钮的小部件。一个按钮可以正常工作(调用一个活动的按钮),但另一个用来更改图片的按钮(如切换键)不起作用。我得到一个
NullPointerException
。我已经跟踪了这个问题,当它被传递到onReceive时,RemoteView似乎是空的

public class Widget extends AppWidgetProvider {
boolean status = false;
private RemoteViews remoteViews;
Button bMain;

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

    final int n = appWidgetIds.length;
    for(int i = 0; i < n; i++){
        final int appWidgetId = appWidgetIds[i];

        remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);

        Intent configIntent = new Intent(context, Main.class);
        configIntent.setAction("callingActivity");
        PendingIntent configPendingIntent = PendingIntent.getActivity(context,
                0, configIntent, 0);
        remoteViews.setOnClickPendingIntent(R.id.bWidgetMain,
                configPendingIntent);

        configIntent = new Intent(context, Widget.class);
        configIntent.setAction("callingibFlash");
        configPendingIntent = PendingIntent.getBroadcast(context, 0,
                configIntent, 0);
        remoteViews.setOnClickPendingIntent(R.id.ibFlash, configPendingIntent);

        appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
    }

}

@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals("callingActivity")) {
        Toast.makeText(context, "Main", Toast.LENGTH_SHORT).show();
    } else if (intent.getAction().equals("callingibFlash")) {
        remoteViews.setImageViewResource(R.id.ibFlash,
                R.drawable.flashlight_on);
    } else {
        super.onReceive(context, intent);
    }
}

@Override
public void onDeleted(Context context, int[] appWidgetIds) {
    super.onDeleted(context, appWidgetIds);
}
} 
公共类小部件扩展了AppWidgetProvider{
布尔状态=假;
私有远程视图远程视图;
按钮B主;
@凌驾
public void onUpdate(上下文上下文,AppWidgetManager AppWidgetManager,
int[]appWidgetIds){
onUpdate(上下文、appWidgetManager、AppWidgetId);
final int n=appWidgetIds.length;
对于(int i=0;i
我建议将
remoteview
作为局部变量,而不是将引用作为实例变量。您可以通过
Context
onReceive()
实现中获得一个新的引用,并使用
AppWidgetManager
像在
onUpdate()中一样更新小部件

@覆盖
公共void onReceive(上下文、意图){
if(intent.getAction().equals(“callingActivity”)){
Toast.makeText(上下文,“Main”,Toast.LENGTH_SHORT).show();
}else if(intent.getAction().equals(“callingibFlash”)){
ComponentName thisWidget=新的ComponentName(上下文,Widget.class);
AppWidgetManager AppWidgetManager=AppWidgetManager.getInstance(上下文);
int[]appWidgetIds=appWidgetManager.getAppWidgetIds(thisWidget);
MyRefactureUpdate(上下文、appWidgetManager、AppWidgetId);
//在onUpdate中使用for循环以访问RemoteView
final int n=appWidgetIds.length;
对于(int i=0;i

一旦有了新的RemoteView,您就可以调用setImageViewResource()和其他任何您想要的功能。对于每种情况,最好都调用super.onReceive

这是更新的问题onReceive@NanoControl:是的,我也是这样理解的。答案有用吗?或者你还有其他问题吗?为了澄清这一点,您可以在
appWidgetIds
上使用类似的for循环来访问
remoteview
,类似于
onUpdate
中的for循环。好的,我在原始代码的基础上添加了一个更具体的代码示例。希望能把事情弄清楚。
@Override
public void onReceive(Context context, Intent intent){
    if (intent.getAction().equals("callingActivity")) {
        Toast.makeText(context, "Main", Toast.LENGTH_SHORT).show();
    } else if (intent.getAction().equals("callingibFlash")) {
        ComponentName thisWidget = new ComponentName(context, Widget.class);
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
        int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
        myRefactoredUpdate(context, appWidgetManager, appWidgetIds);
        // use for loop as in onUpdate to get to your remoteViews
        final int n = appWidgetIds.length;
        for (int i = 0; i < n; i++) {
            final int appWidgetId = appWidgetIds[i];
            remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
            remoteViews.setImageViewResource(R.id.ibFlash, R.drawable.flashlight_on);
        }
    } else {
        super.onReceive(context, intent);
    }
}