Android ArrayIndexOutofBounds仅在2部电话上出现异常
我对此应用程序有问题: 我有这个拉波特。在我朋友的电话和模拟器上一切都很好。下面是有问题的raport和class。它是一个负责4x2小部件的类Android ArrayIndexOutofBounds仅在2部电话上出现异常,android,Android,我对此应用程序有问题: 我有这个拉波特。在我朋友的电话和模拟器上一切都很好。下面是有问题的raport和class。它是一个负责4x2小部件的类 public class CountdownService extends Service { public static final String UPDATE = "update"; public static final String PLUS = "plus"; public static final String MINUS = "minu
public class CountdownService extends Service {
public static final String UPDATE = "update";
public static final String PLUS = "plus";
public static final String MINUS = "minus";
public static final long MODIFY= 86400000;
Bitmap bla[]=null;
private static final int kDigitsViewIds[] = {
R.id.bmp0,
R.id.bmp1,
};
@Override
public void onStart(Intent intent, int startId) {
if (bla==null){
getDigits();
}
String command = intent.getAction();
int appWidgetId = intent.getExtras().getInt(
AppWidgetManager.EXTRA_APPWIDGET_ID);
RemoteViews remoteView = new RemoteViews(getApplicationContext()
.getPackageName(), R.layout.countdownwidget);
AppWidgetManager appWidgetManager = AppWidgetManager
.getInstance(getApplicationContext());
SharedPreferences prefs = getApplicationContext().getSharedPreferences(
"prefs", 0);
int year = prefs.getInt("year", 0);
int month =prefs.getInt("month", 0);
int day = prefs.getInt("day", 0);
//plus button pressed
TimeZone tz = TimeZone.getTimeZone("GMT+1:00");
Date date1 = new Date();
//Calendar calendar = new GregorianCalendar(2012, 05, 8, 18 );
Calendar calendar = new GregorianCalendar(year, month, day);
calendar.setTimeZone(tz);
long timme = calendar.getTimeInMillis() - date1.getTime();
int d = (int) ((timme / 1000) / (3600*24));
Bitmap[] bla = getDigits();
String days=String.format("%02d", d);
String time = days;
char[] digits = time.toCharArray();
for (int i = 0; i <= 1; i++) {
char c =digits[i];
int a=Character.getNumericValue(c);
remoteView.setImageViewBitmap(kDigitsViewIds[i], bla[a]);
}
// apply changes to widget
appWidgetManager.updateAppWidget(appWidgetId, remoteView);
super.onStart(intent, startId);
}
public Bitmap[] getDigits(){
if (bla == null){
bla = prepareDigits();
}
return bla;
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
private Bitmap[] prepareDigits() {
int index = 10;
Bitmap[] bla = new Bitmap[index];
for (int i = 0; i < index; i++) {
bla[1] =(Bitmap) BitmapFactory.decodeResource(getResources(), R.drawable.digit1);
bla[2] =(Bitmap) BitmapFactory.decodeResource(getResources(), R.drawable.digit2);
bla[3] =(Bitmap) BitmapFactory.decodeResource(getResources(), R.drawable.digit3);
bla[4] =(Bitmap) BitmapFactory.decodeResource(getResources(), R.drawable.digit4);
bla[5] =(Bitmap) BitmapFactory.decodeResource(getResources(), R.drawable.digit5);
bla[6] =(Bitmap) BitmapFactory.decodeResource(getResources(), R.drawable.digit6);
bla[7] =(Bitmap) BitmapFactory.decodeResource(getResources(), R.drawable.digit7);
bla[8] =(Bitmap) BitmapFactory.decodeResource(getResources(), R.drawable.digit8);
bla[9] =(Bitmap) BitmapFactory.decodeResource(getResources(), R.drawable.digit9);
bla[0] =(Bitmap) BitmapFactory.decodeResource(getResources(), R.drawable.digit0);
}
return bla;
}
}
我现在知道这是怎么回事了
int year = prefs.getInt("year", 0);
int month =prefs.getInt("month", 0);
int day = prefs.getInt("day", 0);
我的默认值是0,数据是0.0.0
private Bitmap[] prepareDigits() {
int index = 10;
Bitmap[] bla = new Bitmap[index];
for (int i = 0; i < index; i++) {
bla[1] =(Bitmap) BitmapFactory.decodeResource(getResources(), R.drawable.digit1);
bla[2] =(Bitmap) BitmapFactory.decodeResource(getResources(), R.drawable.digit2);
bla[3] =(Bitmap) BitmapFactory.decodeResource(getResources(), R.drawable.digit3);
bla[4] =(Bitmap) BitmapFactory.decodeResource(getResources(), R.drawable.digit4);
bla[5] =(Bitmap) BitmapFactory.decodeResource(getResources(), R.drawable.digit5);
bla[6] =(Bitmap) BitmapFactory.decodeResource(getResources(), R.drawable.digit6);
bla[7] =(Bitmap) BitmapFactory.decodeResource(getResources(), R.drawable.digit7);
bla[8] =(Bitmap) BitmapFactory.decodeResource(getResources(), R.drawable.digit8);
bla[9] =(Bitmap) BitmapFactory.decodeResource(getResources(), R.drawable.digit9);
bla[0] =(Bitmap) BitmapFactory.decodeResource(getResources(), R.drawable.digit0);
}
return bla;
}
}
就个人而言,我认为你应该使用
列表
。数组是5年前的事了。哪一行是CountdownService.java的第72行?这是最好的。。。第72行是空的…哈哈,谢谢,我从24digitalclock得到这个代码,我不知道它是怎么工作的。我纠正它:)
private Bitmap[] prepareDigits() {
int index = 10;
Bitmap[] bla = new Bitmap[index];
for (int i = 0; i < index; i++) {
bla[1] =(Bitmap) BitmapFactory.decodeResource(getResources(), R.drawable.digit1);
bla[2] =(Bitmap) BitmapFactory.decodeResource(getResources(), R.drawable.digit2);
bla[3] =(Bitmap) BitmapFactory.decodeResource(getResources(), R.drawable.digit3);
bla[4] =(Bitmap) BitmapFactory.decodeResource(getResources(), R.drawable.digit4);
bla[5] =(Bitmap) BitmapFactory.decodeResource(getResources(), R.drawable.digit5);
bla[6] =(Bitmap) BitmapFactory.decodeResource(getResources(), R.drawable.digit6);
bla[7] =(Bitmap) BitmapFactory.decodeResource(getResources(), R.drawable.digit7);
bla[8] =(Bitmap) BitmapFactory.decodeResource(getResources(), R.drawable.digit8);
bla[9] =(Bitmap) BitmapFactory.decodeResource(getResources(), R.drawable.digit9);
bla[0] =(Bitmap) BitmapFactory.decodeResource(getResources(), R.drawable.digit0);
}
return bla;
}
}
Bitmap[] bla = new Bitmap[index];
bla[0] = ((BitmapDrawable)getResources().getDrawable(R.drawable.digit0)).getBitmap();
bla[1] = ((BitmapDrawable)getResources().getDrawable(R.drawable.digit1)).getBitmap();
bla[2] = ((BitmapDrawable)getResources().getDrawable(R.drawable.digit2)).getBitmap();
// etc