1

I'm having trouble selecting a text from a textView, every time I long press to select a text the application crashes and I don't understand why. I think it's mainly because the text view it's on a Dialog.

I use the following classes:

TextView:

 <TextView
        style="@style/PUPStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/manual_entry"
        android:enabled="true"
        android:textIsSelectable="true"
        android:focusable="true"
        android:longClickable="true"/>

Dialog class:

public class CardReadAlertDialog extends Dialog implements CardReadAlertUseCases {

private LinearLayout mManualInput;
private LinearLayout mReadCard;
private TextView mMessageView;
private Handler mTimer;
private Button btnManualInput;

public CardReadAlertDialog(@NonNull Context context) {
    super(context);
}

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.card_read_alert);
    setCancelable(false);

    findViewById(R.id.btn_cancel).setOnClickListener((view) -> abortProcess(true));

    findViewById(R.id.btn_cancel_read).setOnClickListener((view) -> abortProcess(true));

    btnManualInput = findViewById(R.id.btn_manual_input);
    btnManualInput.setOnClickListener((view) -> createManualInput());

    findViewById(R.id.btn_accept).setOnClickListener((view) -> {
        ManualCardEntry manualCardEntry = getManualEntryFromInput();
        State.getInstance().setManualEntry(manualEntry);

        if (manualCardEntry != null) {
            abortProcess(false);
        }
    });


    mManualInput = findViewById(R.id.manual_input_view);
    mReadCard = findViewById(R.id.card_reading_view);
    mMessageView = findViewById(R.id.read_message);
}

Error log:

09-09 09:01:14.334 16201-16201/com.carslash.carslashcarslash E/AndroidRuntime: FATAL EXCEPTION: main Process: com.carslash.carslashcarslash, PID: 16201 android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application at android.view.ViewRootImpl.setView(ViewRootImpl.java:578) at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:282) at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85) at android.widget.PopupWindow.invokePopup(PopupWindow.java:1104) at android.widget.PopupWindow.showAtLocation(PopupWindow.java:933) at com.android.internal.policy.impl.PhoneWindow$DecorView$1.run(PhoneWindow.java:2742) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5271) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)

Any help is welcome, thank you beforehand.

Mario Muresean
  • 233
  • 1
  • 5
  • 16

1 Answers1

0

You can use and EditText with textviewstyle it should work

<EditText android:id="@+id/showText"
    style="?android:attr/textViewStyle"
    android:background="@null"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

Reference https://stackoverflow.com/a/7198401/12676247

Jyotish Biswas
  • 674
  • 5
  • 11