简体   繁体   English

Android小部件onReceive崩溃

[英]Android widget onReceive crashing

Basically when I click a particular button on the widget I want to fetch the next record in the database. 基本上,当我单击窗口小部件上的特定按钮时,我想获取数据库中的下一条记录。 The function itself works fine as I use it in the app. 当我在应用程序中使用该功能时,该功能本身可以正常工作。 But in the widget it's crashing. 但是在小部件中它崩溃了。 I'm thinking it's the onReceive function itself that is the problem though as even I just set the onReceive to update a textview it crashes as nullpointerexception. 我以为是onReceive函数本身就是问题所在,尽管即使我只是将onReceive设置为更新文本视图,它也会由于nullpointerexception崩溃而崩溃。

public static String NEXT_RECORD = "next_record"; 公共静态字符串NEXT_RECORD =“ next_record”;

Here is where it gets called: 这是它的调用位置:

Intent intent = new Intent(context, MyWidgetProvider.class); Intent intent = new Intent(上下文,MyWidgetProvider.class);
intent.setAction(NEXT_RECORD); intent.setAction(NEXT_RECORD); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); PendingIntentendingIntent = PendingIntent.getBroadcast(context,0,intent,PendingIntent.FLAG_UPDATE_CURRENT); remoteViews.setOnClickPendingIntent(R.id.TextView02, pendingIntent); remoteViews.setOnClickPendingIntent(R.id.TextView02,endingIntent);

and onreceive 并在收到

  @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)

If newid = mDbHelper.getNextRecord(1, keyid); 如果newid = mDbHelper.getNextRecord(1, keyid); throws this NPE then mDbHelper is null. 抛出该NPE,则mDbHelper为null。 Simply instantiate mDbHelper before trying to read from it: 只需实例化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);
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM