การใช้ฟิลด์แก้ไขข้อความและแสดงใน TextView

ฉันค่อนข้างใหม่กับการเขียนโค้ด แต่มีประสบการณ์ใน Java และ Android มาบ้างแล้ว ฉันมีปัญหาพื้นฐานจริงๆ ที่ทำซ้ำแล้วซ้ำเล่า แต่ด้วยเหตุผลบางอย่าง เหตุการณ์นี้ไม่ได้ผล!

ฉันมีฟิลด์ edittext ที่ผู้ใช้กรอก เมื่อกดปุ่ม Enter แอปจะนำสิ่งนี้และบันทึกเป็นสตริง จากนั้นสตริงจะถูกนำมาใช้เพื่อเติม TextView ด้วยอินพุตของผู้ใช้

ฉันได้สร้าง findviewbyids แล้ว ฉันได้ตั้งค่า String ให้เท่ากับอินพุต 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);



}
}

XML สำหรับเพจที่มีมุมมองข้อความฉันต้องการแสดง 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>

ถ้าฉันใส่เครื่องหมายความคิดเห็นในบรรทัด 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) 

ความช่วยเหลือใด ๆ จะได้รับการขอบคุณ


person Danny Jebb    schedule 24.01.2019    source แหล่งที่มา
comment
มีข้อผิดพลาดใด ๆ ใน logcat หรือไม่? โปรดโพสต์แท็ก xml สำหรับ id playerOneName   -  person Kartik    schedule 25.01.2019
comment
ฉันดูเหมือนจะได้รับข้อผิดพลาด 2 ข้อในการดู Logcat (ไม่เคยใช้มาก่อน ดังนั้นไม่แน่ใจว่าสิ่งเหล่านี้มีประโยชน์หรือไม่   -  person Danny Jebb    schedule 25.01.2019
comment
ข้อผิดพลาดแรกคือ InvocationTargetException ส่วนข้อผิดพลาดที่สองคือ nullpointerException   -  person Danny Jebb    schedule 25.01.2019
comment
นี่คือ xml สำหรับมุมมองข้อความของ playerOneName ‹TextView android:id=@+id/playerOneName android:layout_width=136dp android:layout_height=30dp android:layout_marginStart=8dp android:editable=false android:layout_marginTop=32dp android:visibility=invisible แอป:layout_constraintStart_toStartOf=แอปหลัก:layout_constraintTop_toBottomOf=@+id/appName2 /›   -  person Danny Jebb    schedule 25.01.2019
comment
ฉันยังใหม่กับ Android Studio ฉันเพิ่งเริ่มโมดูล Java ซึ่งเป็นส่วนหนึ่งของปริญญาของฉัน Logcat ของฉันมีข้อมูลจำนวนมหาศาล ข้อผิดพลาด 2 ข้อด้านบนคือสิ่งที่ฉันเชื่อว่าเป็นปัญหา แต่ฉันยังไม่ได้ศึกษาข้อผิดพลาดของ logcat เลย   -  person Danny Jebb    schedule 25.01.2019
comment
โปรดแก้ไขโพสต์ต้นฉบับของคุณและวางโค้ดที่เกี่ยวข้องทั้งหมดที่นั่น นั่นจะเป็น: MainActivity และ XML สำหรับกิจกรรมหลัก (หรือไฟล์กิจกรรมที่เกี่ยวข้อง) เป็นการยากที่จะทราบปัญหาหากไม่มีรหัสเต็ม   -  person S. Czop    schedule 25.01.2019
comment
ขอบคุณฉันได้แก้ไขคำถามเดิมแล้ว ไฟล์ xml บางไฟล์ยังสร้างไม่เสร็จสมบูรณ์ แต่เป็นมุมมองข้อความและข้อความแก้ไขที่ฉันยังไม่ได้ลองใช้   -  person Danny Jebb    schedule 25.01.2019
comment
อาจซ้ำกันของ NullPointerException คืออะไร และฉันจะทำอย่างไร แก้ไขมัน?   -  person Kartik    schedule 25.01.2019


คำตอบ (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);
    }
    

ซึ่งกลายเป็นหนึ่งในต้นตอของปัญหาของคุณ อันดับแรก ไม่แนะนำเนื่องจากคุณ วาดเค้าโครงทั้งหมดใหม่ เมื่อเรียก setContentView ประการที่สอง คุณต้องแน่ใจว่ามีการใช้เลย์เอาต์ onClick ทั้งหมดในโค้ดของคุณ

  1. สุดท้ายแต่ไม่ท้ายสุด คุณควรใช้ setOnClickListener ในโค้ดแทน onClick คุณลักษณะในเลย์เอาต์ เป็นเพราะคุณควรแยกระหว่างตรรกะและ UI
person ישו אוהב אותך    schedule 25.01.2019
comment
ขอบคุณมากสำหรับการตอบกลับของคุณ ฉันได้เปลี่ยน findviewbtids เป็นเมธอด onCreate แล้ว แต่มันก็ไม่ได้ช่วยอะไรมากนัก เรื่องอื่นๆ ที่คุณพูดถึงผมไม่คุ้นเคย ฉันใช้ onClickListener ในแอปอื่น แต่ส่วนใหญ่คัดลอกมาจากโซลูชันออนไลน์ ฉันต้องอ่านวิธีการทำอย่างถูกต้อง - person Danny Jebb; 26.01.2019
comment
เรื่องสั้นสั้น คุณเพียงแค่ต้องตั้งค่าเลย์เอาต์และผูกมุมมองบนเลย์เอาต์เข้ากับโค้ดก่อนที่ปุ่ม onClick จะสามารถทำงานได้ คุณประสบปัญหาเพียงเพราะคุณไม่คุ้นเคยกับวิธีการทำงานของโค้ด Android ใช้เวลาเรียนรู้ให้ลึกซึ้งยิ่งขึ้นอีกหน่อย ;) - person ישו אוהב אותך; 26.01.2019