我有一个显示运行时项目列表的应用程序。当用户按下 aButton
时,所有选择的项目都应该被删除(选择由 决定CheckBox
)。在纵向模式下一切正常,但是当我在行数超过 3时切换到横向模式时,按下删除按钮会得到Force Close Exception
并且我无法打印堆栈跟踪。当有 3 行或更少行时,它可以完美运行!
我已经为layout
(纵向)和layout-land
目录编写了布局。代码和布局如下:
这是删除按钮的处理程序:
Button DeleteGoods=(Button)findViewById(R.id.DeleteSelectedGoodsButton);
DeleteGoods.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
CheckBox cbx = (CheckBox)listView.findViewById(R.id.ChkOrder);
int firstPosition = listView.getFirstVisiblePosition();
for (int i = firstPosition; i < listView.getCount(); i++) {
View v1 = listView.getChildAt(i);
cbx = (CheckBox)v1.findViewById(R.id.ChkOrder);
if(cbx.isChecked())
checkedItemPosition=i;
}
try {
DeleteCheckedItem(checkedItemPosition);
Toast.makeText(getApplicationContext(),"The selected order has been deleted successfully.", Toast.LENGTH_SHORT).show();
finish();
startActivity(getIntent());
} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
}
}
});
这是横向模式下的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000" >
<Button
android:id="@+id/DeleteSelectedGoodsButton"
android:layout_width="150dp"
android:layout_height="45dp"
android:text="@string/DeleteSelectedGoods"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true" />
<Button
android:id="@+id/ConfirmDelete"
android:layout_width="150dp"
android:layout_height="45dp"
android:text="@string/ConfirmDeleteButton"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:id="@+id/CancelButton"
android:layout_width="150dp"
android:layout_height="45dp"
android:text="@string/CancelButton"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true" />
<TextView
android:id="@+id/aa"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="@id/CancelButton" />
<TextView
android:id="@+id/GOODNAME_CELL"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:background="@drawable/border"
android:textSize="18dp"
android:text="Order"
android:paddingLeft="5dp"
android:layout_below="@id/aa"
android:layout_alignParentLeft="true" />
<TextView
android:id="@+id/GOODUNITPRICE_CELL"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:background="@drawable/border"
android:textSize="18dp"
android:text="Unit Price"
android:paddingLeft="5dp"
android:layout_below="@id/aa"
android:layout_toRightOf="@id/GOODNAME_CELL" />
<TextView
android:id="@+id/QUANTITYCELL"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="@drawable/border"
android:textSize="18dp"
android:text="Quantity"
android:paddingLeft="5dp"
android:layout_below="@id/aa"
android:layout_toRightOf="@id/GOODUNITPRICE_CELL" />
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="@style/CodeFont"
android:layout_alignParentLeft="true"
android:layout_below="@id/GOODUNITPRICE_CELL" />
<TextView
android:id="@+id/TotalPriceTextview"
android:layout_width="600dp"
android:layout_height="wrap_content"
android:text=""
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@drawable/back"
android:textColor="#FFFFFF"
android:textSize="20dp" />
</RelativeLayout>
这是我得到的例外:
02-19 10:07:11.398: E/AndroidRuntime(4335): FATAL EXCEPTION: main
02-19 10:07:11.398: E/AndroidRuntime(4335): java.lang.NullPointerException
02-19 10:07:11.398: E/AndroidRuntime(4335): at com.example.nfc.PrepopSqliteDbActivity$1.onClick(PrepopSqliteDbActivity.java:117)
02-19 10:07:11.398: E/AndroidRuntime(4335): at android.view.View.performClick(View.java:2552)
02-19 10:07:11.398: E/AndroidRuntime(4335): at android.view.View$PerformClick.run(View.java:9229)
02-19 10:07:11.398: E/AndroidRuntime(4335): at android.os.Handler.handleCallback(Handler.java:587)
02-19 10:07:11.398: E/AndroidRuntime(4335): at android.os.Handler.dispatchMessage(Handler.java:92)
02-19 10:07:11.398: E/AndroidRuntime(4335): at android.os.Looper.loop(Looper.java:138)
02-19 10:07:11.398: E/AndroidRuntime(4335): at android.app.ActivityThread.main(ActivityThread.java:3701)
02-19 10:07:11.398: E/AndroidRuntime(4335): at java.lang.reflect.Method.invokeNative(Native Method)
02-19 10:07:11.398: E/AndroidRuntime(4335): at java.lang.reflect.Method.invoke(Method.java:507)
02-19 10:07:11.398: E/AndroidRuntime(4335): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
02-19 10:07:11.398: E/AndroidRuntime(4335): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
02-19 10:07:11.398: E/AndroidRuntime(4335): at dalvik.system.NativeStart.main(Native Method)
02-19 10:07:11.408: W/ActivityManager(1768): Force finishing activity com.example.nfc/.PrepopSqliteDbActivity
02-19 10:07:11.909: W/ActivityManager(1768): Activity pause timeout for HistoryRecord{40b2ed78 com.example.nfc/.PrepopSqliteDbActivity}
02-19 10:07:16.353: I/InputDispatcher(1768): Application is not responding: Window{40a73e60 com.example.nfc/com.example.nfc.PrepopSqliteDbActivity paused=false}. 5004.7ms since event, 5004.4ms since wait started
02-19 10:07:16.353: I/InputDispatcher(1768): Dropping event because the pointer is not down.
02-19 10:07:16.353: I/InputDispatcher(1768): Dropping event because the pointer is not down.
02-19 10:07:16.353: I/WindowManager(1768): Input event dispatching timed out sending to com.example.nfc/com.example.nfc.PrepopSqliteDbActivity
02-19 10:07:16.353: I/ActivityManager(1768): Crashing app skipping ANR: ProcessRecord{40a1b188 4335:com.example.nfc/10003} keyDispatchingTimedOut
02-19 10:07:21.428: W/ActivityManager(1768): Launch timeout has expired, giving up wake lock!
02-19 10:07:21.908: W/ActivityManager(1768): Activity idle timeout for HistoryRecord{40a1b7d8 com.example.nfc/.Buy}
02-19 10:07:26.963: I/touchd(1518): 'ab8500_usb' is online
02-19 10:07:26.963: I/touchd(1518): handling_uevent: event is 1
编辑
这是第 117 行:
DeleteCheckedItem(checkedItemPosition);
这是方法的主体:
private void DeleteCheckedItem(int chkPosition)
{
String strName=goods.get(chkPosition).get(GOOD_NAME);
String strPrice=goods.get(chkPosition).get(GOOD_UNITPRICE);
String strQuantity=goods.get(chkPosition).get(QUANTITY);
String whereClause = "GOOD_NAME = ? AND GOOD_UNITPRICE= ? AND QUANTITY= ?";
String[] whereArgs = {strName,strPrice,strQuantity};
database.delete(TABLE_NAME, whereClause, whereArgs);
}