java - Android:带有 string.equals 的 If/else 语句,返回视觉确认
问题描述
我尝试编写一个应用程序示例,该示例检查String 1
来自 a 的用户输入()EditText
并将其与字符串(在本例中translation 1
)进行比较。
在回答和建议之后,我更新了我的 Java 代码,据我所知,我应该可以正常工作,但我没有。(见调试器编辑)
提前致谢!
XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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=".MainActivity">
<!-- Page Title -->
<TextView
android:id="@+id/PageTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="@string/test_title"
android:textAlignment="center"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!-- Input 1 -->
<EditText
android:id="@+id/input1"
android:layout_width="140dp"
android:layout_height="40dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:inputType="text"
android:text=""
android:hint="@string/hint_input"
app:layout_constraintEnd_toStartOf="@+id/translation1"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/PageTitle"
android:imeOptions="actionNext"/>
<!-- Translation 1 -->
<TextView
android:id="@+id/translation1"
android:layout_width="140dp"
android:layout_height="40dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
android:gravity="center"
android:text="@string/test"
android:textAlignment="center"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/input1"
app:layout_constraintTop_toBottomOf="@+id/PageTitle"
android:background="@drawable/translation_borders"/>
<!--Check button-->
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:onClick="Check"
android:text="@string/check_btn"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/translation1"
app:layout_constraintStart_toStartOf="@+id/input1"
app:layout_constraintTop_toBottomOf="@+id/PageTitle" />
</android.support.constraint.ConstraintLayout>
编辑:更新代码。
爪哇:
package com.bnf.Overhooring;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
String right = getString(R.string.right);
String wrong = getString(R.string.wrong);
EditText input1 = (EditText) findViewById(R.id.input1);
String string1 = input1.getText().toString();
String translation1 = "a";
public void Check(View view) {
if (string1.equals(translation1)) input1.setText(right);
else input1.setText(wrong);
}
}
编辑:发布调试器结果
我在更新代码后运行了调试器,发现我的应用程序在启动时一直关闭。调试器中的错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.bnf.overhooring, PID: 471
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.bnf.overhooring/com.bnf.overhooring.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2849)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.content.ContextWrapper.getResources(ContextWrapper.java:86)
at android.view.ContextThemeWrapper.getResourcesInternal(ContextThemeWrapper.java:127)
at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:121)
at android.support.v7.app.AppCompatActivity.getResources(AppCompatActivity.java:542)
at android.content.Context.getString(Context.java:476)
at com.bnf.overhooring.MainActivity.<init>(MainActivity.java:16)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1086)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2839)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
解决方案
我想指出几个错误:
- 不要使用单独的
{ }
块if-else
- 请勿
System.out.println()
用于 Android 开发、尝试Log.i()
或任何其他Log
声明。 - 作为视觉显示,请尝试使用
EditText.setText()
orTextView.setText()
函数在您的情况下显示视觉输出。 - 要遵循以上几点,首先需要初始化相应的
View
,likeTextView
orEditText
byfindViewById()
以使用setText()
orgetText()
。
编辑:由于您的代码应该在活动从布局中膨胀时运行,因此将您的代码放在setContentView()
后面onCreate()
我建议您通过官方开发者指南进行推荐。
推荐阅读
- javascript - 什么是 grid-column : 1 / -1 含义
- node.js - 如何使用 nodejs 上传 zip 文件并解压缩?
- javascript - VueJS/Vuetify - 可拖动的响应式卡片网格 - 不起作用
- linux - rsync --exclude 和 --exclude-from 不起作用
- mongodb - mongodb 一次获取 10_000 行
- python - 我是否必须重新创建表作为现有数据库的模型?
- php - 压缩 FPDF 文件?
- mysql - 数据库性能:拆分表或保持在一起
- javascript - 从 tinyMCE 表中获取文本内容
- scala - Scala 辅助构造函数中的参数