SeekBar和EditText互相改变引起的Stackoverflow错误

时间:2014-03-18 19:45:44

标签: android android-edittext stack-overflow seekbar

所以我将SeekBar与输入类型编号的EditText一起设置。

我希望如此,如果用户输入一个可行范围内的数字,它将改变SeekBar的进度。此外;如果用户决定通过拖动SeekBar来改变它们;而是将EditText更改为SeekBar的当前值。

我收到一个试图实现这个的stackoverflow,我相信这是因为我们都使用他们的onChanged监听器。所以他们互相造成了无限的变化。

如果是这样的话;如何解释,我将如何实现我想要的结果?

代码:

s1.setOnSeekArcChangeListener(new SeekArc.OnSeekArcChangeListener() {

        @Override
        public void onStopTrackingTouch(SeekArc seekArc) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStartTrackingTouch(SeekArc seekArc) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProgressChanged(SeekArc seekArc, int progress,
                boolean fromUser) {
                            if(progress != Integer.parseInt(et1.getText().toString()))
            et1.setText(String.valueOf(progress));
            // TODO Auto-generated method stub

        }
    });

et1.addTextChangedListener(new TextWatcher(){

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
            int i = Integer.parseInt(s.toString());
            if(i != Integer.parseInt(et1.getText().toString()))
            s1.setProgress(i);
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1,
                int arg2, int arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {
            // TODO Auto-generated method stub
        }

    });

logcat的:

    03-18 14:38:59.104: E/AndroidRuntime(2894): FATAL EXCEPTION: main
03-18 14:38:59.104: E/AndroidRuntime(2894): java.lang.StackOverflowError
03-18 14:38:59.104: E/AndroidRuntime(2894):     at android.text.DynamicLayout.reflow(DynamicLayout.java:284)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at android.text.DynamicLayout.<init>(DynamicLayout.java:170)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at android.widget.TextView.makeSingleLayout(TextView.java:5867)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at android.widget.TextView.makeNewLayout(TextView.java:5765)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at android.widget.TextView.checkForRelayout(TextView.java:6304)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at android.widget.TextView.setText(TextView.java:3567)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at android.widget.TextView.setText(TextView.java:3425)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at android.widget.EditText.setText(EditText.java:80)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at android.widget.TextView.setText(TextView.java:3400)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at com.nanospark.upcdemo.HomeFragment$1.onProgressChanged(HomeFragment.java:61)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at com.triggertrap.seekarc.SeekArc.updateProgress(SeekArc.java:425)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at com.triggertrap.seekarc.SeekArc.setProgress(SeekArc.java:454)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at com.nanospark.upcdemo.HomeFragment$5.afterTextChanged(HomeFragment.java:116)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at android.widget.TextView.sendAfterTextChanged(TextView.java:7119)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at android.widget.TextView.setText(TextView.java:3574)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at android.widget.TextView.setText(TextView.java:3425)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at android.widget.EditText.setText(EditText.java:80)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at android.widget.TextView.setText(TextView.java:3400)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at com.nanospark.upcdemo.HomeFragment$1.onProgressChanged(HomeFragment.java:61)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at com.triggertrap.seekarc.SeekArc.updateProgress(SeekArc.java:425)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at com.triggertrap.seekarc.SeekArc.setProgress(SeekArc.java:454)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at com.nanospark.upcdemo.HomeFragment$5.afterTextChanged(HomeFragment.java:116)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at android.widget.TextView.sendAfterTextChanged(TextView.java:7119)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at android.widget.TextView.setText(TextView.java:3574)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at android.widget.TextView.setText(TextView.java:3425)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at android.widget.EditText.setText(EditText.java:80)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at android.widget.TextView.setText(TextView.java:3400)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at com.nanospark.upcdemo.HomeFragment$1.onProgressChanged(HomeFragment.java:61)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at com.triggertrap.seekarc.SeekArc.updateProgress(SeekArc.java:425)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at com.triggertrap.seekarc.SeekArc.setProgress(SeekArc.java:454)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at com.nanospark.upcdemo.HomeFragment$5.afterTextChanged(HomeFragment.java:116)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at android.widget.TextView.sendAfterTextChanged(TextView.java:7119)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at android.widget.TextView.setText(TextView.java:3574)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at android.widget.TextView.setText(TextView.java:3425)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at android.widget.EditText.setText(EditText.java:80)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at android.widget.TextView.setText(TextView.java:3400)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at com.nanospark.upcdemo.HomeFragment$1.onProgressChanged(HomeFragment.java:61)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at com.triggertrap.seekarc.SeekArc.updateProgress(SeekArc.java:425)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at com.triggertrap.seekarc.SeekArc.setProgress(SeekArc.java:454)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at com.nanospark.upcdemo.HomeFragment$5.afterTextChanged(HomeFragment.java:116)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at android.widget.TextView.sendAfterTextChanged(TextView.java:7119)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at android.widget.TextView.setText(TextView.java:3574)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at android.widget.TextView.setText(TextView.java:3425)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at android.widget.EditText.setText(EditText.java:80)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at android.widget.TextView.setText(TextView.java:3400)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at com.nanospark.upcdemo.HomeFragment$1.onProgressChanged(HomeFragment.java:61)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at com.triggertrap.seekarc.SeekArc.updateProgress(SeekArc.java:425)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at com.triggertrap.seekarc.SeekArc.setProgress(SeekArc.java:454)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at com.nanospark.upcdemo.HomeFragment$5.afterTextChanged(HomeFragment.java:116)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at android.widget.TextView.sendAfterTextChanged(TextView.java:7119)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at android.widget.TextView.setText(TextView.java:3574)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at android.widget.TextView.setText(TextView.java:3425)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at android.widget.EditText.setText(EditText.java:80)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at android.widget.TextView.setText(TextView.java:3400)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at com.nanospark.upcdemo.HomeFragment$1.onProgressChanged(HomeFragment.java:61)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at com.triggertrap.seekarc.SeekArc.updateProgress(SeekArc.java:425)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at com.triggertrap.seekarc.SeekArc.setProgress(SeekArc.java:454)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at com.nanospark.upcdemo.HomeFragment$5.afterTextChanged(HomeFragment.java:116)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at android.widget.TextView.sendAfterTextChanged(TextView.java:7119)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at android.widget.TextView.setText(TextView.java:3574)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at android.widget.TextView.setText(TextView.java:3425)
03-18 14:38:59.104: E/AndroidRuntime(2894):     at android.widget.EditText.setText(EditText.java:80

2 个答案:

答案 0 :(得分:5)

onProgressChanged(SeekArc seekArc, int progress, boolean fromUser)中,您有一个fromUser变量。如果用户没有擦洗您的SeekBar / Arc,则不要在EditText上设置文字(该文本已经被认为是正确的,因为它启动了事件链... )

在你的听众中:

@Override
public void onProgressChanged(SeekArc seekArc, int progress, boolean fromUser) {
    if(fromUser && progress != Integer.parseInt(et1.getText().toString())) {
        et1.setText(String.valueOf(progress));
    }
}

答案 1 :(得分:0)

当您更改SeekBar值时,您更改了搜索栏侦听器中的EditText值(et1.setText(String.valueOf(progress));)这是正常的原因,因此当您更改时,EditText侦听器被调用为AND你的EditText值(在SeekBar监听器上),你在EditText的Listener中改变了SeekBar值,然后你创建了一种无限循环。