0
votes

when i try to run this app after enter the emulator when it's try to open the apps suddenly it's stop.

This is my logcat

2021-01-09 20:43:41.905 12890-12890/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main

Process: com.example.myapplication, PID: 12890 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.MainActivity}: java.lang.ClassCastException: com.google.android.material.textview.MaterialTextView cannot be cast to android.widget.EditText at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:223) at android.app.ActivityThread.main(ActivityThread.java:7656) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) Caused by: java.lang.ClassCastException: com.google.android.material.textview.MaterialTextView cannot be cast to android.widget.EditText at com.example.myapplication.MainActivity.onCreate(MainActivity.java:44) at android.app.Activity.performCreate(Activity.java:8000) at android.app.Activity.performCreate(Activity.java:7984) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)  at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)  at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)  at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)  at android.os.Handler.dispatchMessage(Handler.java:106)  at android.os.Looper.loop(Looper.java:223)  at android.app.ActivityThread.main(ActivityThread.java:7656)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 

mainactivity

        public class MainActivity extends AppCompatActivity {
            private EditText mPhoneNumber, mCode;
            private Button msend;
        private  PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;
            String mVerificationId;
        
        
            @SuppressLint("WrongViewCast")
        
        
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                FirebaseApp.initializeApp(this);
        
                userIsLoggedIn();
        
                mPhoneNumber = findViewById(R.id.phoneNumber);
                        mCode = findViewById (R.id.code);
                        msend= findViewById(R.id.send);
        
                        msend.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (mVerificationId!=null)
                                    verifyPhoneNumberWithCode();
                                else
                                    startPhoneNumberVerification();
                            }
        
        
                        });
        
                mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks(){
        
                    public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential){
                        signInWithPhoneAuthCredential(phoneAuthCredential);
                    }
        
                    @Override
                    public void onVerificationFailed(FirebaseException e) {
        
                    }
        
                    @Override
                    public void onCodeSent(String verificationeId, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
                        super.onCodeSent(verificationeId, forceResendingToken);
                        mVerificationId = verificationeId;
                        msend.setText("Verify Code");
                    }
                };
        
            }
        private void  verifyPhoneNumberWithCode(){
                PhoneAuthCredential credential= PhoneAuthProvider.getCredential(mVerificationId, mCode.getText().toString());
                signInWithPhoneAuthCredential(credential);
        }
            private void signInWithPhoneAuthCredential(PhoneAuthCredential phoneAuthCredential){
        
                FirebaseAuth .getInstance().signInWithCredential(phoneAuthCredential).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                          if(task.isSuccessful())
                              userIsLoggedIn();
                    }
                });
            }
        
            private void userIsLoggedIn() {
                FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
                if (user !=null){
                    startActivity(new Intent(getApplicationContext(),MainActivity.class));
                    finish();
                    return;
                }
            }
        
            private void startPhoneNumberVerification() {
        
                PhoneAuthProvider.getInstance().verifyPhoneNumber(
                        mPhoneNumber.getText().toString(),
                        60,
                        TimeUnit.SECONDS,
                        this,
                      mCallbacks);
            }
        }

2nd activity

    package com.example.myapplication;
    public class MainPageActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main_page);
    
            Button mlogout = findViewById(R.id.logout);
            mlogout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    FirebaseAuth.getInstance().signOut();
                    Intent intent= new Intent(getApplicationContext(), MainActivity.class);
                    //to clear the activity after logout instant
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);
                    finish();
                    return;
                }
            });
    
    
    
        }
    
    }

activitymainxml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    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="MainActivity"
    android:layout_alignParentTop="true"
    tools:actionBarNavMode="standard"
    android:orientation="vertical">


    <TextView
        android:id="@+id/phoneNumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Enter Phone Number" />

    <TextView

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Enter Code"
        android:id="@+id/code"/>

    <Button

        android:id="@+id/send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Send Verification Code" />

</LinearLayout>
1

1 Answers

1
votes

You got this in your exception:

java.lang.ClassCastException: com.google.android.material.textview.MaterialTextView cannot be cast to android.widget.EditText

This means you are trying to cast TextView to EditText. So I guess one of these two:

private EditText mPhoneNumber, mCode;

is TextView inside your XML layout.

EDIT: Just saw you have posted your XML code. So your mPhoneMumber and mCode are both TextView. If you want them to be EditText just use EditText. But since you are using MaterialTheme I guess then try to use following:

<com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        
        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:text="wadwada"
            android:layout_height="wrap_content"/>
    </com.google.android.material.textfield.TextInputLayout>

For more about this check this link on Material Design: https://material.io/components/text-fields/android#using-text-fields