我有一个listView,它是通过SQL数据库中的自定义cursuror加载的。它用于从不同活动中的主列表中删除项目。每行旁边都有一个复选框,勾选行的复选框后,当用户单击删除该行的删除按钮时,该行中会出现一个隐藏的删除按钮。
在我的HTC Desire HD上运行2.3.5这完美无缺,但我在朋友的手机,Galaxy Note和S3上运行Ice cream sandwhich时没有任何问题。 在运行icecream sandwhich的手机上,如果他们检查每一行的复选框,然后单击最后一行的删除按钮,则应用程序会因 arrayIndexOutOfBounds 错误而崩溃。 该块中出现错误
if ( isChecked )
{
// Make deleteButton appear
RelativeLayout ll = (RelativeLayout) buttonView.getParent();
deleteButton = (Button) ll.findViewById(R.id.deleteButton);
checked[position] = 1;
deleteButton.setVisibility(0);
}
在第98行的checked[position] = 1;
行。
任何人都可以解释为什么会这样吗?
错误日志在下面,我的代码在下面。
java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
at com.Roardog.FidelrFull.DeleteCampaignCustomCursorAdapater$1.onCheckedChanged(DeleteCampaignCustomCursorAdapater.java:98)
at android.widget.CompoundButton.setChecked(CompoundButton.java:140)
at com.Roardog.FidelrFull.DeleteCampaignCustomCursorAdapater.getView(DeleteCampaignCustomCursorAdapater.java:86)
at android.widget.AbsListView.obtainView(AbsListView.java:2403)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1251)
at android.widget.ListView.onMeasure(ListView.java:1162)
at android.view.View.measure(View.java:12912)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1385)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:670)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:563)
at android.view.View.measure(View.java:12912)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at android.view.View.measure(View.java:12912)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1385)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:670)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:563)
at android.view.View.measure(View.java:12912)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2264)
at android.view.View.measure(View.java:12912)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1271)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2678)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4514)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
at dalvik.system.NativeStart.main(Native Method)
我的代码:
public class DeleteCampaignCustomCursorAdapater extends CursorAdapter {
protected static final String TAG = null;
DatabaseHelper myDbHelper;
protected SQLiteDatabase db;
private final Activity context;
private LayoutInflater mInflater;
protected ListView mListView;
private Cursor c;
public static int[] checked;
TextView merchantNameView;
Button deleteButton;
CheckBox selectForDelete;
//ImageView moveButton;
public DeleteCampaignCustomCursorAdapater(Context context, Cursor c) {
super(context, c);
this.context = (Activity) context;
mInflater = LayoutInflater.from(context);
this.c = c;
c.moveToFirst();
initializeChecked();
}
@Override
public void bindView(View view, Context context, final Cursor cursor) {
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return mInflater.inflate(R.layout.delete_campaign_row_layout, null);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
Typeface tf = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Regular.ttf"); //Create Roboto font
c.moveToPosition(position);
myDbHelper = new DatabaseHelper(context); //Links to DatabaseHelper class
if (convertView == null) {
mInflater = context.getLayoutInflater();
convertView = mInflater.inflate(R.layout.delete_campaign_row_layout, null);
}
merchantNameView = (TextView) convertView.findViewById(R.id.deleteMerchantNameView);
deleteButton = (Button) convertView.findViewById(R.id.deleteButton);
selectForDelete = (CheckBox) convertView.findViewById(R.id.deleteCheckBox);
merchantNameView.setText(c.getString(c.getColumnIndex("merchantName")));
merchantNameView.setTypeface(tf);
merchantNameView.setTextColor(Color.parseColor("#000000"));
final int rowID = c.getInt(c.getColumnIndex("_id"));
if (checked[position] == 0) {
selectForDelete.setChecked(false);
} else {
selectForDelete.setChecked(true);
}
selectForDelete.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
// Make deleteButton appear
RelativeLayout ll = (RelativeLayout) buttonView.getParent();
deleteButton = (Button) ll.findViewById(R.id.deleteButton);
checked[position] = 1;
deleteButton.setVisibility(View.VISIBLE);
}
else {
//Hide deleteButton
RelativeLayout ll = (RelativeLayout) buttonView.getParent();
deleteButton = (Button) ll.findViewById(R.id.deleteButton);
//checked[position] = 1;
deleteButton.setVisibility(View.INVISIBLE);
}
}
});
deleteButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
if(rowID > 0) {
//Log.d(TAG, "Button Click. rowID = "+rowID);
myDbHelper.deleteRecordWithID(rowID); //Call deleteRecord on selected row
c.requery(); //requery the cursor
notifyDataSetChanged(); //notify the view the data set has changed
initializeChecked(); // re-set the checked array
}
}
});
return convertView;
}
public void initializeChecked() {
checked = new int[c.getCount()];
int i = 0;
while (i < c.getCount()) {
checked[i] = 0;
i++;
}
}
}