0

在模拟器上完美运行,在手机崩溃......!我检查了崩溃的线路,但我不知道为什么!请查看代码当我尝试使用填充的位图数组更新视图时它会崩溃

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
4

1 回答 1

0

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

从我对您的堆栈跟踪的阅读来看,问题似乎出在此处:Randomnuberwidget.onUpdate(Randomnuberwidget.java:167)。

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

编辑

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

像这样的东西:

    Bitmap bit= loadContactPhoto(paramContext.getContentResolver(),Long.parseLong(idContact));
    if (idContact.compareTo("") != 0 && bit != null)  {
        sms_rubrica.add(bit);
    }
于 2013-02-06T00:22:01.820 回答