-1

I am relatively new to coding but have some experience in Java and Android. I have a really basic problem that I have done over and over again but for some reason, this occurrence doesn't work!

I have an edittext field which is populated by the user. When an enter button is pressed the app then takes this and saves it as a String. The String is then used to populate the TextView with the user input.

I have created the findviewbyids, I have set the String to equal the edittext input and then set the text in the text view to be the String as per the code below. I have checked previous apps I have made and this has always worked before...

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



}
}

The XML for the page with the text view I want to display the String in:

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

The XML for the layout with the edittext which I get the user input from:

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

If I comment out the line p1Name.setText(name); then it works fine, I have a toast to check the name is taken and name is set as the input. So there seems to be something wrong with the p1Name. I have checked and triple checked all the IDs are correct.

I have spent over an hour now trying to figure it out and can't see what I am doing wrong!

This is the lines I get on logcat (These are all the lines shown in red)

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) 

Any help would be gratefully received.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Danny Jebb
  • 39
  • 5
  • 1
    any error in logcat? please post the xml tag for id `playerOneName` – Kartik Jan 24 '19 at 23:31
  • I seem to get 2 errors looking at Logcat (Never used it before so not sure if these are helpful, – Danny Jebb Jan 24 '19 at 23:31
  • The first error is InvocationTargetException the second is a nullpointerexception. – Danny Jebb Jan 24 '19 at 23:32
  • This is the xml for the textview of playerOneName – Danny Jebb Jan 24 '19 at 23:34
  • I am still new to android studio, I have just started a java module as part of my degree. My logcat contains massive amounts of info, the 2 errors above are what I believe the problem is. But I have not yet done any studying of the logcat errors. – Danny Jebb Jan 24 '19 at 23:36
  • Please edit your original post and paste all your relevante code there. That would be: MainActivity and XML for Main Activity (or the relevant activity files). It is hard to know the problem without the full code – S. Czop Jan 24 '19 at 23:39
  • Thanks I have edited the original question. Some of the xml files aren't fully finished but these are text and edittext views I am not yet trying to use. – Danny Jebb Jan 24 '19 at 23:50
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Kartik Jan 25 '19 at 00:06

1 Answers1

1

The problem is simply because of the following:

  1. you haven't bind the view with the findViewById. This is because of the following code:

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

you should directly bind the view with your views() method. So, change the code to this:

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

        // bind the view.
        views();
    }
  1. You're changing the layout view each time a button is clicked with this method:

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

which is become one of the source for your problem. First, it's not recommended because you're redrawing entire layout when calling setContentView. Second, you need to make sure all the layout onClick is implemented in your code.

  1. Last but not the least, you better using setOnClickListener in code instead onClick attribute in layout. It is because you should separate between logic and UI.
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
  • Thanks very much for your response. I have changed the findviewbtids into the onCreate method but it didn't help a great deal. Some of the other things you mentioned I am not familiar with. I have used onClickListener in another app but it was mainly copied from a solution online. I need to read up on how to do it properly – Danny Jebb Jan 25 '19 at 22:15
  • Long story short, you just need to set the layout and bind the view on the layout to the code before the button `onClick` can works. You have the problem only because you haven't familiarize yourself with how Android code works. Just take your time to learn a bit deeper ;) – ישו אוהב אותך Jan 26 '19 at 03:41