我的活动是空白的,即使它不应该是空白的?

5sxhfpxr  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(356)

我是一个完全的编码初学者,决定在没有正式培训或基础课程的情况下尝试android Studio,以获得乐趣。因此,如果这似乎是一个愚蠢的问题,我很抱歉,但每当我尝试运行代码时,它都是空白的?你知道怎么解决这个问题吗?
下面是一个屏幕截图

package com.example.trtalpha17;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;

import org.jetbrains.annotations.NotNull;

public class Register_Tutor extends AppCompatActivity {

    private static final String[] SCHOOLS = new String[]{

            "A", "B", "C"
    };

    private static final String[] COURSE = new String[]{
            "Bachelor of Science in Social Work (BS Social Work)",
            "Bachelor of Science in Marine Transportation (BSMT)",
            "Bachelor of Science in Food Technology (BS Food Tech)",
            "Bachelor of Science in Nutrition and Dietetics (BS Nutrition and Dietetics)"
    };

    private static final String[] EDUCA = new String[]{
            "Grade 9", "Grade 10", "Grade 11", "Grade 12", "1st Year College", "2nd Year College", "3rd Year College", "4th Year College", "5th Year College", "Graduate"
    };

    private static final String[] TITLES = new String[]{
            "Mr.", "Ms.", "Mrs.", "Mx."
    };

    EditText metEmail, metPhone, metPassword, metPasswordconfirm, metNamefirst, metNamelast;
    Button mbutton;
    ProgressBar progressBar2;
    FirebaseAuth fAuth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_tutor2);

        final AutoCompleteTextView metTitle = (AutoCompleteTextView)findViewById(R.id.etTitle);
        final AutoCompleteTextView mautoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
        final AutoCompleteTextView meduc = (AutoCompleteTextView) findViewById(R.id.educ);
        final AutoCompleteTextView metcourse = (AutoCompleteTextView) findViewById(R.id.etcourse);

        metTitle.setThreshold(2);
        mautoCompleteTextView.setThreshold(1);
        meduc.setThreshold(1);
        metcourse.setThreshold(1);

        ImageView marrow = (ImageView) findViewById(R.id.arrow);

        ArrayAdapter<String> adapterarrow = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, TITLES);
        metTitle.setAdapter(adapterarrow);
        ArrayAdapter<String> adapterschools = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, SCHOOLS);
        mautoCompleteTextView.setAdapter(adapterschools);
        ArrayAdapter<String> adapteredu = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, EDUCA);
        meduc.setAdapter(adapteredu);
        ArrayAdapter<String> adaptercourse = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, COURSE);
        metcourse.setAdapter(adaptercourse);

        marrow.setOnClickListener(new  View.OnClickListener() {
            @Override
            public void onClick(View view) {
                metTitle.showDropDown();
            }
        });

        metNamefirst = findViewById(R.id.etNamefirst);
        metNamelast = findViewById(R.id.etNamelast);
        metEmail = findViewById(R.id.etEmail);
        metPhone =findViewById(R.id.etPhone);
        metPassword = findViewById(R.id.etPassword);
        metPasswordconfirm =findViewById(R.id.etPasswordconfirm);
        mbutton = findViewById(R.id.button);
        progressBar2 = findViewById(R.id.progressBar2);
        fAuth = FirebaseAuth.getInstance();

        if(fAuth.getCurrentUser() != null){
            startActivity(new Intent(getApplicationContext(),MainActivity.class));
            finish();
        }

        mbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String email = metEmail.getText().toString().trim();
                String password = metPassword.getText().toString().trim();

                if(TextUtils.isEmpty(email)){
                    metEmail.setError("Email is required.");
                    return;
                }

                if(TextUtils.isEmpty(password)){
                    metPassword.setError("Password is required.");
                    return;
                }

                if(password.length()<6){
                    metPassword.setError("Password length must be at least 6 characters.");
                    return;
                }

                fAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull @NotNull Task<AuthResult> task) {
                        if(task.isSuccessful()){
                            Toast.makeText(Register_Tutor.this, "Registration complete.", Toast.LENGTH_SHORT).show();
                            startActivity(new Intent(getApplicationContext(),MainActivity.class));

                        }else {
                            Toast.makeText(Register_Tutor.this, "There was an error making your that request. Please try again later." + task.getException().getMessage(), Toast.LENGTH_SHORT).show();

                        }
                    }
                });
            }

        });
    }
}

布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".Register_Tutor">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:fontFamily="cursive"
        android:text="Register as a Tutor"
        android:textColor="#000000"
        android:textSize="28sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TableRow
        android:id="@+id/tableRow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView2">

        <AutoCompleteTextView
            android:id="@+id/etTitle"
            android:layout_width="68dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:drawableLeft="@drawable/ic_baseline_person_24"
            android:drawablePadding="5dp"
            android:hint="Title"
            android:inputType="textPersonName" />

        <ImageView
            android:id="@+id/arrow"
            android:layout_width="16dp"
            android:layout_height="45dp"
            android:layout_marginStart="4dp"
            android:layout_marginLeft="4dp"
            android:drawablePadding="5dp"
            android:src="@mipmap/outline_arrow_drop_down_black_18" />

        <EditText
            android:id="@+id/etNamefirst"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:hint="First name"
            android:inputType="textPersonName" />

        <EditText
            android:id="@+id/etNamelast"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:hint="Last name"
            android:inputType="textPersonName" />
    </TableRow>

    <LinearLayout
        android:id="@+id/linear1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintTop_toBottomOf="@+id/tableRow">

        <EditText
            android:id="@+id/etEmail"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:drawableLeft="@drawable/ic_baseline_email_24"
            android:drawablePadding="5dp"
            android:hint="Email address"
            android:inputType="textEmailAddress" />

        <EditText
            android:id="@+id/etPhone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:drawableLeft="@drawable/ic_baseline_local_phone_24"
            android:drawablePadding="5dp"
            android:hint="Phone number"
            android:inputType="phone" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linear2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linear1">

        <AutoCompleteTextView
            android:id="@+id/autoCompleteTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:completionThreshold="1"
            android:drawableLeft="@drawable/ic_baseline_school_24"
            android:drawablePadding="5dp"
            android:hint="School" />

        <AutoCompleteTextView
            android:id="@+id/educ"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:completionThreshold="1"
            android:drawableLeft="@drawable/ic_baseline_article_24"
            android:drawablePadding="5dp"
            android:hint="Educational Attainment" />

        <AutoCompleteTextView
            android:id="@+id/etcourse"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:completionThreshold="1"
            android:drawableLeft="@drawable/ic_baseline_article_24"
            android:drawablePadding="5dp"
            android:hint="Course" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linear3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintTop_toBottomOf="@+id/linear2">

        <EditText
            android:id="@+id/etPassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:drawableLeft="@drawable/ic_baseline_lock_24"
            android:drawablePadding="5dp"
            android:hint="Password"
            android:inputType="textPassword" />

        <EditText
            android:id="@+id/etPasswordconfirm"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:drawableLeft="@drawable/ic_baseline_lock_open_24"
            android:drawablePadding="5dp"
            android:hint="Confirm Password"
            android:inputType="textPassword" />

        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="24dp"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginBottom="10dp"
            android:background="@drawable/button"
            android:gravity="center"
            android:hint="SIGN UP"
            android:textColorHint="#FDFCFC"
            app:backgroundTint="#315E8C" />

        <ProgressBar
            android:id="@+id/progressBar2"
            style="?android:attr/progressBarStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="invisible" />

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

安卓清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.trtalpha17">

    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.TRTAlpha17"
        >

        <activity android:name=".Register_Tutor">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>
        </activity>

        <activity android:name=".Login" />
        <activity android:name=".RegisterStudent" />
        <activity android:name=".RegistrationHome" />

        <activity android:name=".MainActivity" />

    </application>

</manifest>
xtfmy6hx

xtfmy6hx1#

是否从ide清理并重建项目或无效缓存/重新启动?您将在“文件”菜单下找到它。
看起来你的代码很好,所以我决定在ide中运行它,编译和运行都很完美。

火基控制台-

所以你可以在你这边做一些检查-1。

setContentView(R.layout.activity_register_tutor2);

layout.activity\u register\u tutor2是您的主/默认布局吗?或任何其他布局`?
2.

if(fAuth.getCurrentUser() != null){
            startActivity(new Intent(getApplicationContext(),MainActivity.class));
            finish();
        }

如果您已至少登录一次,此代码将带您进入mainactivity。那是空白活动吗?确保您没有共享mainactivity的布局文件,因此我不确定。
您可以通过以下方式更轻松地实现注销以测试/调试应用程序-

private Button mLogOutBtn;
    mLogOutBtn = findViewById(R.id.log_out_btn);
    mAuth = FirebaseAuth.getInstance();

    mLogOutBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mAuth.signOut();
            startActivity(new Intent(MainActivity.this, Register_Tutor.class));
            finish();
        }
    });

相关问题