エミュレータでは完全に動作し、電話機のクラッシュでは... !! クラッシュする行を確認しましたが、理由がわかりません!! コードを参照してください。その間にビットマップの配列を入力してビューを更新しようとするとクラッシュします。
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);
}
スタック トレースは次のとおりです。
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