0

I'm learning how to user Android Studio and I'm working on an app that is a form, like the ones you would register as a user at a website. You add a username, your full name, email address and then use a DatePicker to select your date of birth. Then you click a "submit" button and it would then navigate to a "Thank You "username" page.

Everything was working until I've tried to add a "Up" navigation button would return to the previous form, thereby starting from scratch with a blank form. Now when I try to submit the form, it would abruptly crash with a "SimpleRegistrationForm has stopped" error. I thought I've followed the steps verbatim, but did I miss something? , but due to my inexperience, I'm not sure what I've didn't implement. Any assistance would be appreciated and upvoted.

PS:

(This article may be similar to: https://stackoverflow.com/questions/43403402/android-up-button-isnt-working)

(I've consulted: https://developer.android.com/training/appbar/up-action.html) 

Here the changes I've did (the following changes are in bold or if not in bold, then are marked by "**" to surround the text:

My activity_display.message.xml file:

<?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:id="@+id/activity_display_message"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".DisplayMessageActivity">

    **<android.support.v7.widget.Toolbar
        android:id="@+id/my_child_toolbar"
        android:layout_width="match_parent"
        android:layout_height="29dp"
        android:background="?attr/colorPrimary"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />**


    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="80dp"
        android:text="@string/user_Name"
        android:textColor="@color/colorPrimary"
        android:textSize="24sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/thank_you"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="2dp"
        android:contentDescription="@string/thank_you_for_signing_up"
        android:text="@string/thank_you_for_signing_up"
        android:textAlignment="center"
        android:textSize="24sp"
        app:layout_constraintBottom_toTopOf="@+id/textView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/my_child_toolbar"
        tools:text="@string/thank_you_for_signing_up" />

    <!--<Button
        android:id="@+id/back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="finishActivity"
        android:layout_marginBottom="391dp"
        android:layout_marginTop="164dp"
        android:text="Back"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />-->

</android.support.constraint.ConstraintLayout>

My DisplayMessageActivty.java file (Some sections of code are commented out):

package com.example.simpleregistrationform.feature;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.support.v7.widget.Toolbar;
import android.support.v7.app.ActionBar;


public class DisplayMessageActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_message);
        **// my_child_toolbar is defined in the layout file
        Toolbar myChildToolbar = findViewById(R.id.my_child_toolbar);
        setSupportActionBar(myChildToolbar);

        // Get a support ActionBar corresponding to this toolbar
        ActionBar ab = getSupportActionBar();

        // Enable the Up button
        ab.setDisplayHomeAsUpEnabled(true);**

        // Get the Intent that started this activity and extract the string
        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

        // Capture the layout's TextView and set the string as its text
        TextView textView = findViewById(R.id.textView);
        textView.setText(message);
    }

    /*public void finishActivity(View v){
        finish();
    }*/

}

And my AndroidManifest.xml file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.simpleregistrationform.app">

    **<application>

        <!-- The main/home activity (it has no parent activity) -->

        <activity android:name="com.example.simpleregistrationform.feature.MainActivity">

        </activity>

        <!-- A child of the main activity -->
        <activity
            android:name="com.example.simpleregistrationform.feature.DisplayMessageActivity"
            android:label="@string/my_child_toolbar"
            android:parentActivityName="com.example.simpleregistrationform.feature.MainActivity">

            <!-- Parent activity meta-data to support 4.0 and lower -->
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.simpleregistrationform.feature.MainActivity" />
        </activity>
    </application>**


</manifest>

This is what my error log is showing me:

enter image description here

user71311
  • 11
  • 2
  • 8

1 Answers1

0

You have already set the ActionBar with this line setSupportActionBar(myChildToolbar); Why added it a second time ActionBar ab = getSupportActionBar();.

Simply remove the second line!To add other attributes simply write getSupportActionBar(); instead of ab