解除键盘时的StackOverflowError

时间:2014-07-17 09:38:26

标签: java android

我有一个从MultiAutoCompleteTextView扩展的自定义视图,以创建像gmail中的联系人一样的chiped视图。当我接触到这个视图并且keyborad被解雇时,casuse堆栈溢出。它只发生在我的nexus 4中这是logcat。

java.lang.StackOverflowError
            at android.text.DynamicLayout.reflow(DynamicLayout.java:284)
            at android.text.DynamicLayout.<init>(DynamicLayout.java:170)
            at android.widget.TextView.makeSingleLayout(TextView.java:6134)
            at android.widget.TextView.makeNewLayout(TextView.java:6032)
            at android.widget.TextView.checkForRelayout(TextView.java:6571)
            at android.widget.TextView.onRtlPropertiesChanged(TextView.java:8672)
            at android.view.View.resolvePadding(View.java:12407)
            at android.view.View.getPaddingLeft(View.java:15603)
            at com.tokenautocomplete.TokenCompleteTextView.maxTextWidth(TokenCompleteTextView.java:260)
            at com.tokenautocomplete.TokenCompleteTextView.access$1000(TokenCompleteTextView.java:54)
            at com.tokenautocomplete.TokenCompleteTextView$ViewSpan.prepView(TokenCompleteTextView.java:822)
            at com.tokenautocomplete.TokenCompleteTextView$ViewSpan.getSize(TokenCompleteTextView.java:841)
            at com.tokenautocomplete.TokenCompleteTextView$TokenImageSpan.getSize(TokenCompleteTextView.java:885)

这是我的预备视图代码

private void prepView() {
        int widthSpec = MeasureSpec.makeMeasureSpec((int)maxTextWidth(), MeasureSpec.AT_MOST);
        int heightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);

        view.measure(widthSpec, heightSpec);
        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
    }

这是tokenCompleteTextView的第260行

private float maxTextWidth() {
    return getWidth() - getPaddingLeft() - getPaddingRight();
}

使用TokenAutoComplete libray作为令牌视图。

这是我的布局

<?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:background="@color/gray" 
android:clickable="true">

 <LinearLayout
 android:id="@+id/llsearch"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"   
 android:orientation="horizontal"   
 android:weightSum="4"      
 android:background="@color/listview_color"
 android:layout_marginTop="@dimen/hdpi_4dp"
 android:layout_marginBottom="@dimen/hdpi_4dp"
 android:gravity="center_vertical">

 <in.ispg.chipview.ConatctCompleteTextView
 android:id="@+id/edtsearch"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:hint="@string/search"
 android:layout_weight="1"
 android:textSize="@dimen/textsize_edittext"
 android:textColor="@color/black"
 android:paddingLeft="@dimen/hdpi_4dp"
 android:paddingRight="@dimen/hdpi_4dp"
 android:layout_marginLeft="@dimen/hdpi_8dp"
 android:layout_marginRight="@dimen/hdpi_8dp"
 android:singleLine="false"
 android:minLines="1"
 android:maxLines="5"
 >
    <requestFocus />
</in.ispg.chipview.ConatctCompleteTextView>


<Button 
    android:id="@+id/btnsearch"
    android:layout_width="fill_parent"
    android:layout_height="@dimen/hdpi_33dp"
    android:text="@string/done"
    android:layout_weight="3"
    android:background="@drawable/send_button"
    android:layout_marginRight="@dimen/hdpi_8dp"
    android:textColor="@color/white"
    android:gravity="center"

    />

         

<in.ispg.utils.FontTextView 
android:id="@android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textSize="@dimen/textsize_edittext"
android:textColor="#595959"
android:textStyle="bold"
android:text="" />

<ProgressBar
    android:id="@+id/progressBar1"
    style="?android:attr/progressBarStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:indeterminate="true"
    android:indeterminateDrawable ="@drawable/progress"
    android:visibility="gone" />

注意

我知道如何解雇键盘。那不是我的问题。当我在特定视图中执行此操作时,出现stackoverflow错误。

3 个答案:

答案 0 :(得分:0)

1。)试试这个,希望它能解决你的问题,我使用下面的代码来隐藏软件键盘

public void hideSoftKeyboard(Activity activity) {
    InputMethodManager inputMethodManager = (InputMethodManager) activity
            .getSystemService(Activity.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus()
            .getWindowToken(), 0);
}

OR

如果我需要关心键盘出现和消失的时间(经常是这样),那么我所做的就是将我的顶级布局类自定义为覆盖onMeasure()的类。基本逻辑是,如果布局发现自己填充的内容明显少于窗口的总面积,则可能会显示软键盘。

  import android.app.Activity;
  import android.content.Context;
  import android.graphics.Rect;
  import android.util.AttributeSet;
  import android.widget.LinearLayout;

/ *  * LinearLayoutThatDetectsSoftKeyboard - LinearLayout的一种变体,可以检测何时  *软键盘显示和隐藏(Android无法告诉你,奇怪的是)。  * /

public class LinearLayoutThatDetectsSoftKeyboard extends LinearLayout {

public LinearLayoutThatDetectsSoftKeyboard(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public interface Listener {
    public void onSoftKeyboardShown(boolean isShowing);
}
private Listener listener;
public void setListener(Listener listener) {
    this.listener = listener;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int height = MeasureSpec.getSize(heightMeasureSpec);
    Activity activity = (Activity)getContext();
    Rect rect = new Rect();
    activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
    int statusBarHeight = rect.top;
    int screenHeight = activity.getWindowManager().getDefaultDisplay().getHeight();
    int diff = (screenHeight - statusBarHeight) - height;
    if (listener != null) {
        listener.onSoftKeyboardShown(diff>128); // assume all soft keyboards are at least 128 pixels high
    }
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);       
}

}

然后在你的Activity类......

 public class MyActivity extends Activity implements      LinearLayoutThatDetectsSoftKeyboard.Listener {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
    LinearLayoutThatDetectsSoftKeyboard mainLayout = (LinearLayoutThatDetectsSoftKeyboard)findViewById(R.id.main);
    mainLayout.setListener(this);
    ...
}


@Override
public void onSoftKeyboardShown(boolean isShowing) {
    // do whatever you need to do here
}

 ...
}

答案 1 :(得分:0)

我使用此代码隐藏软键盘

//关闭软键盘

InputMethodManager inputMethod = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethod.hideSoftInputFromWindow(getView().getWindowToken(), 0);

答案 2 :(得分:0)

我遇到了同样的问题。我不知道为什么会这样。我在不同的设备和模拟器中测试过。这个错误只发生在某些设备上,而在模拟器中我无法模拟它。这就是我所做的:

@Override
public int getSize(Paint paint, CharSequence charSequence, int i, int i2, Paint.FontMetricsInt fm) {
        try{
            prepView();
        }catch(StackOverflowError error){

        }
        ...

此代码阻止应用程序关闭。

相关问题