0

I get a NullPointerException when I try:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stats);

        final EditText nameText = (EditText) findViewById(R.id.creatureName);

        String nameDisplay = nameText.getText().toString(); // <--- Error here

        TextView name = (TextView) findViewById(R.id.textViewStatsName);
        name.setText(nameDisplay);

        goBackButton();
    }

The EditText exists on the MainActivity, while I try to access it when another activity is created. I want to have the same text as written on the EditText on the (empty) TextView.

I have seen that there are quite simular questions here, but sadly I never found any answer to the question. Also I am quite new to Android Studio and programming apps, so if I have done anything wrong in the code, please explain.

Thanks in advance.

EDIT:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.something.creature.StatsActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Go back"
        android:id="@+id/backButton"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_marginStart="18dp"
        android:layout_marginBottom="18dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Health Points:"
        android:id="@+id/textHP"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="114dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Strength Level:"
        android:id="@+id/testSTRlvl"
        android:layout_marginTop="44dp"
        android:layout_below="@+id/textHP"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/textViewStatsName"
        android:layout_centerHorizontal="true"/>
</RelativeLayout>
Jonilo5
  • 45
  • 8

0 Answers0