Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么我得到这个空引用对象?_Java_Android_Nullpointerexception - Fatal编程技术网

Java 为什么我得到这个空引用对象?

Java 为什么我得到这个空引用对象?,java,android,nullpointerexception,Java,Android,Nullpointerexception,我尝试在我的应用程序上显示客户配置文件,但当我单击配置文件按钮配置文件页面时,这些代码行会使应用程序崩溃 该错误表示,试图对空对象引用调用虚拟方法“void android.widget.EditText.setTextjava.lang.CharSequence” 可以显示我的姓名和地址,但不能显示我的电子邮件和电话号码,这很奇怪为什么会这样 我检查我的数据库,它包含数据,我在这里被困在如何解决我的问题,因为代码不能读取我的电子邮件和电话号码,但可以准备我的姓名和地址 public class

我尝试在我的应用程序上显示客户配置文件,但当我单击配置文件按钮配置文件页面时,这些代码行会使应用程序崩溃

该错误表示,试图对空对象引用调用虚拟方法“void android.widget.EditText.setTextjava.lang.CharSequence”

可以显示我的姓名和地址,但不能显示我的电子邮件和电话号码,这很奇怪为什么会这样

我检查我的数据库,它包含数据,我在这里被困在如何解决我的问题,因为代码不能读取我的电子邮件和电话号码,但可以准备我的姓名和地址

public class CustProfileActivity extends AppCompatActivity {

    TextView email;
    EditText name, address, phone;
    Button update;

    DatabaseReference databaseReference;
    FirebaseAuth firebaseAuth;
    String userId;

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

        name = findViewById(R.id.name);
        address = findViewById(R.id.address);
        update = findViewById(R.id.update);


        databaseReference = FirebaseDatabase.getInstance().getReference("Customer").child("");
        firebaseAuth = FirebaseAuth.getInstance();
        userId = firebaseAuth.getUid();

        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                Customer cust = dataSnapshot.child(userId).getValue(Customer.class);

                name.setText(cust.name);
                address.setText(cust.home_address);
                phone.setText(cust.telephone_number);
                email.setText(cust.email);

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });


似乎您没有从布局中初始化EditText。初始化后,尝试对其执行操作

您在手机上设置文字的操作在该视图初始化之前完成。这就是它抛出错误的原因

在空对象引用上作废android.widget.EditText.setTextjava.lang.CharSequence


您必须初始化这些视图电子邮件、电话


因此,在onCreate通过find view by id初始化此视图时,您忘记了初始化这些视图。like name=findviewbydr.id.name;
 <TextView
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Phone No"
        android:textSize="13sp"/>

    <EditText
        android:id="@+id/phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


    <TextView
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Email"
        android:textSize="13dp"/>

    <TextView
        android:id="@+id/email"
        android:hint="email@gmail.com"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


public class Customer {
    public String id;
    public  String name;
    public  String email;
    public String home_address;
    public String telephone_number;


    public Customer(){

    }



    public Customer(String id, String name, String email, String home_address, String telephone_number) {
        this.id = id;
        this.name = name;
        this.email = email;
        this.home_address = home_address;
        this.telephone_number = telephone_number;
    }
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getHome_address() {
        return home_address;
    }

    public void setHome_address(String home_address) {
        this.home_address = home_address;
    }

    public String getTelephone_number() {
        return telephone_number;
    }

    public void setTelephone_number(String telephone_number) {
        this.telephone_number = telephone_number;
    }



public class CustProfileActivity extends AppCompatActivity {
 ...// ... means rest of your code

@Override
protected void onCreate(Bundle savedInstanceState) {
 ...
  // missed to initialize phone & email
  phone = findViewById(R.id.phone);
  email = findViewById(R.id.email);
 ...
 }
}