我尝试编写一个应用程序示例,该示例检查EditText
中的用户输入(String 1
(并将其与字符串进行比较(在本例中为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()
或TextView.setText()
函数来显示您的情况下的视觉输出。 - 要遵循上述几点,首先需要初始化相应的
View
,例如TextView
或EditText
,findViewById()
使用setText()
或getText()
。
编辑:由于您的代码应该在活动从布局中膨胀时运行,因此请将代码放在setContentView()
之后onCreate()
我建议您通过官方开发人员指南进行推荐。
如果您想在移动屏幕上进行视觉确认,那么您可以让TextView
或EditText
显示它。您可以使用setText()
将文本设置为EditText input1
。
例如:input1.setText("Text");
TextView translation = (TextView) findViewById(R.id.translation1);
translation.setText(input1.getText().toString());
您可以使用以下命令设置OnClickListener
:
Button btn = findViewById(R.id.button);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//Your code goes here
}
});
这将从 edittext 比较中获取值,并根据文本视图中的文本设置文本
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText input1 = (EditText) findViewById(R.id.input1);
String String1 = input1.getText().toString();
String translation1 = "a";
Button btn = findViewById(R.id.button);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(String1.equals(translation1)) {
//System.out.print("This right!");
translation.setText("This right!");
} else {
//System.out.print("This is wrong!");
translation.setText("This wrong!");
}
}
}
}