gpt4 book ai didi

java - IllegalStateException 和 NullPointer 异常

转载 作者:行者123 更新时间:2023-11-30 12:05:00 26 4
gpt4 key购买 nike

我正在尝试运行以下 Activity ,但是当我点击提交按钮时,我的应用程序崩溃并出现给定的异常。

我已经把声明和初始化分开了。

我的Complaint.java文件如下

package com.example.android.complaintbox_ritesltd;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;



public class Complaint extends AppCompatActivity {

private FirebaseDatabase mFirebaseDatabase;
private DatabaseReference mComplaintsDatabaseRegerence;
private EditText ct;

@Override
protected void onCreate(Bundle savedInstanceState) {

mFirebaseDatabase=FirebaseDatabase.getInstance();
mComplaintsDatabaseRegerence=mFirebaseDatabase.getReference().child("Complaints");
ct=findViewById(R.id.comp_box);



super.onCreate(savedInstanceState);
setContentView(R.layout.activity_complaint);

}
public void submitButton(View view) {
//TODO: Send Complaint to the database
ComplaintDetails complaintDetails=new ComplaintDetails(ct.getText().toString());
mComplaintsDatabaseRegerence.push().setValue(complaintDetails);


//Clears the input box
ct.setText("");
}

}

和activity_complaint.xml如下

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

xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Complaint"
android:orientation="vertical"
android:background="#E3F2FD"

>



<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/colorPrimary"
android:text="Write Your Complaint Here!"
android:textSize="30dp"
android:padding="20dp"
android:layout_marginTop="30dp"
android:textStyle="bold"

/>

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="30dp"
android:hint="Complaint!"
android:id="@+id/comp_box"
android:layout_marginTop="60dp"
android:inputType="text"
android:background="@drawable/white_round_corner_box"
android:layout_margin="14dp"
/>

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit"
android:layout_margin="14dp"
android:padding="14dp"
android:textAllCaps="true"
android:background="@drawable/rounded_corners"
android:textColor="@android:color/white"
android:layout_marginTop="90dp"
android:id="@+id/submit_button"
android:onClick="submitButton"
/>

</LinearLayout>

错误是:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.complaintbox_ritesltd, PID: 3227
java.lang.IllegalStateException: Could not execute method for android:onClick
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6597) 
at android.view.View.performClickInternal(View.java:6574) 
at android.view.View.access$3100(View.java:778) 
at android.view.View$PerformClick.run(View.java:25885) 
at android.os.Handler.handleCallback(Handler.java:873) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:6669) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.example.android.complaintbox_ritesltd.Complaint.submitButton(Complaint.java:42)
at java.lang.reflect.Method.invoke(Native Method) 
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) 
at android.view.View.performClick(View.java:6597) 
at android.view.View.performClickInternal(View.java:6574) 
at android.view.View.access$3100(View.java:778) 
at android.view.View$PerformClick.run(View.java:25885) 
at android.os.Handler.handleCallback(Handler.java:873) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:6669) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

最佳答案

public class Complaint extends AppCompatActivity {

private FirebaseDatabase mFirebaseDatabase;
private DatabaseReference mComplaintsDatabaseRegerence;
private EditText ct;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_complaint);
mFirebaseDatabase=FirebaseDatabase.getInstance();
mComplaintsDatabaseRegerence=mFirebaseDatabase.getReference().child("Complaints");
ct=findViewById(R.id.comp_box); // It needs to be below your setContentView.
}
}

原因:

因为 onCreate 需要知道您在类里面使用的布局是什么。

然后您可以分配您的 Edittext 否则它会导致崩溃并出现 nullpointer 异常。

关于java - IllegalStateException 和 NullPointer 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56706121/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com