Android小部件onReceive崩溃

基本上,当我单击小部件上的特定按钮时,我想获取数据库中的下一条记录。 该功能本身正常工作,因为我在应用程序中使用它。 但在小部件中,它正在崩溃。 我认为这是onreceive函数本身就是问题,但即使我只是设置onReceive来更新它崩溃的文本视图为nullpointerexception。

public static String NEXT_RECORD =“next_record”;

这是它被调用的地方:

Intent intent = new Intent(context,MyWidgetProvider.class);
intent.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)

Caused by: java.lang.NullPointerException
    at com.example.app.hs.MyWidgetProvider.onReceive(MyWidgetProvider.java:100)

如果newid = mDbHelper.getNextRecord(1, keyid); 抛出这个NPE然后mDbHelper为空。 在尝试读取mDbHelper之前,先简单地实例化一下:

@Override
public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);
    if (NEXT_RECORD.equals(intent.getAction())) {
        mDbHelper = new DbHelper(context); // I guessed at the constructor's parameters
        newid = mDbHelper.getNextRecord(1, keyid);
    }

}
链接地址: http://www.djcxy.com/p/50647.html

上一篇: Android widget onReceive crashing

下一篇: How to Open Camera with surface holder in class which extends BroadcastReceiver