【问题标题】:Taking an edittext field and displaying in a TextView获取一个编辑文本字段并在 TextView 中显示
【发布时间】:2019-01-24 23:24:39
【问题描述】:

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

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

我创建了findviewbyids,我将字符串设置为等于edittext 输入,然后按照下面的代码将文本视图中的文本设置为字符串。我检查了我以前制作的应用程序,这一直有效...

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的文本视图页面的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"
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>

带有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">


<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>

如果我注释掉 p1Name.setText(name); 行,那么它工作正常,我有一个祝酒词来检查名称是否被采用,并且名称被设置为输入。所以p1Name 似乎有问题。我已经检查并三次检查了所有 ID 是否正确。

我已经花了一个多小时试图弄清楚,但看不出我做错了什么!

这是我在 logcat 上得到的行(这些都是红色显示的行)

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) 

如有任何帮助,我们将不胜感激。

【问题讨论】:

  • logcat 有什么错误吗?请张贴 id playerOneName 的 xml 标签
  • 我似乎在查看 Logcat 时遇到了 2 个错误(以前从未使用过,所以不确定这些是否有帮助,
  • 第一个错误是 InvocationTargetException 第二个是空指针异常。
  • 这是 playerOneName 的 textview 的 xml
  • 我还是 android studio 的新手,我刚刚开始了一个 java 模块作为我学位的一部分。我的 logcat 包含大量信息,上面的 2 个错误是我认为的问题所在。但是我还没有对 logcat 错误进行任何研究。

标签: java android android-edittext textview illegalstateexception


【解决方案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 分开。

【讨论】:

  • 非常感谢您的回复。我已将 findviewbtids 更改为 onCreate 方法,但这并没有太大帮助。你提到的其他一些我不熟悉的事情。我在另一个应用程序中使用过 onClickListener,但它主要是从在线解决方案中复制的。我需要阅读如何正确地做到这一点
  • 长话短说,您只需要设置布局并将布局上的视图绑定到代码,按钮onClick 就可以工作。您遇到问题只是因为您不熟悉 Android 代码的工作原理。花点时间深入学习一下;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-30
  • 2012-11-04
相关资源
最近更新 更多