接受一个edittext字段并在TextView中显示

时间:2019-01-24 23:24:39

标签: java android android-edittext textview illegalstateexception

我对编码还比较陌生,但是在Java和Android方面有一定经验。我有一遍又一遍地做了一个非常基本的问题,但是由于某种原因,这种情况不起作用了!

我有一个TextView字段,由用户填充。当按下回车按钮时,应用程序会将其保存为字符串。然后,该字符串用于用用户输入填充findviewbyids

我已经创建了edittext,已经将String设置为等于package com.example.android.golfhandicap; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { EditText playerName; TextView playerOneName; String name; int handicap; EditText playerHandicap; TextView playerOneHcp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.front_page); } //set view by IDs public void views() { playerName = findViewById(R.id.inputPlayerName); playerOneName = findViewById(R.id.playerOneName); playerHandicap = findViewById(R.id.inputPlayerHcp); playerOneHcp = findViewById(R.id.playerOneHcp); } /** * Add the codes for the button to navigate around the app */ public void enterScore1(View view) { setContentView(R.layout.front_page); } public void addPlayer(View view) { setContentView(R.layout.new_player); } public void addScore(View view) { setContentView(R.layout.input_page); } public void enterPlayer(View view) { views(); name = playerName.getText().toString(); //handicap = Integer.parseInt(playerHandicap.getText().toString()); playerOneName.setText(name); //playerOneHcp.setText(handicap); Toast.makeText(this, "player name is " + name + " and handicap is " + handicap , Toast.LENGTH_SHORT).show(); setContentView(R.layout.front_page); } } 输入,然后按照下面的代码将文本视图中的文本设置为String。我已经检查过以前制作的应用,并且在以前一直可以使用...

String

我要在其中显示<?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" android:visibility="visible" tools:context=".MainActivity"> <TextView android:id="@+id/appName2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginTop="16dp" android:layout_marginEnd="8dp" android:text="Golf handicap" android:textAlignment="center" android:textColor="@color/colorPrimaryDark" android:textSize="24sp" android:textStyle="bold" android:visibility="visible" android:editable="false" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/playerOneName" android:layout_width="136dp" android:layout_height="30dp" android:layout_marginStart="8dp" android:editable="false" android:layout_marginTop="32dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/appName2" /> <TextView android:id="@+id/playerOneName2" android:editable="false" android:layout_width="136dp" android:layout_height="30dp" android:layout_marginStart="8dp" android:layout_marginTop="24dp" android:visibility="invisible" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/playerOneName3" /> <TextView android:id="@+id/playerOneName3" android:editable="false" android:layout_width="136dp" android:layout_height="30dp" android:layout_marginStart="8dp" android:layout_marginTop="24dp" android:visibility="invisible" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/playerOneName" /> <Button android:id="@+id/addScore" android:layout_width="200dp" android:layout_height="40dp" android:layout_marginBottom="16dp" android:background="@android:color/holo_green_light" android:onClick="addScore" android:text="Add new score" android:visibility="visible" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/addPlayer" /> <Button android:id="@+id/addPlayer" android:layout_width="200dp" android:layout_height="40dp" android:layout_marginBottom="16dp" android:background="@android:color/holo_green_light" android:onClick="addPlayer" android:text="Add new player" android:visibility="visible" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toStartOf="@+id/addScore" app:layout_constraintStart_toStartOf="parent" /> <TextView android:id="@+id/playerOneHcp" android:layout_width="43dp" android:layout_height="30dp" android:layout_marginStart="31dp" android:layout_marginTop="32dp" android:editable="false" android:ems="10" android:inputType="number" android:textAlignment="center" android:visibility="visible" app:layout_constraintStart_toEndOf="@+id/playerOneName" app:layout_constraintTop_toBottomOf="@+id/appName2" /> <TextView android:id="@+id/editText2" android:editable="false" android:layout_width="43dp" android:layout_height="30dp" android:layout_marginStart="31dp" android:layout_marginTop="24dp" android:ems="10" android:inputType="number" android:textAlignment="center" android:visibility="invisible" app:layout_constraintStart_toEndOf="@+id/playerOneName2" app:layout_constraintTop_toBottomOf="@+id/editText3" /> <TextView android:id="@+id/editText3" android:editable="false" android:layout_width="43dp" android:layout_height="30dp" android:layout_marginStart="31dp" android:layout_marginTop="24dp" android:ems="10" android:inputType="number" android:textAlignment="center" android:visibility="invisible" app:layout_constraintStart_toEndOf="@+id/playerOneName3" app:layout_constraintTop_toBottomOf="@+id/playerOneHcp" /> </android.support.constraint.ConstraintLayout> 的文本视图页面的XML:

edittext

带有<?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"> <TextView android:id="@+id/enterHcpText" android:layout_width="136dp" android:layout_height="50dp" android:layout_marginStart="8dp" android:layout_marginTop="24dp" android:editable="false" android:text="Current handicap :" android:textAlignment="center" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/enterAgeText" /> <TextView android:id="@+id/enterAgeText" android:layout_width="136dp" android:layout_height="50dp" android:layout_marginStart="8dp" android:layout_marginTop="24dp" android:editable="false" android:text="Player age :" android:textAlignment="center" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/enterNameText" /> <TextView android:id="@+id/enterNameText" android:layout_width="136dp" android:layout_height="50dp" android:layout_marginStart="8dp" android:layout_marginTop="32dp" android:editable="false" android:text="Player name :" android:textAlignment="center" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/appName3" /> <TextView android:id="@+id/appName3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginTop="16dp" android:layout_marginEnd="8dp" android:editable="false" android:text="Golf handicap" android:textAlignment="center" android:textColor="@color/colorPrimaryDark" android:textSize="24sp" android:textStyle="bold" android:visibility="visible" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <EditText android:id="@+id/inputPlayerName" android:layout_width="130dp" android:layout_height="50dp" android:layout_marginTop="32dp" android:layout_marginEnd="33dp" android:maxLength="25" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@+id/appName3" /> <EditText android:id="@+id/inputPlayerAge" android:layout_width="130dp" android:layout_height="50dp" android:layout_marginTop="24dp" android:layout_marginEnd="33dp" android:inputType="number" android:maxLength="2" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@+id/inputPlayerName" /> <EditText android:id="@+id/inputPlayerHcp" android:layout_width="130dp" android:layout_height="50dp" android:layout_marginTop="24dp" android:layout_marginEnd="33dp" android:inputType="number" android:maxLength="2" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@+id/inputPlayerAge" /> <Button android:id="@+id/enterPlayer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="148dp" android:layout_marginTop="96dp" android:layout_marginEnd="148dp" android:onClick="enterPlayer" android:text="Enter" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/inputPlayerHcp" /> </android.support.constraint.ConstraintLayout> 的布局的XML,我从以下位置获取用户输入:

p1Name.setText(name);

如果我注释掉p1Name行,那么它工作正常,我敬酒了一下以检查名称是否被使用,并将名称设置为输入。因此2019-01-24 23:29:39.376 9545-9545/com.example.android.golfhandicap E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.android.golfhandicap, PID: 9545 java.lang.IllegalStateException: Could not execute method for android:onClick at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390) at android.view.View.performClick(View.java:6294) at android.view.View$PerformClick.run(View.java:24770) at android.os.Handler.handleCallback(Handler.java:790) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) at android.view.View.performClick(View.java:6294)  at android.view.View$PerformClick.run(View.java:24770)  at android.os.Handler.handleCallback(Handler.java:790)  at android.os.Handler.dispatchMessage(Handler.java:99)  at android.os.Looper.loop(Looper.java:164)  at android.app.ActivityThread.main(ActivityThread.java:6494)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at com.example.android.golfhandicap.MainActivity.enterPlayer(MainActivity.java:65) at java.lang.reflect.Method.invoke(Native Method)  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)  at android.view.View.performClick(View.java:6294)  at android.view.View$PerformClick.run(View.java:24770)  at android.os.Handler.handleCallback(Handler.java:790)  at android.os.Handler.dispatchMessage(Handler.java:99)  at android.os.Looper.loop(Looper.java:164)  at android.app.ActivityThread.main(ActivityThread.java:6494)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)  似乎有问题。我已经检查并三重检查了所有ID是否正确。

我已经花了一个多小时试图找出答案,却看不到我在做什么错!

这是我在logcat上看到的行(所有这些行都以红色显示)

char*

任何帮助都会感激不尽。

1 个答案:

答案 0 :(得分:1)

问题仅仅是由于以下原因:

  1. 您尚未将视图与findViewById绑定。这是因为以下代码:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.front_page);
    
        // Here you haven't bind the view.
    }
    

您应该将视图直接与views()方法绑定。因此,将代码更改为此:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.front_page);

        // bind the view.
        views();
    }
  1. 您每次使用此方法单击按钮时都在更改布局视图:

    public void addScore(View view) {
        setContentView(R.layout.input_page);
    }
    

这已成为您问题的根源之一。首先,不建议您这样做,因为您redrawing entire layout when calling setContentView。其次,您需要确保在代码中实现了所有布局onClick

  1. 最后但并非最不重要的一点是,最好在代码中使用setOnClickListener属性,而不是在布局中使用onClick属性。这是因为您应该将逻辑和UI分开。