0

RegisterActivity.Java

This is my register activity here toolbar problem. I think app does not run.

package com.example.chatapp;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;


import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.rengwuxian.materialedittext.MaterialEditText;

import java.util.HashMap;

public class RegisterActivity extends AppCompatActivity {
    MaterialEditText username, email, password;
    Button btn_register;

    FirebaseAuth auth;
    DatabaseReference reference;

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

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("Register");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        username = findViewById(R.id.username);
        email = findViewById(R.id.email);
        password = findViewById(R.id.password);
        btn_register = findViewById(R.id.btn_register);
         auth = FirebaseAuth.getInstance();

         btn_register.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 String txt_username = username.getText().toString();
                 String txt_email = email.getText().toString();
                 String txt_password = password.getText().toString();

                 if (TextUtils.isEmpty(txt_username) || TextUtils.isEmpty(txt_email) || TextUtils.isEmpty(txt_password)) {
                     Toast.makeText(RegisterActivity.this, "All fildes are Required", Toast.LENGTH_SHORT).show();
                 } else if(txt_password.length() <  6){
                     Toast.makeText(RegisterActivity.this, "Password at least 6 characters", Toast.LENGTH_SHORT).show();
                 }else {
                     register(txt_username, txt_email, txt_password);
                 }
             }
         });

    }
    private void register(String username, String email, String password)
    {
        auth.createUserWithEmailAndPassword(email, password)
                .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if(task.isSuccessful())
                        {
                            FirebaseUser firebaseUser = auth.getCurrentUser();
                            assert firebaseUser != null;
                            String userid = firebaseUser.getUid();
                            reference = FirebaseDatabase.getInstance().getReference("Users").child(userid);
                            HashMap<String,String> hashMap = new HashMap<>();
                            hashMap.put("id", userid);
                            hashMap.put("username", username);
                            hashMap.put("imageURL",     "default");

                            reference.setValue(hashMap).addOnCompleteListener(new OnCompleteListener<Void>() {
                                @Override
                                public void onComplete(@NonNull Task<Void> task) {
                                    if(task.isSuccessful())
                                    {
                                        Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
                                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                                        startActivity(intent);
                                        finish();
                                    }

                                }
                            });
                        } else {
                            Toast.makeText(RegisterActivity.this, "You can't Register with this email or password", Toast.LENGTH_SHORT).show();
                        }
                    }
                });

    }
}

activity_registert.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".RegisterActivity">

    <include
        android:id="@+id/toolbar"
        layout="@layout/bar_layout"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:gravity="center_horizontal"
        android:layout_below="@id/toolbar"
        android:padding="16dp"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Create new Account"
            android:textSize="20sp"
            android:textStyle="bold"
            >
        </TextView>
    <com.rengwuxian.materialedittext.MaterialEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/username"
        android:layout_marginTop="10dp"
        app:met_floatingLabel="normal"
        android:hint="Username"/>

        <com.rengwuxian.materialedittext.MaterialEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/email"
            android:layout_marginTop="10dp"
            android:inputType="textEmailAddress"
            app:met_floatingLabel="normal"
            android:hint="Email"/>

        <com.rengwuxian.materialedittext.MaterialEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/password"
            android:layout_marginTop="10dp"
            android:inputType="textPassword"
            app:met_floatingLabel="normal"
            android:hint="Password"/>

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Register"
            android:id="@+id/btn_register"
            android:layout_marginTop="10dp"
            android:background="@color/design_default_color_primary_dark"
            android:textColor="#fff">

        </Button>

    </LinearLayout>

</RelativeLayout>

bar_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="wrap_content"
     android:id="@+id/toolbar"
    android:background="@color/design_default_color_primary_dark"
    >

</Toolbar>

when I click on Register Activity, app crashes. I am new android user I find errors couples of hours but I cant understand where the error is and how to solve it

--------- beginning of crash
2020-11-22 18:04:54.534 2167-2167/com.example.chatapp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.chatapp, PID: 2167
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.chatapp/com.example.chatapp.RegisterActivity}: java.lang.ClassCastException: android.widget.Toolbar cannot be cast to androidx.appcompat.widget.Toolbar
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
     Caused by: java.lang.ClassCastException: android.widget.Toolbar cannot be cast to androidx.appcompat.widget.Toolbar
        at com.example.chatapp.RegisterActivity.onCreate(RegisterActivity.java:38)
        at android.app.Activity.performCreate(Activity.java:6975)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
        at android.os.Handler.dispatchMessage(Handler.java:105) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6541) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 
2020-11-22 18:04:54.569 2167-2201/com.example.chatapp V/FA: Connection attempt already in progress
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

1 Answers1

0

In your activity, you are importing the androidx Toolbar while using a support Toolbar in the activity xml file. Inside your xml file, you need to change android.widget.Toolbar to androidx.appcompat.widget.Toolbar. This is throwing the java.lang.ClassCastException.

In your bar_layout, replace the word Toolbar with androidx.appcompat.widget.Toolbar:

<androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"/>

You also need to learn a bit about "Stack Trace", how to read it and how to try to solve your problems depending on it. Link to understand Stack Trace: What is a stack trace, and how can I use it to debug my application errors?

private static
  • 745
  • 8
  • 17