当前位置: 首页 > 知识库问答 >
问题:

RuntimeException:无法启动activity ComponentInfo。有一些错误,我已经尝试了很多次,但是这个logcat问题

姜华翰
2023-03-14
2021-01-09 20:43:41.905 12890-12890/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
        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);
            }
        }
    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;
                }
            });
    
    
    
        }
    
    }
<?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个答案

冯招
2023-03-14

您在例外情况中得到了以下内容:

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

这意味着您正在尝试将TextView强制转换为EditText。所以我猜这两个中的一个:

private EditText mPhoneNumber, mCode;

是XML布局中的TextView。

<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>
 类似资料: