基本上,当我单击小部件上的特定按钮时,我想获取数据库中的下一条记录。当我在应用程序中使用它时,该功能本身可以正常工作。但是在小部件中它崩溃了。我认为问题出在 onReceive 函数本身,即使我只是将 onReceive 设置为更新文本视图,它也会作为 nullpointerexception 崩溃。
公共静态字符串 NEXT_RECORD = "next_record";
这是它被调用的地方:
Intent intent = new Intent(context, MyWidgetProvider.class);
意图.setAction(NEXT_RECORD); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); remoteViews.setOnClickPendingIntent(R.id.TextView02, pendingIntent);
并收到
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if(NEXT_RECORD.equals(intent.getAction())){
newid = mDbHelper.getNextRecord(1, keyid);
}
}
04-12 14:47:57.591: E/AndroidRuntime(30633): FATAL EXCEPTION: main
04-12 14:47:57.591: E/AndroidRuntime(30633): java.lang.RuntimeException: Unable to start receiver com.example.app.hs.MyWidgetProvider: java.lang.NullPointerException
04-12 14:47:57.591: E/AndroidRuntime(30633): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2362)
04-12 14:47:57.591: E/AndroidRuntime(30633): at android.app.ActivityThread.access$1500(ActivityThread.java:142)
04-12 14:47:57.591: E/AndroidRuntime(30633): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284)
04-12 14:47:57.591: E/AndroidRuntime(30633): at android.os.Handler.dispatchMessage(Handler.java:99)
04-12 14:47:57.591: E/AndroidRuntime(30633): at android.os.Looper.loop(Looper.java:137)
04-12 14:47:57.591: E/AndroidRuntime(30633): at android.app.ActivityThread.main(ActivityThread.java:4931)
04-12 14:47:57.591: E/AndroidRuntime(30633): at java.lang.reflect.Method.invokeNative(Native Method)
04-12 14:47:57.591: E/AndroidRuntime(30633): at java.lang.reflect.Method.invoke(Method.java:511)
04-12 14:47:57.591: E/AndroidRuntime(30633): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
04-12 14:47:57.591: E/AndroidRuntime(30633): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
04-12 14:47:57.591: E/AndroidRuntime(30633): at dalvik.system.NativeStart.main(Native Method)
04-12 14:47:57.591: E/AndroidRuntime(30633): Caused by: java.lang.NullPointerException
04-12 14:47:57.591: E/AndroidRuntime(30633): at com.example.app.hs.MyWidgetProvider.onReceive(MyWidgetProvider.java:100)