簡體   English   中英

在橫向模式下強制關閉,但在縱向中不強制關閉

[英]Force Close in Landscape Mode, but not in Portrait

我有一個應用程序,它顯示運行時的項目列表。 當用戶按下Button ,應刪除所有選定的項目(選擇由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);
}

checkedItemPositionint還是Integer

我的猜測是它被初始化為nullInteger

旋轉時,將重新創建活動,並將checkedItemPosition重新初始化為null

然后輸入for循環,但沒有選中Checkbox視圖,因此checkedItemPosition仍然為null

您嘗試在null上調用DeleteItemChecked ,而它期望一個原始int和voila: NullPointerException

解決方案:將checkedItemPosition初始化為0

我遇到過這種問題。 我的理論與沙迪的觀察有關

縱向模式可以顯示比橫向模式更多的行。 因為您迭代listview項目計數並嘗試獲取所述位置的視圖。 如果列表視圖的視圖或項目在窗口/電話屏幕中不可見。 您可能會獲得空值。 每次滾動都會調用適配器的getView()方法,因為初始化或重新創建listview項是在getView()中而沒有在getView()中正確調用的方法,在窗口/電話屏幕中看不到的項目未初始化。 您可以使用listview中的方法: getLastVisiblePosition()來限制您將迭代到列表視圖的項目數。

希望這會幫助你。 問候。

看起來databaseTABLE_NAME都是null 在第117行之前添加這些行來測試:

if (strName == null)
    Log.w("Test", "strName is NULL");
if (strPrice == null)
    Log.w("Test", "strPrice is NULL");
if (strQuantity == null)
    Log.w("Test", "strQuantity is NULL");
if (TABLE_NAME == null)
    Log.w("Test", "TABLE_NAME is NULL");
if (whereClause == null)
    Log.w("Test", "whereClause is NULL. WTF!");
if (whereArgs == null)
    Log.w("Test", "whereArgs is NULL. WTF!");
if (database == null)
    Log.w("Test", "database is NULL");

遇到問題,解決方法如下。 當您旋轉屏幕時,適配器將再次初始化,並且前一個陣列將被清除。 你可以做的是將onConfigurationChange()放在活動中並嘗試檢查輸出。

查看您的代碼,如果按下刪除但沒有檢查任何內容會發生什么?

回答你的問題,按下刪除后,是否可能看不到選中的項目? 如果是這樣,由於View回收(一個可怕的,如果聰明的東西),當你到達刪除按鈕代碼時它可能實際上不存在,所以你需要注意那時查詢視圖。 建立“當前選擇列表”可能更明智,因為選擇是(並且未制作),因此當按下刪除時,您不再需要查詢任何視圖。

順便說一下,它看起來也只會刪除第一個檢查項目,但我可能錯過了那里的東西。

你有沒有在清單文件中指定配置更改的處理,如果沒有,請通過添加: android:configChanges="keyboardHidden|orientation to你的活動來實現。

請粘貼logcat,這將提供更多信息..祝你好運..

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM