为什么第二次点击编辑文本字段会导致应用程序错误?安卓-JAVA

问题描述 投票:0回答:0

我有一个获取数字的编辑文本字段。当场地空旷时,我可以随意触摸它。如果出现一个字符并且我关闭键盘,我第二次点击 edittext 应用程序将获得一个 get 运行时。

我试图在有关 EditText 的 Android 文档中找到一些方法或任何东西来帮助我,但我没有找到它。

这是我的代码

Java

package com.example.aplikacja_testowa;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class CallSettings extends AppCompatActivity {
    private TextView textView;
    private EditText editText;
    private Button saveButton;

    public static final String SHARED_PREFS = "sharedPrefs";
    public static final String TEXT = "text";
    //public static final

    private String text;

    ////////////////
    int dem=0;
    int them;
    int selectTheme = 2;
    ////////////////

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

        them=SmsSettings.loadData(CallSettings.this, "style.txt", null, dem);
        selectTheme = them;
        Log.i("TAG", "onCreate selectTheme: "+selectTheme);
        FontSettings.setDynamicTheme(CallSettings.this, selectTheme);

        setContentView(R.layout.activity_call_settings);
        textView = findViewById(R.id.callInstruction);
        editText = findViewById(R.id.textPhoneNumber);
        saveButton = findViewById(R.id.save_button);
    }

    public void savePhoneNumber(View v){
        SmsSettings.saveData(this, "callnumber.txt", editText, 0);
        Toast.makeText(this, R.string.number_saved, Toast.LENGTH_SHORT).show();
    }
}

XML

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".CallSettings">

    <TextView
        android:id="@+id/callInstruction"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/after_ent_the_num"
        app:layout_constraintBottom_toTopOf="@+id/textPhoneNumber"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/textPhoneNumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/inp_ph_num"
        android:inputType="phone"
        android:minHeight="48dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/save_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="80dp"
        android:onClick="savePhoneNumber"
        android:text="@string/save_sett"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textPhoneNumber" />
</androidx.constraintlayout.widget.ConstraintLayout>

java android android-edittext
© www.soinside.com 2019 - 2024. All rights reserved.