我尝试编写一个应用示例,以检查来自String 1
的用户输入(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>
编辑:更新代码。
Java:
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)
答案 0 :(得分:0)
如果要在移动屏幕上进行视觉确认,则可以让TextView
或EditText
进行显示。您可以使用EditText input1
将文本设置为setText()
。
例如: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中获取值进行比较,并基于textview中的值设置文本
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!");
}
}
}
}
答案 1 :(得分:0)
我要指出的一些错误:
{ }
使用单独的块if-else
System.out.println()
用于Android开发,请尝试使用Log.i()
或任何其他Log
语句。EditText.setText()
或TextView.setText()
函数在您的情况下显示视觉输出。 View
初始化相应的TextView
(例如EditText
或findViewById()
以使用setText()
或{{ 1}}。 编辑:由于您的代码应在活动从版式膨胀时运行,因此请将代码放在getText()
中的setContentView()
之后
我建议您通过official developer guide进行推荐。