有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java SupportActionBar未针对所有活动显示

有一段时间,我把我的主题搞砸了,不知怎么搞砸了,不知为什么,我无法回头。我当前的活动栏显示了除LoginActivity类之外的所有活动。我的注册课很好

工具栏。xml

<?xml version="1.0" encoding="utf-8"?>
<安卓.support.v7.widget.Toolbar xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:app="http://schemas.安卓.com/apk/res-auto"
    安卓:id="@+id/toolbar"
    安卓:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    安卓:layout_height="wrap_content"
    安卓:layout_width="match_parent"
    安卓:layout_alignParentTop="true"
    安卓:minHeight="?attr/actionBarSize"
    安卓:background="?attr/colorPrimary" />

后勤活动。xml(显示Actionbar,但缺少按钮,为空)

<?xml version="1.0" encoding="utf-8"?>
<安卓.support.design.widget.CoordinatorLayout
    xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:app="http://schemas.安卓.com/apk/res-auto"
    xmlns:tools="http://schemas.安卓.com/tools"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    安卓:fitsSystemWindows="true"
    tools:context="com.hmrs.login.LoginActivity">

    <安卓.support.design.widget.AppBarLayout
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:theme="@style/AppTheme.AppBarOverlay">

        <安卓.support.v7.widget.Toolbar
            安卓:id="@+id/my_toolbar"
            安卓:layout_width="match_parent"
            安卓:layout_height="?attr/actionBarSize"
            安卓:background="?attr/colorPrimary"
            安卓:elevation="4dp"
            安卓:theme="@style/ThemeOverlay.AppCompat.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

    </安卓.support.design.widget.AppBarLayout>

    <include layout="@layout/toolbar"/>

    <LinearLayout
        安卓:layout_width="fill_parent"
        安卓:layout_height="match_parent"
        安卓:background="@color/colorPrimaryDark"
        安卓:gravity="center"
        安卓:orientation="vertical"
        安卓:padding="@dimen/activity_horizontal_margin">


        <TextView
            安卓:layout_width="wrap_content"
            安卓:layout_height="@dimen/logo_w_h"
            安卓:layout_gravity="center_horizontal"
            安卓:layout_marginBottom="30dp"
            安卓:text="@string/app_name"
            安卓:textSize="30sp"/>

        <安卓.support.design.widget.TextInputLayout
            安卓:layout_width="match_parent"
            安卓:layout_height="wrap_content">

            <EditText
                安卓:id="@+id/email"
                安卓:layout_width="match_parent"
                安卓:layout_height="wrap_content"
                安卓:layout_marginBottom="10dp"
                安卓:hint="@string/hint_email"
                安卓:inputType="textEmailAddress"
                安卓:textColor="@color/darkTextColor"
                安卓:textColorHint="@color/darkTextColor" />
        </安卓.support.design.widget.TextInputLayout>

        <安卓.support.design.widget.TextInputLayout
            安卓:layout_width="match_parent"
            安卓:layout_height="wrap_content">

            <EditText
                安卓:id="@+id/password"
                安卓:layout_width="fill_parent"
                安卓:layout_height="wrap_content"
                安卓:layout_marginBottom="10dp"
                安卓:hint="@string/hint_password"
                安卓:inputType="textPassword"
                安卓:textColor="@color/darkTextColor"
                安卓:textColorHint="@color/darkTextColor" />
        </安卓.support.design.widget.TextInputLayout>

        <!-- Login Button -->

        <Button
            安卓:id="@+id/btn_login"
            安卓:layout_width="fill_parent"
            安卓:layout_height="wrap_content"
            安卓:layout_marginTop="20dip"
            安卓:background="@color/white"
            安卓:text="@string/btn_login"
            安卓:textColor="@安卓:color/black"
            安卓:textAllCaps="false"/>

        <Button
            安卓:id="@+id/btn_reset_password"
            安卓:layout_width="fill_parent"
            安卓:layout_height="wrap_content"
            安卓:layout_marginTop="20dip"
            安卓:background="@color/colorAccent"
            安卓:text="@string/btn_forgot_password"
            安卓:textAllCaps="false"
            安卓:textColor="@color/darkTextColor" />


        <Button
            安卓:id="@+id/btn_signup"
            安卓:layout_width="match_parent"
            安卓:layout_height="wrap_content"
            安卓:layout_gravity="bottom"
            安卓:layout_marginTop="20dp"
            安卓:layout_weight="0"
            安卓:background="@color/colorAccent"
            安卓:gravity="center|bottom"
            安卓:text="@string/btn_link_to_register"
            安卓:textAllCaps="false"
            安卓:textColor="@color/darkTextColor"
            安卓:textSize="15dp" />

    </LinearLayout>

    <ProgressBar
        安卓:id="@+id/progressBar"
        安卓:layout_width="30dp"
        安卓:layout_height="30dp"
        安卓:layout_gravity="center|bottom"
        安卓:layout_marginBottom="20dp"
        安卓:visibility="gone" />
</安卓.support.design.widget.CoordinatorLayout>

物流活动类

public class LoginActivity extends AppCompatActivity {


    private EditText inputEmail, inputPassword;
    private FirebaseAuth auth;
    private ProgressBar progressBar;
    private Button btnSignup, btnLogin, btnReset;
    private FirebaseAuth.AuthStateListener authListener;
    private FirebaseAuth mAuth;
    private FirebaseFirestore mFirestore;
    private FirebaseAuth.AuthStateListener authStateListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(myToolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayUseLogoEnabled(true);
        }
        //Get Firebase auth instance
        mAuth = FirebaseAuth.getInstance();
        mFirestore = FirebaseFirestore.getInstance();
        FirebaseApp.initializeApp(this);


        authStateListener = new FirebaseAuth.AuthStateListener() {
            FirebaseUser firebaseUser = mAuth.getCurrentUser();
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                if (firebaseUser != null) {
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                }
            }
        };
        // set the view now
        setContentView(R.layout.activity_login);



        inputEmail = (EditText) findViewById(R.id.email);
        inputPassword = (EditText) findViewById(R.id.password);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        btnSignup = (Button) findViewById(R.id.btn_signup);
        btnLogin = (Button) findViewById(R.id.btn_login);
        btnReset = (Button) findViewById(R.id.btn_reset_password);

        //Get Firebase auth instance
        auth = FirebaseAuth.getInstance();


        btnSignup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
            }
        });

        btnReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(LoginActivity.this, ResetPasswordActivity.class));
            }
        });

        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String email = inputEmail.getText().toString();
                final String password = inputPassword.getText().toString();

                if (TextUtils.isEmpty(email)) {
                    Toast.makeText(getApplicationContext(), "Please enter a valid email address!", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (TextUtils.isEmpty(password)) {
                    Toast.makeText(getApplicationContext(), "Please enter a valid password!", Toast.LENGTH_SHORT).show();
                    return;
                }

                progressBar.setVisibility(View.VISIBLE);

                //authenticate user
                auth.signInWithEmailAndPassword(email, password)
                        .addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
                            @Override
                            public void onComplete(@NonNull Task<AuthResult> task) {
                                // If sign in fails, display a message to the user. If sign in succeeds
                                // the auth state listener will be notified and logic to handle the
                                // signed in user can be handled in the listener.
                                progressBar.setVisibility(View.GONE);
                                if (!task.isSuccessful()) {
                                    // there was an error
                                    if (password.length() < 6) {
                                        inputPassword.setError(getString(R.string.minimum_password));
                                    } else {
                                        Toast.makeText(LoginActivity.this, getString(R.string.auth_failed), Toast.LENGTH_LONG).show();
                                    }
                                } else {
                                    Intent intent = new Intent(LoginActivity.this, AccountPageActivity.class);
                                    startActivity(intent);
                                    finish();
                                }
                            }
                        });
            }
        });
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        menu.findItem(R.id.action_Close).setVisible(false); //hide cancel button
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.item_settings:
                startActivity(new Intent(getApplicationContext(), TabMenu.class));
                return true;
            case R.id.item_backup:
                startActivity(new Intent(getApplicationContext(), AccountPageActivity.class));
                return true;
            case R.id.item_rhPlus:
                startActivity(new Intent(getApplicationContext(), LoginActivity.class));
                return true;
            case R.id.item_usageguide:
                startActivity(new Intent(getApplicationContext(), ExpenseCreation.class));
                return true;
            case R.id.item_aboutUs:
                startActivity(new Intent(getApplicationContext(), AboutUs.class));
                return true;
            default:
                return super.onOptionsItemSelected(item);

        }
    }
}

RegisterActivity类(此处显示AppBar)

public class RegisterActivity extends AppCompatActivity {

    private EditText inputEmail, inputPassword, firstName;
    private Button btnSignIn, btnSignUp, btnResetPassword;
    private ProgressBar progressBar;
    private static final String TAG = "RegisterActivity";
    private FirebaseFirestore mFirestore;
    private FirebaseAuth mAuth;
    private FirebaseAuth.AuthStateListener authStateListener;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        //Get Firebase auth instance
        mAuth = FirebaseAuth.getInstance();
        mFirestore = FirebaseFirestore.getInstance();
        FirebaseApp.initializeApp(this);
        mAuth = FirebaseAuth.getInstance();
        mFirestore = FirebaseFirestore.getInstance();

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

        btnSignIn = (Button) findViewById(R.id.sign_in_button);
        btnSignUp = (Button) findViewById(R.id.sign_up_button);
        inputEmail = (EditText) findViewById(R.id.email);
        firstName = (EditText) findViewById(R.id.firstName);
        inputPassword = (EditText) findViewById(R.id.password);
        btnResetPassword = (Button) findViewById(R.id.btn_reset_password);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);

        btnResetPassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(RegisterActivity.this, ResetPasswordActivity.class));
            }
        });

        btnSignIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
                finish();
            }
        });

        authStateListener = new FirebaseAuth.AuthStateListener() {
            FirebaseUser firebaseUser = mAuth.getCurrentUser();
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                if (firebaseUser != null) {
                    Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                }
            }
        };


        btnSignUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String name= firstName.getText().toString().trim();
                String email = inputEmail.getText().toString().trim();
                String password = inputPassword.getText().toString().trim();


                if (TextUtils.isEmpty(name)) {
                    Toast.makeText(getApplicationContext(), "Name cannot be empty!", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (TextUtils.isEmpty(email)) {
                    Toast.makeText(getApplicationContext(), "Email cannot be empty!", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (TextUtils.isEmpty(password)) {
                    Toast.makeText(getApplicationContext(), "Password cannot be empty!", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (password.length() < 6) {
                    Toast.makeText(getApplicationContext(), "Password is too short, enter minimum of 6 characters!", Toast.LENGTH_SHORT).show();
                    return;
                }

                progressBar.setVisibility(View.VISIBLE);
                mAuth.createUserWithEmailAndPassword(email, password)
                        .addOnCompleteListener(RegisterActivity.this, new OnCompleteListener<AuthResult>() {
                            @Override
                            public void onComplete(@NonNull Task<AuthResult> task) {

                                FirebaseUser user = task.getResult().getUser();
                                progressBar.setVisibility(View.GONE);

                                if (task.isSuccessful()) {
                                     String fName = firstName.getText().toString().trim();
                                     String userEmail = inputEmail.getText().toString().trim();
                                     String uid = FirebaseAuth.getInstance().getUid();
                                     String tokenId = FirebaseInstanceId.getInstance().getToken();
                                     Boolean hasAcceptedToc = true;
                                     @SuppressLint("SimpleDateFormat") String accountCreationTimestamp =  new SimpleDateFormat("yyyy:MM:dd_HH:mm:ss").format(Calendar.getInstance().getTime());

                                    UserModel userModel = new UserModel(fName, userEmail, uid, tokenId,
                                            accountCreationTimestamp, hasAcceptedToc);

                                    mFirestore.collection("users").document(userEmail)
                                            .set(userModel).addOnSuccessListener(new OnSuccessListener<Void>() {
                                        @Override
                                        public void onSuccess(Void aVoid) {
                                             Toast.makeText(RegisterActivity.this, "Account created successfully!", Toast.LENGTH_SHORT).show();
                                        }
                                    });
                                    startActivity(new Intent(RegisterActivity.this, MainActivity.class));
                                    finish();

                                } else {
                                    Toast.makeText(RegisterActivity.this, "Failed to create account", Toast.LENGTH_SHORT).show();

                                }
                            }
                        });
            }


        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        menu.findItem(R.id.action_Close).setVisible(false); //hide cancel button
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.item_settings:
                startActivity(new Intent(getApplicationContext(), TabMenu.class));
                return true;
            case R.id.item_backup:
                startActivity(new Intent(getApplicationContext(), AccountPageActivity.class));
                return true;
            case R.id.item_rhPlus:
                startActivity(new Intent(getApplicationContext(), LoginActivity.class));
                return true;
            case R.id.item_usageguide:
                startActivity(new Intent(getApplicationContext(), ExpenseCreation.class));
                return true;
            case R.id.item_aboutUs:
                startActivity(new Intent(getApplicationContext(), AboutUs.class));
                return true;
            default:
                return super.onOptionsItemSelected(item);

        }
    }
        }

注册活动。xml

<?xml version="1.0" encoding="utf-8"?>
<安卓.support.design.widget.CoordinatorLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:app="http://schemas.安卓.com/apk/res-auto"
    xmlns:tools="http://schemas.安卓.com/tools"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    tools:context=".login.RegisterActivity">

       <安卓.support.design.widget.AppBarLayout
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:theme="@style/AppTheme.AppBarOverlay">

        <安卓.support.v7.widget.Toolbar
            安卓:id="@+id/toolbar"
            安卓:layout_width="match_parent"
            安卓:layout_height="?attr/actionBarSize"
            安卓:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </安卓.support.design.widget.AppBarLayout>

    <include layout="@layout/toolbar"/>

    <LinearLayout
        安卓:layout_width="fill_parent"
        安卓:layout_height="fill_parent"
        安卓:background="@color/colorPrimaryDark"
        安卓:gravity="center"
        安卓:orientation="vertical"
        安卓:padding="@dimen/activity_horizontal_margin">


        <TextView
            安卓:layout_width="wrap_content"
            安卓:layout_height="wrap_content"
            安卓:layout_gravity="center_horizontal"
            安卓:layout_marginBottom="40dp"
            安卓:text="@string/app_name"
            安卓:textSize="30sp"/>

        <安卓.support.design.widget.TextInputLayout
            安卓:layout_width="match_parent"
            安卓:layout_height="wrap_content">

            <EditText
                安卓:id="@+id/firstName"
                安卓:layout_width="match_parent"
                安卓:layout_height="wrap_content"
                安卓:hint="@string/first_Name"
                安卓:inputType="textPersonName"
                安卓:maxLines="1"
                安卓:singleLine="true"
                安卓:textColor="@安卓:color/black" />

        </安卓.support.design.widget.TextInputLayout>
        <安卓.support.design.widget.TextInputLayout
            安卓:layout_width="match_parent"
            安卓:layout_height="wrap_content">

            <EditText
                安卓:id="@+id/email"
                安卓:layout_width="match_parent"
                安卓:layout_height="wrap_content"
                安卓:hint="@string/email"
                安卓:inputType="textEmailAddress"
                安卓:maxLines="1"
                安卓:singleLine="true"
                安卓:textColor="@安卓:color/black" />

        </安卓.support.design.widget.TextInputLayout>

        <安卓.support.design.widget.TextInputLayout
            安卓:layout_width="match_parent"
            安卓:layout_height="wrap_content"
            app:passwordToggleEnabled="true">

            <EditText
                安卓:id="@+id/password"
                安卓:layout_width="match_parent"
                安卓:layout_height="wrap_content"
                安卓:focusableInTouchMode="true"
                安卓:hint="Create a password"

                安卓:imeOptions="actionUnspecified"
                安卓:inputType="textPassword"
                安卓:maxLines="1"
                安卓:singleLine="true"
                安卓:textColor="@安卓:color/white" />

        </安卓.support.design.widget.TextInputLayout>

        <TextView
            安卓:id="@+id/tv_toc"
            安卓:layout_width="match_parent"
            安卓:layout_height="wrap_content"
            安卓:text="@string/toc_remind" />

        <Button
            安卓:id="@+id/sign_up_button"
            安卓:layout_width="match_parent"
            安卓:layout_height="wrap_content"
            安卓:layout_marginTop="16dp"
            安卓:background="@color/colorAccent"
            安卓:text="@string/action_sign_in_short"
            安卓:textColor="@安卓:color/black"
            安卓:textAllCaps="false"/>

        <Button
            安卓:id="@+id/btn_reset_password"
            安卓:layout_width="fill_parent"
            安卓:layout_height="wrap_content"
            安卓:layout_marginTop="20dip"
            安卓:background="@color/colorAccent"
            安卓:text="@string/btn_forgot_password"
            安卓:textAllCaps="false"
            安卓:textColor="@安卓:color/black" />

        <!-- Link to Login Screen -->

        <Button
            安卓:id="@+id/sign_in_button"
            安卓:layout_width="fill_parent"
            安卓:layout_height="wrap_content"
            安卓:layout_marginTop="20dip"
            安卓:background="@color/colorAccent"
            安卓:elevation="0dp"
            安卓:text="@string/btn_link_to_login"
            安卓:textAllCaps="false"
            安卓:textColor="@安卓:color/black"
            安卓:textSize="15sp" />
    </LinearLayout>

    <ProgressBar
        安卓:id="@+id/progressBar"
        安卓:layout_width="30dp"
        安卓:layout_height="30dp"
        安卓:layout_gravity="center|bottom"
        安卓:layout_marginBottom="20dp"
        安卓:visibility="gone" />
</安卓.support.design.widget.CoordinatorLayout>

风格。xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="windowNoTitle">true</item>
    </style>

    <!-- Used for Calender theme -->
    <style name="DatePickerTheme" parent="安卓:Theme.Material.Light.Dialog">
        <item name="安卓:colorPrimary">@color/red</item>

        <!-- No need to override 'datePickerStyle' -->
        <!-- <item name="安卓:datePickerStyle">@style/MyDatePickerStyle</item> -->
    </style>


    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>

共 (0) 个答案