Не удается устранить ошибку символа при изменении текста в текстовом представлении кнопкой в ​​Android Studio - PullRequest
0 голосов
/ 15 мая 2018

Помогите мне, я пытаюсь разработать приложение для Судоку. Я застрял в этом, я просто BSC. Первый год парень ничего не знает

Здесь мой код Xml для моего кода TextView

<TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/change"
        android:text="Not Changed"/>

Вот мой XML-код для кнопки

    <Button
    android:id="@+id/btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="24dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:text="Test Button"
    app:layout_constraintBottom_toTopOf="@+id/btn_sample"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

Вот мой код Java

public void btn(View view) {
        TextView t=(TextView)findViewById(change);
        t.setText("Changed");
}

Если мне это удастся, это мотивирует меня и моих товарищей по CLG Я нуб здесь извините за плохой английский Заранее спасибо

Обновление: Ответ решен Новый код для кнопки

<Button
    android:id="@+id/btn"
    android:onClick="btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="24dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:text="Test Button"
    app:layout_constraintBottom_toTopOf="@+id/btn_sample"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

Новый обновленный код для Java

public void btn(View view) {
        TextView t=(TextView)findViewById(R.id.change);
        t.setText("Changed");
}

Благодаря Nilesh-rathod

1 Ответ

0 голосов
/ 15 мая 2018

findViewById()

Находит представление, идентифицированное атрибутом android:id XML, обработанным в onCreate(Bundle),

Используйте это

TextView t=(TextView)findViewById(R.id.change);

Вместо этого

TextView t=(TextView)findViewById(change);

EDIT

Если вы хотите добавить событие кликаВы Button, чем, пожалуйста, используйте ниже редактировать

Добавить android:onclick="btn" в вашем Button

<Button
    android:id="@+id/btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="24dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:onclick="btn"
    android:text="Test Button"
    app:layout_constraintBottom_toTopOf="@+id/btn_sample"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />
...