代码之家  ›  专栏  ›  技术社区  ›  shruti pandey

在提交按钮中遇到问题。从验证OTP屏幕,它不会导航到仪表板屏幕

  •  0
  • shruti pandey  · 技术社区  · 3 年前

    我做了三项活动,分别是:

    1. 森多普内
    2. 验证第二部分
    3. 仪表板

    当我点击OTP验证屏幕上的提交按钮时,它不会让我进入仪表板屏幕,我的应用程序在此之后自动关闭。谁能告诉我哪里出的错吗?

    用于验证TP2的XML代码

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView 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"
        android:overScrollMode="never"
        android:scrollbars="none"
        tools:context=".verificationOTPtwo">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_horizontal"
            android:orientation="vertical">
    
            <ImageView
                android:layout_width="130dp"
                android:layout_height="130dp"
                android:layout_marginTop="80dp"
                android:contentDescription="@string/app_name"
                android:src="@drawable/recieved" />
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="30dp"
                android:text="OTP Verififcation"
                android:textColor="@color/colorTextFirst"
                android:textSize="25sp"
                android:textStyle="bold" />
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="50dp"
                android:layout_marginLeft="50dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="50dp"
                android:gravity="center"
                android:text="Please enter the OTP sent to you"
                android:textColor="@color/colorTextSecond"
                android:textSize="14sp" />
    
            <TextView
                android:id="@+id/textmobileshownumber"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="50dp"
                android:layout_marginTop="4dp"
                android:layout_marginEnd="50dp"
                android:gravity="center"
                android:textColor="@color/colorTextFirst"
                android:textSize="14sp"
                android:textStyle="bold" />
    
            //###############################################
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="30dp"
                android:gravity="center"
                android:orientation="horizontal">
    
                <EditText
                    android:id="@+id/inputotp1"
                    android:layout_width="40dp"
                    android:layout_height="46dp"
                    android:background="@drawable/background"
                    android:gravity="center"
                    android:imeOptions="actionNext"
                    android:importantForAutofill="no"
                    android:inputType="number"
                    android:maxLength="1"
                    android:textColor="@color/colorTextFirst"
                    android:textSize="24sp"
                    android:textStyle="bold"
                    tools:ignore="LabelFor" />
    
                <EditText
                    android:id="@+id/inputotp2"
                    android:layout_width="40dp"
                    android:layout_height="46dp"
                    android:layout_marginStart="4dp"
                    android:layout_marginLeft="4dp"
                    android:background="@drawable/background"
                    android:gravity="center"
                    android:imeOptions="actionNext"
                    android:importantForAutofill="no"
                    android:inputType="number"
                    android:maxLength="1"
                    android:textColor="@color/colorTextFirst"
                    android:textSize="24sp"
                    android:textStyle="bold"
                    tools:ignore="LabelFor" />
    
                <EditText
                    android:id="@+id/inputotp3"
                    android:layout_width="40dp"
                    android:layout_height="46dp"
                    android:layout_marginStart="4dp"
                    android:layout_marginLeft="4dp"
                    android:background="@drawable/background"
                    android:gravity="center"
                    android:imeOptions="actionNext"
                    android:importantForAutofill="no"
                    android:inputType="number"
                    android:maxLength="1"
                    android:textColor="@color/colorTextFirst"
                    android:textSize="24sp"
                    android:textStyle="bold"
                    tools:ignore="LabelFor" />
    
                <EditText
                    android:id="@+id/inputotp4"
                    android:layout_width="40dp"
                    android:layout_height="46dp"
                    android:layout_marginStart="4dp"
                    android:layout_marginLeft="4dp"
                    android:background="@drawable/background"
                    android:gravity="center"
                    android:imeOptions="actionNext"
                    android:importantForAutofill="no"
                    android:inputType="number"
                    android:maxLength="1"
                    android:textColor="@color/colorTextFirst"
                    android:textSize="24sp"
                    android:textStyle="bold"
                    tools:ignore="LabelFor" />
    
                <EditText
                    android:id="@+id/inputotp5"
                    android:layout_width="40dp"
                    android:layout_height="46dp"
                    android:layout_marginStart="4dp"
                    android:layout_marginLeft="4dp"
                    android:background="@drawable/background"
                    android:gravity="center"
                    android:imeOptions="actionNext"
                    android:importantForAutofill="no"
                    android:inputType="number"
                    android:maxLength="1"
                    android:textColor="@color/colorTextFirst"
                    android:textSize="24sp"
                    android:textStyle="bold"
                    tools:ignore="LabelFor" />
    
                <EditText
                    android:id="@+id/inputotp6"
                    android:layout_width="40dp"
                    android:layout_height="46dp"
                    android:layout_marginStart="4dp"
                    android:layout_marginLeft="4dp"
                    android:background="@drawable/background"
                    android:gravity="center"
                    android:imeOptions="actionNext"
                    android:importantForAutofill="no"
                    android:inputType="number"
                    android:maxLength="1"
                    android:textColor="@color/colorTextFirst"
                    android:textSize="24sp"
                    android:textStyle="bold"
                    tools:ignore="LabelFor" />
    
    
            </LinearLayout>
    
            //################################################
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:gravity="center"
                android:orientation="horizontal">
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Not received OTP ?"
                    android:textColor="@color/colorTextSecond"
                    android:textSize="14sp" />
    
                <TextView
                    android:id="@+id/textresendotp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="8dp"
                    android:layout_marginLeft="8dp"
                    android:text="RESEND OTP AGAIN"
                    android:textColor="@color/colorTextFirst"
                    android:textSize="15sp"
                    android:textStyle="bold" />
    
            </LinearLayout>
    
            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
    
                <Button
                    android:id="@+id/buttonotpsubmit"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="30dp"
                    android:background="@drawable/backgroundbutton"
                    android:text="Submit"
                    android:textColor="@color/white"
                    android:textStyle="bold" />
    
                <ProgressBar
                    android:id="@+id/progressbar_verify_otp"
                    android:layout_width="35dp"
                    android:layout_height="35dp"
                    android:layout_gravity="center"
                    android:visibility="gone" />
    
    
            </FrameLayout>
    
    
        </LinearLayout>
    
    </ScrollView>
    

    用于验证TP2的Java代码

    package com.example.animatedsplashdemo;
    
    import androidx.annotation.NonNull;
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.text.Editable;
    import android.text.TextWatcher;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ProgressBar;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import com.google.android.gms.tasks.OnCompleteListener;
    import com.google.android.gms.tasks.Task;
    import com.google.firebase.FirebaseException;
    import com.google.firebase.auth.AuthResult;
    import com.google.firebase.auth.FirebaseAuth;
    import com.google.firebase.auth.PhoneAuthCredential;
    import com.google.firebase.auth.PhoneAuthProvider;
    
    import java.util.concurrent.TimeUnit;
    
    public class verificationOTPtwo extends AppCompatActivity {
    
        EditText inputnumber1, inputnumber2, inputnumber3, inputnumber4, inputnumber5, inputnumber6;
        String getotpbackend;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_verification_otptwo);
    
            final Button verifyButtonClick = findViewById(R.id.buttonotpsubmit);
    
    
            inputnumber1 = findViewById(R.id.inputotp1);
            inputnumber2 = findViewById(R.id.inputotp2);
            inputnumber3 = findViewById(R.id.inputotp3);
            inputnumber4 = findViewById(R.id.inputotp4);
            inputnumber5 = findViewById(R.id.inputotp5);
            inputnumber6 = findViewById(R.id.inputotp6);
    
            TextView textView = findViewById(R.id.textmobileshownumber);
            textView.setText(String.format(
                    "+91-%s", getIntent().getStringExtra("mobile")
    
            ));
    
            getotpbackend = getIntent().getStringExtra("backendotp");
    
            final ProgressBar progressBarverifyotp = findViewById(R.id.progressbar_verify_otp);
    
            verifyButtonClick.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (!inputnumber1.getText().toString().trim().isEmpty() && !inputnumber2.getText().toString().trim().isEmpty() && !inputnumber3.getText().toString().trim().isEmpty() && !inputnumber4.getText().toString().trim().isEmpty() && !inputnumber5.getText().toString().trim().isEmpty() && !inputnumber6.getText().toString().trim().isEmpty()) {
    
                        String entercodeotp = inputnumber1.getText().toString() +
                                inputnumber2.getText().toString() +
                                inputnumber3.getText().toString() +
                                inputnumber4.getText().toString() +
                                inputnumber5.getText().toString() +
                                inputnumber6.getText().toString();
    
                        if (getotpbackend != null) {
                            progressBarverifyotp.setVisibility(View.VISIBLE);
                            verifyButtonClick.setVisibility(View.INVISIBLE);
    
                            PhoneAuthCredential phoneAuthCredential = PhoneAuthProvider.getCredential(
                                    getotpbackend, entercodeotp
                            );
    
                            FirebaseAuth.getInstance().signInWithCredential(phoneAuthCredential)
                                    .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                                        @Override
                                        public void onComplete(@NonNull Task<AuthResult> task) {
                                            progressBarverifyotp.setVisibility(View.GONE);
                                            verifyButtonClick.setVisibility(View.VISIBLE);
    
    
                                            if (task.isSuccessful()) {
                                                Intent intent = new Intent(getApplicationContext(), dashboard.class);
                                                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                                startActivity(intent);
    
    
                                            } else {
                                                Toast.makeText(verificationOTPtwo.this, "Enter the correct OTP", Toast.LENGTH_SHORT).show();
                                            }
    
    
                                        }
                                    });
    
                        } else {
                            Toast.makeText(verificationOTPtwo.this, "Please check internet connection", Toast.LENGTH_SHORT).show();
                        }
    
                        //        Toast.makeText(verificationOTPtwo.this, "OTP Verified", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(verificationOTPtwo.this, "Please enter the OTP", Toast.LENGTH_SHORT).show();
                    }
                }
            });
    
            numberotpmove();
    
    
            TextView resendlabel = findViewById(R.id.textresendotp);
    
            resendlabel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    PhoneAuthProvider.getInstance().verifyPhoneNumber(
                            "+91" + getIntent().getStringExtra("mobile"),
                            60,
                            TimeUnit.SECONDS,
                            verificationOTPtwo.this,
                            new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
                                @Override
                                public void onVerificationCompleted(@NonNull PhoneAuthCredential phoneAuthCredential) {
    
    
                                }
    
                                @Override
                                public void onVerificationFailed(@NonNull FirebaseException e) {
    
                                    Toast.makeText(verificationOTPtwo.this, e.getMessage(), Toast.LENGTH_SHORT).show();
    
                                }
    
                                @Override
                                public void onCodeSent(@NonNull String newbackendotp, @NonNull PhoneAuthProvider.ForceResendingToken forceResendingToken) {
    
                                    getotpbackend = newbackendotp;
                                    Toast.makeText(verificationOTPtwo.this, "OTP sended successfully", Toast.LENGTH_SHORT).show();
    
    
                                }
                            }
                    );
    
                }
            });
    
    
        }
    
        private void numberotpmove() {
    
            inputnumber1.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
                }
    
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if (!s.toString().trim().isEmpty()) {
                        inputnumber2.requestFocus();
                    }
    
                }
    
                @Override
                public void afterTextChanged(Editable s) {
    
                }
            });
    
            inputnumber2.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
                }
    
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if (!s.toString().trim().isEmpty()) {
                        inputnumber3.requestFocus();
                    }
    
                }
    
                @Override
                public void afterTextChanged(Editable s) {
    
                }
            });
    
            inputnumber3.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
                }
    
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if (!s.toString().trim().isEmpty()) {
                        inputnumber4.requestFocus();
                    }
    
                }
    
                @Override
                public void afterTextChanged(Editable s) {
    
                }
            });
    
            inputnumber4.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
                }
    
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if (!s.toString().trim().isEmpty()) {
                        inputnumber5.requestFocus();
                    }
    
                }
    
                @Override
                public void afterTextChanged(Editable s) {
    
                }
            });
    
            inputnumber5.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
                }
    
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if (!s.toString().trim().isEmpty()) {
                        inputnumber6.requestFocus();
                    }
    
                }
    
                @Override
                public void afterTextChanged(Editable s) {
    
                }
            });
        }
    }
    

    仪表板爪哇:

    package com.example.animatedsplashdemo;
    
    import android.os.Bundle;
    
    import androidx.appcompat.app.ActionBarDrawerToggle;
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.appcompat.widget.Toolbar;
    import androidx.drawerlayout.widget.DrawerLayout;
    
    import com.google.android.material.navigation.NavigationView;
    
    public class dashboard extends AppCompatActivity {
    
        DrawerLayout drawerLayout;
        NavigationView navigationView;
        Toolbar toolbar;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_dashboard);
    
            drawerLayout = findViewById(R.id.drawerlayout);
            navigationView = findViewById(R.id.navigationview);
            toolbar = findViewById(R.id.toolbar);
    
            ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.navigation_open, R.string.navigation_close);
    
            drawerLayout.addDrawerListener(toggle);
            toggle.syncState();
    
            setSupportActionBar(toolbar);
        }
    }
    

    Logcat:

        2022-04-03 01:27:14.483 21162-21162/com.example.animatedsplashdemo 
           E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.animatedsplashdemo, PID: 21162
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.animatedsplashdemo/com.example.animatedsplashdemo.dashboard}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3792)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3968)
        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:2307)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:246)
        at android.app.ActivityThread.main(ActivityThread.java:8506)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1139)
     Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
        at androidx.appcompat.app.AppCompatDelegateImpl.setSupportActionBar(AppCompatDelegateImpl.java:581)
        at androidx.appcompat.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:183)
        at com.example.animatedsplashdemo.dashboard.onCreate(dashboard.java:32)
        at android.app.Activity.performCreate(Activity.java:8198)
        at android.app.Activity.performCreate(Activity.java:8182)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3765)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3968) 
        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:2307) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:246) 
        at android.app.ActivityThread.main(ActivityThread.java:8506) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1139)
    

     Â

    0 回复  |  直到 3 年前
        1
  •  0
  •   Vishal Chauhan    3 年前

    设置 窗口操作栏 在应用主题中为false。

     <item name="windowActionBar">false</item>
    

    希望这对你有帮助。