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.