簡體   English   中英

Android小部件onReceive崩潰

[英]Android widget onReceive crashing

基本上,當我單擊窗口小部件上的特定按鈕時,我想獲取數據庫中的下一條記錄。 當我在應用程序中使用該功能時,該功能本身可以正常工作。 但是在小部件中它崩潰了。 我以為是onReceive函數本身就是問題所在,盡管即使我只是將onReceive設置為更新文本視圖,它也會由於nullpointerexception崩潰而崩潰。

公共靜態字符串NEXT_RECORD =“ next_record”;

這是它的調用位置:

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

並在收到

  @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為null。 只需實例化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