2013-02-06 36 views
0

在模擬器上完美工作,在電話崩潰...! 我檢查了那條墜毀的線路,但是我不明白爲什麼!看到代碼,請 它會崩潰當我試圖更新視圖與位圖陣列填補了同時小部件imageview位圖錯誤

public static final ArrayList<Bitmap> sms_rubrica = new ArrayList<Bitmap>(); 
    public static Bitmap No_Foto = null; 

    public void onUpdate(Context paramContext, AppWidgetManager appWidgetManager, 
      int[] appWidgetIds){ 

     ComponentName watchWidget; 
     rview = new RemoteViews(paramContext.getPackageName(), R.layout.widget_layoutmain); 
     watchWidget = new ComponentName(paramContext, Randomnuberwidget.class); 

     Uri uriSMSURI = Uri.parse("content://sms/inbox"); 

     Cursor cur = paramContext.getContentResolver().query(uriSMSURI, null, null, null, "date desc limit 3"); 

     //Default photo 

     No_Foto = BitmapFactory.decodeResource(paramContext.getResources(), R.drawable.no_photo); 




     while (cur.moveToNext()) { 

      String address = cur.getString(cur.getColumnIndex("address")); 
      String body = cur.getString(cur.getColumnIndexOrThrow("body")); 
      String id; 
      String date; 
      int idColumn = cur.getColumnIndex("_id"); 
      int dateColumn = cur.getColumnIndex("date"); 

      id = cur.getString(idColumn); 
      date = cur.getString(dateColumn); 


     Date dateFromSms = new Date(Long.parseLong(date)); 

      Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(address)); 
      Cursor cs= paramContext.getContentResolver().query(uri, new String[]{PhoneLookup.DISPLAY_NAME},PhoneLookup.NUMBER+"='"+address+"'",null,null); 

      String idContact = fetchContactIdFromPhoneNumber(address, paramContext); 

      //Add photo to array 
      if (idContact.compareTo("") != 0) { 
        Bitmap bit= loadContactPhoto(paramContext.getContentResolver(),Long.parseLong(idContact)); 
        sms_rubrica.add(bit); 
       } 
      else sms_rubrica.add(No_Foto); 

      if(cs.getCount()>0) 
      { 
      cs.moveToFirst(); 
      address=cs.getString(cs.getColumnIndex(PhoneLookup.DISPLAY_NAME)); 
      } 

      sms_num.add(address); 
      sms_body.add(body); 
      sms_id.add(id); 
      sms_time.add(dateFromSms.toLocaleString()); 


     } 


     Bitmap photo = sms_rubrica.get(0); 

     rview.setTextViewText(R.id.Mittente,sms_num.get(0)); 
     rview.setTextViewText(R.id.TestoSms, sms_body.get(0)); 
     rview.setTextViewText(R.id.Orario, sms_time.get(0)); 
      //Here's on telephone crash giving null pointer exception but not in the emulator 
     rview.setImageViewBitmap(R.id.imgRubrica, photo); 


     appWidgetManager.updateAppWidget(watchWidget, rview); 
    } 

下面就來獲得接觸

public static Bitmap loadContactPhoto(ContentResolver cr, long id) { 
      Uri uri =    ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id); 
      InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri); 

      if (input == null) { 
       return null; 
      } 
      return BitmapFactory.decodeStream(input); 
    } 

這裏的URI照片的方法堆棧跟蹤:

02-06 01:11:44.289: E/AndroidRuntime(8624): FATAL EXCEPTION: main 
    02-06 01:11:44.289: E/AndroidRuntime(8624): java.lang.RuntimeException: Unable to start receiver com.imrankhanandroid.HomeWidgetRandomNum.Randomnuberwidget: java.lang.NullPointerException 
    02-06 01:11:44.289: E/AndroidRuntime(8624): at android.app.ActivityThread.handleReceiver(ActivityThread.java:1809) 
    02-06 01:11:44.289: E/AndroidRuntime(8624):  at android.app.ActivityThread.access$2400(ActivityThread.java:117) 
    02-06 01:11:44.289: E/AndroidRuntime(8624): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:985) 
    02-06 01:11:44.289: E/AndroidRuntime(8624): at android.os.Handler.dispatchMessage(Handler.java:99) 
    02-06 01:11:44.289: E/AndroidRuntime(8624):  at android.os.Looper.loop(Looper.java:130) 
    02-06 01:11:44.289: E/AndroidRuntime(8624):  at android.app.ActivityThread.main(ActivityThread.java:3687) 
     02-06 01:11:44.289: E/AndroidRuntime(8624): at java.lang.reflect.Method.invokeNative(Native Method) 
    02-06 01:11:44.289: E/AndroidRuntime(8624):  at java.lang.reflect.Method.invoke(Method.java:507) 
    02-06 01:11:44.289: E/AndroidRuntime(8624):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842) 
    02-06 01:11:44.289: E/AndroidRuntime(8624): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 
    02-06 01:11:44.289: E/AndroidRuntime(8624):  at dalvik.system.NativeStart.main(Native Method) 
    02-06 01:11:44.289: E/AndroidRuntime(8624): Caused by: java.lang.NullPointerException 
    02-06 01:11:44.289: E/AndroidRuntime(8624):  at android.widget.RemoteViews$ReflectionAction.writeToParcel(RemoteViews.java:730) 
    02-06 01:11:44.289: E/AndroidRuntime(8624): at android.widget.RemoteViews.writeToParcel(RemoteViews.java:1376) 
    02-06 01:11:44.289: E/AndroidRuntime(8624): at com.android.internal.appwidget.IAppWidgetService$Stub$Proxy.updateAppWidgetProvider(IAppWidgetService.java:402) 
    02-06 01:11:44.289: E/AndroidRuntime(8624):  at android.appwidget.AppWidgetManager.updateAppWidget(AppWidgetManager.java:283) 
    02-06 01:11:44.289: E/AndroidRuntime(8624):  at com.imrankhanandroid.HomeWidgetRandomNum.Randomnuberwidget.onUpdate(Randomnuberwidget.java:167) 
     02-06 01:11:44.289: E/AndroidRuntime(8624): at android.appwidget.AppWidgetProvider.onReceive(AppWidgetProvider.java:61) 
    02-06 01:11:44.289: E/AndroidRuntime(8624):  at com.imrankhanandroid.HomeWidgetRandomNum.Randomnuberwidget.onReceive(Randomnuberwidget.java:205) 
    02-06 01:11:44.289: E/AndroidRuntime(8624): at android.app.ActivityThread.handleReceiver(ActivityThread.java:1798) 
    02-06 01:11:44.289: E/AndroidRuntime(8624): ... 10 more 
+0

附加堆棧跟蹤。 –

回答

0

看起來你正在使用Randomnumberwidget可能是什麼原因造成的NullPointerException異常的paramContext。

從我閱讀你的堆棧跟蹤,它看起來像問題在這裏: Randomnuberwidget.onUpdate(Randomnuberwidget.java:167)。

嘗試將getApplicationContext()傳遞給paramContext。這意味着無論參數來自哪裏,都會變成paramContext,請確保它是getApplicationContext(),因爲有時如果您使用的是不同的上下文,那麼您可以在那裏獲得空指針。

編輯

您可能要還檢查空使用位圖前。因此,如果位圖爲空,則使用默認位圖。

事情是這樣的:

Bitmap bit= loadContactPhoto(paramContext.getContentResolver(),Long.parseLong(idContact)); 
    if (idContact.compareTo("") != 0 && bit != null) { 
     sms_rubrica.add(bit); 
    } 
+0

它是自動生成方法,並在代碼上標記錯誤行 – user1873662

+0

是錯誤行位於堆棧跟蹤中。但是你沒有在你的代碼中包含行號。 –

+0

我在哪裏必須通過GetApplicationContext()而不是paramContext? – user1873662