小部件imageview位图错误

时间:2013-02-06 00:01:45

标签: android widget

在模拟器上运行完美,在手机崩溃...... !!  我检查了崩溃的线但我无法理解为什么!请看代码  当我尝试使用while

中填充的位图数组更新视图时,它会崩溃
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 );
    }

以下是获取联系人的uri照片的方法

  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);
    }

这是堆栈跟踪:

  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

1 个答案:

答案 0 :(得分:0)

看起来你用于Randomnumberwidget的paramContext可能是导致NullPointerException的原因。

从我对你的堆栈跟踪的阅读中,看起来问题就在这里: Randomnuberwidget.onUpdate(Randomnuberwidget.java:167)。

尝试将getApplicationContext()传递给paramContext。这意味着无论参数来自哪个转变为paramContext,请确保它是getApplicationContext(),因为有时如果您使用的是更改的其他上下文,则可以在那里获得空指针。

已修改

您可能还希望在使用位图之前检查null。因此,如果位图为null,则使用默认位图。

这样的事情:

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