gpt4 book ai didi

android - AppWidgetManager 在 onUpdate 之外?

转载 作者:行者123 更新时间:2023-11-30 03:12:28 25 4
gpt4 key购买 nike

我有一个简单的小部件,我尝试在 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();
}
}
}
}

}

最佳答案

appWidgetManager 字段在那里为空,因为您从未为其分配值。

关于android - AppWidgetManager 在 onUpdate 之外?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20718207/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com