java.lang.RuntimeException: Unable to start activity ComponentInfo{star.amv.zeroamv/star.amv.zeroamv.MainActivity}: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.Button
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2666)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2727)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1478)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6121)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:892)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:782)
Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.Button
at star.amv.zeroamv.MainActivity.onCreate(MainActivity.java:44)
at android.app.Activity.performCreate(Activity.java:6698)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2619)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2727)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1478)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6121)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:892)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:782)
Код
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.material.snackbar.Snackbar;
import com.google.firebase.FirebaseApp;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseAuthException;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.rengwuxian.materialedittext.MaterialEditText;
import star.amv.zeroamv.Models.User;
import static star.amv.zeroamv.R.layout.activity_main;
public class MainActivity extends AppCompatActivity {
Button btnSignIn, btnRegister;
FirebaseAuth auth;
FirebaseDatabase db;
DatabaseReference users;
RelativeLayout root;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(activity_main);
btnSignIn=findViewById(R.id.btnSignIn);
btnRegister=findViewById(R.id.btnRegister);
root = findViewById(R.id.root_element);
auth = FirebaseAuth.getInstance();
db = FirebaseDatabase.getInstance();
users = db.getReference("Users");
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showRegisterWindow();
}
});
}
private void showRegisterWindow() {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("Зарегистрироваться");
dialog.setMessage("Введите все данные для регистрации");
LayoutInflater inflater = LayoutInflater.from(this);
@SuppressLint("InflateParams") View register_window = inflater.inflate(R.layout.registar_window, null);
dialog.setView(register_window);
final MaterialEditText email = register_window.findViewById(R.id.emailField);
final MaterialEditText pass = register_window.findViewById(R.id.passField);
final MaterialEditText name = register_window.findViewById(R.id.nameField);
final MaterialEditText phone = register_window.findViewById(R.id.phoneField);
dialog.setNegativeButton("Отменить", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
dialogInterface.dismiss();
}
});
dialog.setPositiveButton("Добавить", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
if (TextUtils.isEmpty(email.getText().toString())) {
Snackbar.make(root, "Введите вашу почту", Snackbar.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(name.getText().toString())) {
Snackbar.make(root, "Введите ваше имя", Snackbar.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(phone.getText().toString())) {
Snackbar.make(root, "Введите ваш тедефон", Snackbar.LENGTH_SHORT).show();
return;
}
if (pass.getText().toString().length() < 5) {
Snackbar.make(root, "Введите пароль, которое более 5 символов", Snackbar.LENGTH_SHORT).show();
return;
}
// Регистрация пользователя
auth.createUserWithEmailAndPassword(email.getText().toString(), pass.getText().toString())
.addOnSuccessListener(new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult authResult) {
User user = new User();
user.setEmail(email.getText().toString());
user.setEmail(name.getText().toString());
user.setEmail(pass.getText().toString());
user.setEmail(phone.getText().toString());
users.child(user.getEmail())
.setValue(user)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Snackbar.make(root, "Пользователь добавлен!", Snackbar.LENGTH_SHORT).show();
}
});
}
});
}
});
dialog.show();
}
}
activity_main.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:background="@drawable/fon4"
android:layout_height="match_parent"
android:id="@+id/root_element"
tools:context=".MainActivity">
<TextView
android:id="@+id/text_bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:breakStrategy="high_quality"
android:fontFamily="@font/ranga_bold"
android:text="@string/text_bottom"
android:textColor="#070707"
android:textSize="20sp" />
<LinearLayout
android:id="@+id/btnSignIn"
android:layout_width="341dp"
android:layout_height="wrap_content"
android:layout_above="@+id/text_bottom"
android:layout_centerHorizontal="true"
android:layout_marginStart="30dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="30dp"
android:layout_marginBottom="30dp"
android:orientation="horizontal"
android:weightSum="1"
tools:ignore="ExtraText">
<Button
android:id="@+id/bntRegister"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="6dp"
android:layout_weight="1"
android:background="@drawable/btn_register"
android:text="@string/bntRegister"
android:textColor="@android:color/white"
tools:ignore="ButtonStyle" />
</LinearLayout>
<LinearLayout
android:id="@+id/btnRegister"
android:layout_width="382dp"
android:layout_height="wrap_content"
android:layout_above="@+id/text_bottom"
android:layout_centerHorizontal="true"
android:layout_marginStart="30dp"
android:layout_marginTop="60dp"
android:layout_marginEnd="60dp"
android:layout_marginBottom="82dp"
android:weightSum="1"
tools:ignore="ExtraText">
<Button
android:id="@+id/bntSignIn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginStart="2dp"
android:layout_weight="1"
android:background="@drawable/btn_sign_in"
android:text="@string/btnSignIn"
android:textColor="@android:color/white" />
</LinearLayout>
</RelativeLayout>
Button btnSignIn, btnRegister;
замените наView btnSignIn, btnRegister;