简体   繁体   English

在横向模式下强制关闭,但在纵向中不强制关闭

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

I have an application which shows a list of items in runtime. 我有一个应用程序,它显示运行时的项目列表。 When the user presses a Button , all selected items should be deleted (selection determined by CheckBox ). 当用户按下Button ,应删除所有选定的项目(选择由CheckBox确定)。 In portrait mode everything works well, but when I switch to landscape mode when the number of rows are more than 3 , pressing the delete button gets the Force Close Exception and I am unable to print the stack trace. 纵向模式下一切正常,但是当行数大于3时切换到横向模式 ,按下删除按钮会获得Force Close Exception ,我无法打印堆栈跟踪。 When there are 3 or less rows it works perfectly! 如果有3行或更少行,它可以完美运行! I've written the layout for both layout (portrait) and layout-land directories. 我已经为layout (纵向)和layout-land目录编写了布局。 Code and Layouts are as follows: 代码和布局如下:

Here is the handler for Delete Button: 这是删除按钮的处理程序:

   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();
                }


        }

    });

Here is the layout in landscape mode: 这是横向模式下的布局:

<?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>

This is the exception I am getting: 这是我得到的例外:

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

Edit 编辑

This is line 117: 这是第117行:

DeleteCheckedItem(checkedItemPosition);

and this is the method's body: 这是方法的主体:

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);
}

Is checkedItemPosition an int or an Integer ? checkedItemPositionint还是Integer

My guess is its an Integer that was initialized to null . 我的猜测是它被初始化为nullInteger

When you rotate, the activity is recreated and your checkedItemPosition was reinitialized to null . 旋转时,将重新创建活动,并将checkedItemPosition重新初始化为null

Then you enter your for-loop, but none of the Checkbox views were checked so your checkedItemPosition is still null . 然后输入for循环,但没有选中Checkbox视图,因此checkedItemPosition仍然为null

You try to call DeleteItemChecked on a null while it expects a primitive int and voila: NullPointerException . 您尝试在null上调用DeleteItemChecked ,而它期望一个原始int和voila: NullPointerException

Solution: initialize checkedItemPosition to 0 . 解决方案:将checkedItemPosition初始化为0

I encountered this kind of problem. 我遇到过这种问题。 My theory is related to the observation of Shadi 我的理论与沙迪的观察有关

Portrait mode can display more rows than in landscape mode. 纵向模式可以显示比横向模式更多的行。 And since you iterate the listview item count and try to get the view on the said position. 因为您迭代listview项目计数并尝试获取所述位置的视图。 If the view or item of the listview is not visible in the window/phone screen. 如果列表视图的视图或项目在窗口/电话屏幕中不可见。 There is possibility that you can get null values. 您可能会获得空值。 getView() method of an adapter is called every scrolling, since initialization or recreating listview items is in getView() without proper calling in getView() method items that are not visible in window/phone screen is not initialized. 每次滚动都会调用适配器的getView()方法,因为初始化或重新创建listview项是在getView()中而没有在getView()中正确调用的方法,在窗口/电话屏幕中看不到的项目未初始化。 You can use the method in listview: getLastVisiblePosition() to limit the number of item you will iterate to the listview. 您可以使用listview中的方法: getLastVisiblePosition()来限制您将迭代到列表视图的项目数。

Hope this will help you. 希望这会帮助你。 Regards. 问候。

It looks like either database or TABLE_NAME is null . 看起来databaseTABLE_NAME都是null Add these lines before line 117 to test: 在第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");

Got the problem and the solution is as below. 遇到问题,解决方法如下。 when you rotate the screen the Adapter is getting initialized again and the previous array is getting cleaned. 当您旋转屏幕时,适配器将再次初始化,并且前一个阵列将被清除。 what you can do is put the onConfigurationChange() in the activity and try to check the output. 你可以做的是将onConfigurationChange()放在活动中并尝试检查输出。

Looking at your code, what happens if delete is pressed but nothing is checked? 查看您的代码,如果按下删除但没有检查任何内容会发生什么?

Getting to your question, is it possible that the checked item is not visible once delete is pressed? 回答你的问题,按下删除后,是否可能看不到选中的项目? If so, because of View recycling (a scary, if clever, thing), it might not actually exist when you get to the delete button code, so you need to be careful about querying views at that point. 如果是这样,由于View回收(一个可怕的,如果聪明的东西),当你到达删除按钮代码时它可能实际上不存在,所以你需要注意那时查询视图。 It might be wiser to build up a "current selected list" as selections are made (and unmade) so that when delete is pressed you no longer have to query any views. 建立“当前选择列表”可能更明智,因为选择是(并且未制作),因此当按下删除时,您不再需要查询任何视图。

As an aside, it also looks like it will only delete the first checked item, but I may have missed something there. 顺便说一下,它看起来也只会删除第一个检查项目,但我可能错过了那里的东西。

Have you specified the handling of config changes in the Manifest file, if not please do so by adding: android:configChanges="keyboardHidden|orientation to your activity. 你有没有在清单文件中指定配置更改的处理,如果没有,请通过添加: android:configChanges="keyboardHidden|orientation to你的活动来实现。

Please paste logcat, that will provide more info.. Good luck.. 请粘贴logcat,这将提供更多信息..祝你好运..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 数据丢失并在横向和纵向模式下强制关闭android - data lose and force close in landscape and portrait mode android 将人像旋转到风景时,我要关闭力量,我尝试了android:configChanges =“ keyboard | keyboardHidden”并以人像模式工作吗? - Getting Force Close when rotating portrait to landscape and I tried android:configChanges=“keyboard|keyboardHidden” and working in portrait mode? 从纵向更改为横向时强制关闭错误 - Force Close error when changing from portrait to landscape 根据屏幕大小在Android中强制纵向或横向模式 - Force portrait or landscape mode in android depending on screen size 屏幕方向被强制设置为纵向,但倾斜时活动仍将旋转为横向! 如何强制人像模式? - Screen orientation forced to portrait, but still, the activities will rotate to landscape when tilted!! How to force portrait mode? 手机为纵向模式,平板电脑为纵向+横向 - Portrait mode for Phones and Portrait+Landscape for Tablets 相应地更改为风景/人像模式 - To change accordingly to landscape/portrait mode 纵向模式下的Android Landscape VideoView - Android Landscape VideoView in portrait mode 适用于Android应用程序的横向和纵向模式 - Landscape and portrait mode for android application 横向和纵向定向模式问题 - landscape and portrait orientation mode issue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM