1

我希望有人有一个想法,因为这个问题让我忘记了。

在我的应用程序中,我有一个 Activity,其中包含一个片段,用于将新联系人插入到具有以下布局的地址簿中:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frag_contact_add_linearLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#dddddd"   
android:orientation="vertical">

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginTop="22dp"
    android:layout_marginBottom="52dp"
    android:isScrollContainer="true"   
    android:fillViewport="true"
    >      
    <LinearLayout 
        android:id="@+id/frag_contact_add_contact_scroll_layout"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:background="#dddddd"             
        android:orientation="vertical"
        > 
        <LinearLayout 
            android:id="@+id/frag_contact_add_contact_info"
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:orientation="horizontal"
            android:layout_marginLeft="12dp"        
            android:layout_marginRight="12dp" 
            android:layout_marginTop="8dp"
            android:paddingTop="10dp"       
            android:paddingBottom="10dp"       
            android:background="@drawable/contacts_lv_phone_number_style"        
            > 
            <LinearLayout 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content" 
                android:orientation="vertical"
                android:layout_marginLeft="0dp">          
                <ImageView 
                    android:id="@+id/frag_contact_add_avatar" 
                    android:layout_width="120dp" 
                    android:layout_height="120dp" 
                    android:layout_centerInParent="true" 
                    android:src="@drawable/icona_user_profilo">              
                </ImageView>
            </LinearLayout> 

            <LinearLayout 
                android:layout_width="0dp" 
                android:layout_height="120dp" 
                android:layout_weight="1"
                android:orientation="vertical"
                android:layout_marginLeft="12dp"          
                > 
                <EditText
                       android:id="@+id/frag_contact_add_name" 
                    android:layout_width="fill_parent" 
                    android:singleLine="true"
                    android:layout_height="wrap_content" 
                    android:background="@drawable/textview_style"
                    android:textStyle="bold" 
                    android:textColor="#000000" 
                    android:textSize="20dp">          
                </EditText>
                <EditText
                       android:id="@+id/frag_contact_add_cogname" 
                    android:layout_width="fill_parent" 
                    android:singleLine="true"
                    android:layout_height="wrap_content" 
                    android:background="@drawable/textview_style"
                    android:layout_marginTop="8dp"
                    android:textStyle="bold" 
                    android:textColor="#000000" 
                    android:textSize="20dp">          
                </EditText>
            </LinearLayout> 

        </LinearLayout>

        <LinearLayout 
            android:id="@+id/frag_contact_add_contact_number"
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:layout_below="@id/frag_contact_add_contact_info"
            android:orientation="vertical"
            android:layout_marginTop="8dp"          
            android:layout_marginLeft="12dp"        
            android:layout_marginRight="12dp"        
            > 
            <LinearLayout 
                android:layout_width="fill_parent" 
                android:layout_height="36dp"
                android:layout_marginTop="6dp"
                android:orientation="horizontal"
                android:gravity="bottom"
                >
                <ImageView
                    android:id="@+id/frag_contact_add_iv_add"
                    android:layout_width="32dp"
                    android:layout_height="32dp"
                    android:layout_marginLeft="4dp"
                    android:layout_marginRight="12dp"
                    android:layout_gravity="center"
                    android:background="@drawable/bt_plus_selector"
                    android:src="@drawable/transparent_pixel"
                    android:scaleType="centerInside">
                </ImageView>                 
            </LinearLayout>
            <LinearLayout 
                android:id="@+id/frag_contact_info_add_linearLayout_items" 
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent" 
                android:orientation="vertical"
                android:paddingTop="12dp"
                android:paddingBottom="12dp"
                android:background="@drawable/contacts_lv_phone_number_style"
                > 
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</ScrollView>

<LinearLayout 
    android:id="@+id/frag_contact_add_contact_save"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="4dp"
    android:orientation="horizontal"
    android:gravity="center_horizontal"
    > 
    <ImageView
        android:id="@+id/frag_contact_add_button_save"
          android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_marginLeft="2dp"
        android:layout_marginRight="2dp"
        android:background="#00000000"
        android:scaleType="centerInside"
        android:src="@drawable/contacts_bt_save_selector">    
    </ImageView>      
</LinearLayout>            
</RelativeLayout>

名为“frag_contact_info_add_linearLayout_items”的 LinearLayout 是一种用于动态插入/删除具有电话号码详细信息的项目(视图)的布局。通过将视图膨胀到 LinearLayout 中来插入每个视图。

名为“view_contact_phone_element”的单个项目具有以下布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:orientation="horizontal"
    > 
    <Spinner 
        android:id="@+id/contact_phone_phoneNumberTypeSpinner"
        android:layout_width="110dp" 
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:spinnerMode="dropdown"
        android:drawSelectorOnTop="true"
        android:popupBackground="@drawable/contacts_spinner_phonenumbertype_style" 
        android:background="@drawable/spinner_background_selector"
        >
    </Spinner>
    <ImageView
        android:id="@+id/contact_phone_imageView_delete"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:background="@drawable/bt_meno_selector"
        android:src="@drawable    ransparent_pixel"
        android:scaleType="centerInside">
    </ImageView>     
    <EditText 
        android:id="@+id/contact_phone_textView_number"
        android:layout_width="fill_parent"            
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/contact_phone_phoneNumberTypeSpinner"
        android:layout_toLeftOf="@id/contact_phone_imageView_delete"
        android:layout_marginLeft="1dp"
        android:layout_marginRight="2dp"
        android:layout_centerVertical="true"
        android:text="0123456789" 
        android:textColor="#000000"                        
        android:textSize="18dp"
        android:singleLine="true"
        > 
    </EditText>        
</RelativeLayout>

当调用 onActivityCreated 方法时,“frag_contact_info_add_linearLayout_items”布局使用 loadPhoneNumbers() 方法填充了 4 个“view_contact_phone_element”默认视图(没有电话号码):

private void loadPhoneNumbers() {
    View phoneNumberView = null;
    mPhoneNumbersLayout.removeAllViews();
    List<NgnPhoneNumber> sPhoneNumberItems = mNewContact.getPhoneNumbers();
    for (NgnPhoneNumber item : sPhoneNumberItems) {
        phoneNumberView = displayPhoneNumberItem(item);
    }
}

private View displayPhoneNumberItem(final NgnPhoneNumber item) {
    LayoutInflater lf = mActivity.getLayoutInflater();
    View phoneItemView = lf.inflate(R.layout.view_contact_phone_element, null, false);
    final ViewHolder viewHolder=new ViewHolder();                
    mPhoneNumbersLayout.setTag(viewHolder);
    viewHolder.spinner=(Spinner)phoneItemView.findViewById(R.id.contact_phone_phoneNumberTypeSpinner);
    ArrayAdapter<String> phoneNumberTypeAdapter=new ArrayAdapter<String>(getActivity(), R.layout.view_spinner, mPhoneNumberTypes);
    phoneNumberTypeAdapter.setDropDownViewResource(R.drawable.spinner_text);
    viewHolder.spinner.setAdapter(phoneNumberTypeAdapter);
    viewHolder.spinner.setSelection(item.getPhoneType().ordinal());
    viewHolder.spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            item.setPhoneType(PhoneType.values()[position]);
        }
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });    
    final TextView tvPhoneNumber = (TextView)phoneItemView.findViewById(R.id.contact_phone_textView_number);  
    tvPhoneNumber.setText(item.getNumber());    
    tvPhoneNumber.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View mView, MotionEvent mMotionEvent) {
            Log.d(TAG, "onTouch(): motionEvent=" + mMotionEvent.toString());
            if(mMotionEvent.getAction() == MotionEvent.ACTION_UP) {
                ((XTabFragContacts)mActivity).showPhoneNumberDialog(
                        mActivity.getClass().getCanonicalName(),
                        mNewContact, 
                        item,
                        mActivity,
                        mHandler);
                return false;
            }
            return true;
        }
    }); 

    viewHolder.ivDelete = (ImageView)phoneItemView.findViewById(R.id.contact_phone_imageView_delete);
    viewHolder.ivDelete.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view) {
                mNewContact.removePhoneNumber(item.getNumber());
                loadPhoneNumbers();
        }
    });

    RelativeLayout.LayoutParams rlParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    mPhoneNumbersLayout.addView(phoneItemView, rlParams);
    return phoneItemView;
} 

问题是上下滚动这个片段 3 或 4 次,会导致我的应用程序崩溃并显示以下日志:

05-23 15:04:40.229: D/com.mydom.myapp.FragmentContactAdd(16209): onTouch(): motionEvent=MotionEvent{40623ea0 action=0 x=62.0 y=30.0 pressure=0.03137255 size=0.90999997}
05-23 15:04:40.239: D/com.mydom.myapp.FragmentContactAdd(16209): onTouch(): motionEvent=MotionEvent{40623ea0 action=2 x=61.0 y=29.0 pressure=0.03137255 size=0.95}
05-23 15:04:40.249: D/com.mydom.myapp.FragmentContactAdd(16209): onTouch(): motionEvent=MotionEvent{40623ea0 action=2 x=59.0 y=26.0 pressure=0.03137255 size=0.97999996}
05-23 15:04:40.259: D/com.mydom.myapp.FragmentContactAdd(16209): onTouch(): motionEvent=MotionEvent{40623ea0 action=2 x=57.0 y=19.0 pressure=0.03137255 size=0.98999995}
05-23 15:04:40.279: D/com.mydom.myapp.FragmentContactAdd(16209): onTouch(): motionEvent=MotionEvent{40623ea0 action=3 x=57.0 y=7.0 pressure=0.03137255 size=1.0}
05-23 15:04:41.649: W/dalvikvm(16209): threadid=1: thread exiting with uncaught exception (group=0x400ac560)
05-23 15:04:41.649: E/ACRA(16209): ACRA caught a StackOverflowError exception for com.mydom.myapp. Building report.
05-23 15:04:41.919: I/ACRA(16209): READ_LOGS granted! ACRA can include LogCat and DropBox data.
05-23 15:04:41.939: D/ACRA(16209): Retrieving logcat output...
05-23 15:04:42.099: D/ACRA(16209): Retrieving logcat output...
05-23 15:04:42.189: D/ACRA(16209): Writing crash report file 1369314282000.stacktrace.
05-23 15:04:42.229: D/ACRA(16209): About to send status bar notification from #handleException
05-23 15:04:42.229: D/ACRA(16209): Creating Notification for 1369314282000.stacktrace
05-23 15:04:42.259: D/ACRA(16209): Waiting for Toast + worker...
05-23 15:04:42.259: D/ACRA(16209): Wait for Toast + worker ended. Kill Application ? true
05-23 15:04:42.289: E/ACRA(16209): com.mydom.myapp fatal error : null
05-23 15:04:42.289: E/ACRA(16209): java.lang.StackOverflowError
05-23 15:04:42.289: E/ACRA(16209):  at android.view.View.addFocusables(View.java:3591)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.ViewGroup.addFocusables(ViewGroup.java:637)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.ViewGroup.addFocusables(ViewGroup.java:637)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.ViewGroup.addFocusables(ViewGroup.java:637)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.ViewGroup.addFocusables(ViewGroup.java:637)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.ViewGroup.addFocusables(ViewGroup.java:637)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.ViewGroup.addFocusables(ViewGroup.java:637)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.ViewGroup.addFocusables(ViewGroup.java:637)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.ViewGroup.addFocusables(ViewGroup.java:637)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.ViewGroup.addFocusables(ViewGroup.java:637)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.ViewGroup.addFocusables(ViewGroup.java:637)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.ViewGroup.addFocusables(ViewGroup.java:637)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.ViewGroup.addFocusables(ViewGroup.java:637)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.ViewGroup.addFocusables(ViewGroup.java:618)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.View.getFocusables(View.java:3559)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.FocusFinder.findNextFocus(FocusFinder.java:109)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.FocusFinder.findNextFocus(FocusFinder.java:93)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.ViewGroup.focusSearch(ViewGroup.java:476)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.ViewGroup.focusSearch(ViewGroup.java:478)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.ViewGroup.focusSearch(ViewGroup.java:478)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.ViewGroup.focusSearch(ViewGroup.java:478)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.ViewGroup.focusSearch(ViewGroup.java:478)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.ViewGroup.focusSearch(ViewGroup.java:478)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.ViewGroup.focusSearch(ViewGroup.java:478)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.ViewGroup.focusSearch(ViewGroup.java:478)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.ViewGroup.focusSearch(ViewGroup.java:478)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.ViewGroup.focusSearch(ViewGroup.java:478)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.ViewGroup.focusSearch(ViewGroup.java:478)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.View.focusSearch(View.java:3494)
05-23 15:04:42.289: E/ACRA(16209):  at android.widget.TextView.onCreateInputConnection(TextView.java:4922)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.inputmethod.InputMethodManager.startInputInner(InputMethodManager.java:945)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.inputmethod.InputMethodManager.checkFocus(InputMethodManager.java:1129)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.inputmethod.InputMethodManager.isActive(InputMethodManager.java:542)
05-23 15:04:42.289: E/ACRA(16209):  at android.widget.TextView.onDraw(TextView.java:4368)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.View.draw(View.java:6880)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.View.draw(View.java:6883)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.View.draw(View.java:6883)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.View.draw(View.java:6986)
05-23 15:04:42.289: E/ACRA(16209):  at android.widget.FrameLayout.draw(FrameLayout.java:357)
05-23 15:04:42.289: E/ACRA(16209):  at android.widget.ScrollView.draw(ScrollView.java:1703)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.View.draw(View.java:6883)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.View.draw(View.java:6883)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.View.draw(View.java:6883)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.View.draw(View.java:6883)
05-23 15:04:42.289: E/ACRA(16209):  at android.widget.FrameLayout.draw(FrameLayout.java:357)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
05-23 15:04:42.289: E/ACRA(16209):  at android.view.ViewGroup.drawChild(ViewGroup.jav

请注意,我仅在某些设备上遇到此问题,例如:-三星 GT-S5660 和 GT-S6500(运行 Android 2.3.6)-华为 U8186(运行 Android 2.3.6)

虽然我对三星 GT-I9300、三星 GT-I8190 和 LG E400 等设备没有任何问题。

有人看到我在代码中做错了什么吗?

提前致谢。

更新:

我通过用一个简单的按钮替换“view_contact_phone_element”中的微调器来解决。单击按钮时,我只打开一个对话框,显示可以从用户中选择的不同标签。

我猜滚动视图中的微调器可能会在某些设备上导致此类问题。

4

1 回答 1

1

你得到了 StackOverflowException,我认为这是因为你将视图添加到 LinearLayout,它尝试为每个可见项目调用 android.view.ViewGroup.addFocusables(),所以你得到了 StackOverflowException。LinearLayout 不能处理这么多可能的可见元素,它不是大量子视图的好容器。

我认为,您最好使用ListView而不是将视图添加到 LinearLayout,还可以使用ViewHolder模式来更好地使用内存并提高 ListView 的性能。

于 2013-05-23T14:04:17.827 回答