1

I've been writing a starter app in Android Studio (after 20 years in assembly, C++, VBA, and 2 years in C#), and I'm having good luck in general. But I'm stumped on this one.

I have an EditText where I put a sample from voice to text (no punctuation, no caps or quotes or hyphens). I select a section, and can cleanly, with click of a button, do all caps, initial caps, all hyphens between, surround with quote marks, &c. I can run each of these in succession, the select being retained by a line that restores it a the end of each click, so in a test case, I have a selection with all of the above enhancements one on top of another. There seems to be no limit, until I change the selection area.

In the debugger, I see that all the steps are working as expected. The crash comes only when I attempt to copy the new, second selection into a variable string.

sel = txt.substring(sst,snd);// this works fine prior to new select

Then the next F7 (Step Into, the least step I can do) takes me to

public void onClick(@android.support.annotation.NonNull android.view.View v) { /* compiled code */ }

One more F7 and it crashes.

Is there a way to reset the EditText before or after a change of selection that I am neglecting?

Many thanks to all who will comment.

I've gone as deep as I can into the underlying code to find the point of controversy. I've looked for a way to reset the EditText after finishing with the original selection.

This is the base class shared setup for each of these buttons:

package com.example.app5;
import android.widget.EditText;

public class EditorButton {
    public String txt = "", outtxt = "", sel = "";
    public int sst, snd;
    public EditText t;

    public EditorButton(EditText T) {
        t = T;
        txt = t.getText().toString();
        sst = t.getSelectionStart();
        snd = t.getSelectionEnd();

        sel = txt.substring(sst,snd);
    }
    public void Finish(){
        String newtxt = txt.substring(0,sst) + outtxt + txt.substring(snd);
        t.setText((CharSequence)newtxt);
        t.setSelection(sst, (sst + outtxt.length()));
    }

// Then as an example, one of the inheritors
 public void Q(View view){
        EditorButton eb = new EditorButton(t);
        eb.outtxt = "\"" + eb.txt.substring(eb.sst,eb.snd) + "\"";
        eb.Finish();
    }

Expected results, the same as before.
Actual: first selection great, second one, crash.

  • Run the app outside the debugger, then [look at Logcat to see the stack trace](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this). If you do not grok the stack trace, edit your question and add it. – CommonsWare Jun 09 '19 at 23:21
  • OK, I did that, found this line: Caused by: java.lang.StringIndexOutOfBoundsException: length=43; regionStart=22; regionLength=-14 So I set a break at the EditorButton line 15 which is sel = txt.substring(sst,snd); ran to the break, ran out of the first stop (which has always worked) moved the selection to a different substring ran to the break, then carefully, using all F7s (StepInto) and — to my amazement — the second selection was processed correctly as well! No change in the code. Only the very tiptoe gradual stepping through it. Weird. Thanks for LogCat tip tho – ProudPrimate Jun 10 '19 at 13:00
  • Now it's all working perfectly! Needed aging, I guess ???? – ProudPrimate Jun 10 '19 at 13:02

0 Answers0