FindViewByid为EditText返回null,应用程序崩溃

问题描述 投票:0回答:3

我尝试运行代码时,我的应用崩溃。我正在尝试通过使用用户名来创建注册。如果我尝试不带

name=(EditText)findViewById(R.id.textInputLayout2);

然后它以正确的布局显示页面在运行代码时,在上面的代码存在的情况下,应用程序将关闭,没有任何内容。Java代码如下

package com.example.thefamily;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
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.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;

import java.security.PrivateKey;

public class Sign_up extends AppCompatActivity {

private EditText name, username, userpass, userpass2, useremail, mobileno;
private Button regButton;
//private TextView userLogin;
private FirebaseAuth firebaseAuth;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sign_up);
    getSupportActionBar().setTitle("Join The Family");
    setupUIviews();}


private void setupUIviews(){

    name=(EditText)findViewById(R.id.textInputLayout2);

}




}

布局代码如下

<?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=".Sign_up">

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fontFamily="@font/crafty_girls"
    android:text="Join The Family"
    android:textSize="30sp"
    app:layout_constraintBottom_toTopOf="@+id/textInputLayout2"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:textColor="#808080" />


<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/textInputLayout2"
    android:layout_width="350dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="100dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">



    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:hint="Full Name" />
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/textInputLayout3"
    android:layout_width="350dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="14dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textInputLayout2">

    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:hint="Username" />
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/textInputLayout4"
    android:layout_width="350dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="14dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textInputLayout3">

    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:hint="Email ID" />
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/textInputLayout5"
    android:layout_width="350dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="14dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textInputLayout4">

    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:hint="Mobile No" />
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/textInputLayout6"
    android:layout_width="350dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="14dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textInputLayout5">

    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:hint="Password" />
</com.google.android.material.textfield.TextInputLayout>


<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/textInputLayout7"
    android:layout_width="350dp"
    android:layout_marginTop="14dp"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textInputLayout6">

    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:hint="Confirm Password" />
</com.google.android.material.textfield.TextInputLayout>

<RadioGroup
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintTop_toBottomOf="@+id/textInputLayout7">

    <androidx.appcompat.widget.AppCompatTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Gender : "
        android:textSize="20dp"
        android:layout_marginLeft="32dp"
        android:layout_marginTop="14dp"
        />

    <androidx.appcompat.widget.AppCompatRadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Male"
        android:layout_marginLeft="32dp"
        android:layout_marginTop="14dp"
        />
    <androidx.appcompat.widget.AppCompatRadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Female"
        android:layout_marginLeft="32dp"
        android:layout_marginTop="14dp"
    />


   </RadioGroup>


<Button
    android:id="@+id/b3"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="90dp"
    android:text="Register"
    android:textSize="30dp"
    android:fontFamily="@font/crafty_girls"
    android:background="@drawable/button_round"

    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textInputLayout7" />


</androidx.constraintlayout.widget.ConstraintLayout>

请帮助其崩溃的原因。

android firebase android-edittext findviewbyid r.id
3个回答
0
投票

将您的(EditText)更改为(TextInputLayout)

public class Sign_up extends AppCompatActivity {

private TextInputLayout name;
private Button regButton;
//private TextView userLogin;
private FirebaseAuth firebaseAuth;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sign_up);
    getSupportActionBar().setTitle("Join The Family");
    setupUIviews();}


private void setupUIviews(){

    name = (TextInputLayout) findViewById(R.id.textInputLayout2);

}

}

0
投票

我认为您的应用崩溃不是您提出的问题“名称=(EditText)findViewById(R.id.textInputLayout2);”

您可以在Android Studio中使用logcat并将其设置为Error,以查看代码中发生了什么。

enter image description here


0
投票

[TextInputLayout不是EditText,可以通过textInputLayout.getEditText获得EditText。


0
投票

您将从EditText中获得输入文本,而不是从TextInputLayout中获得输入文本。给EditText而不是TextInputLayout指定ID。

<com.google.android.material.textfield.TextInputLayout
    android:layout_width="350dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="100dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">



    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textInputLayout2"        //or name it whatever you want
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:hint="Full Name" />
</com.google.android.material.textfield.TextInputLayout>
© www.soinside.com 2019 - 2024. All rights reserved.