我发现问题是EditText
返回NullPointerException
(remove(at:)
)。现在,我已经在Google上搜索了几个小时的解决方案,并找到了一些解决方案,但它们并不适合我。有人能够编辑我的地雷代码,以便我从Edittext
中正确地获取值作为字符串。这是我的代码:
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"/>
<!--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.view.View;
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);
String translation1 = getString(R.string.translation1);
EditText input1 = (EditText) findViewById(R.id.input1);
String string1 = input1.getText().toString();
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: 30985
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 :(得分:2)
您的代码经过简化,如下所示:
public class MainActivity extends AppCompatActivity { String right = getString(R.string.right); String wrong = getString(R.string.wrong); String translation1 = getString(R.string.translation1); }
调用getString()
时发生的事情确实有些复杂,但是要理解的重要一点是它需要一个Resources
对象。在构造活动实例时(即正在尝试调用Resources
时),该getString()
对象不是立即“就绪”。
通常,要等到super.onCreate()
被调用后才能尝试访问资源框架;这包括呼叫getString()
。
您可以通过将代码更改为如下形式来解决它:
public class MainActivity extends AppCompatActivity {
private String right;
private String wrong;
private String translation1;
private EditText input1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.right = getString(R.string.right);
this.wrong = getString(R.string.wrong);
this.translation1 = getString(R.string.translation1);
this.input1 = (EditText) findViewById(R.id.input1);
}
public void Check(View view) {
String string1 = input1.getText().toString();
if (string1.equals(translation1)) {
input1.setText(right);
}
else {
input1.setText(wrong);
}
}
}
我们在这里要做的重要事情是将right
,wrong
,translation1
和input1
的分配移到我们的onCreate()
方法中。我们还确保getString()
调用发生在super.onCreate()
之后,并且findViewById()
调用发生在setContentView()
之后。顺序很重要。
最后,在实际调用string1
方法之前,没有理由为Check()
分配值。这样一来,我们可以确保使用EditText
中的最新值。
答案 1 :(得分:0)
在生命周期中发生onCreate之前,您无法访问视图。当android初始化所有视图时,就可以访问它。
这对我来说很好
package com.example.casti.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
String right = "right";
String wrong = "wrong";
String translation1 = "translation1";
EditText input1;
String string1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
input1 = findViewById(R.id.input1);
string1 = input1.getText().toString();
}
public void Check(View view) {
if (string1.equals(translation1)) input1.setText(right);
else input1.setText(wrong);
}
}