我按下按钮以打开注册时出错,我得到了这个错误,我已经尝试了很多,但无法解决。我检查了按钮的ID,并且我也更改了它的名称,但也没有得到解决。我也将应用程序紧凑按钮更改为Button,但仍然存在错误。我还附加了Java文件和XML,请查看此
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.orderfoodapp, PID: 12570
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.orderfoodapp/com.example.orderfoodapp.Signup_ac}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.AppCompatButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.AppCompatButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.orderfoodapp.Signup_ac.onCreate(Signup_ac.java:39)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
活动的名称是Signup_ac.java。它的代码在下面
package com.example.orderfoodapp;
import android.app.ProgressDialog;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.AppCompatEditText;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.security.Permission;
public class Signup_ac extends AppCompatActivity {
AppCompatEditText editText,editText1,editText2;
AppCompatButton button12;
@Override
protected void onCreate(Bundle savedInstanceState) {
editText=(AppCompatEditText) findViewById(R.id.name_TextField);
editText1=(AppCompatEditText) findViewById(R.id.phone_TextField);
editText2=(AppCompatEditText) findViewById(R.id.password_TextField);
button12=(AppCompatButton) findViewById(R.id.sign_upbtnn);
final FirebaseDatabase database=FirebaseDatabase.getInstance();
final DatabaseReference databaseReference=database.getReference("user");
button12.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final ProgressDialog progressDialog=new ProgressDialog(Signup_ac.this);
progressDialog.setMessage("Please wait....");
progressDialog.show();
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.child(editText1.getText().toString()).exists()){
progressDialog.dismiss();
Toast.makeText(getApplicationContext(),"Phone number already registered.",Toast.LENGTH_LONG);
}
else {
progressDialog.dismiss();
User user=new User(editText.getText().toString(),editText2.getText().toString());
databaseReference.child(editText1.getText().toString()).setValue(user);
Toast.makeText(getApplicationContext(),"Sign up successful",Toast.LENGTH_LONG);
finish();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
});
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup_ac);
}
}
XML文件名activity_signup_ac.xml。其代码如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".Signup_ac"
android:background="@drawable/mi_bg"
android:focusable="true"
android:focusableInTouchMode="true"
>
<LinearLayout
android:id="@+id/firstL_layout"
android:layout_width="match_parent"
android:layout_height="300dp"
android:orientation="vertical"
android:layout_centerVertical="true"
>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/name_TextInputLayout"
android:orientation="vertical"
android:hint="Name"
android:layout_marginTop="80dp"
>
<android.support.v7.widget.AppCompatEditText
android:id="@+id/name_TextField"
android:layout_width="match_parent"
android:layout_height="50dp"
android:inputType="text"
android:text="Tom Cruise"
android:textColor="@android:color/white" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/phone_TextInputLayout"
android:orientation="vertical"
android:hint="Phone"
>
<android.support.v7.widget.AppCompatEditText
android:id="@+id/phone_TextField"
android:layout_width="match_parent"
android:layout_height="50dp"
android:inputType="text"
android:text="3438204823048"
android:textColor="@android:color/white" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/passw_TextInputLayout"
android:orientation="vertical"
android:hint="Password"
>
<android.support.v7.widget.AppCompatEditText
android:id="@+id/passw_TextField"
android:layout_width="match_parent"
android:layout_height="50dp"
android:inputType="text"
android:text="12345"
android:textColor="@android:color/white" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/firstL_layout"
>
<android.support.v7.widget.AppCompatButton
android:id="@+id/sign_upbtnn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_margin="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:background="@color/colorSignUp"
android:text="Create Your Account"
android:textColor="@android:color/white" />
</LinearLayout>
</RelativeLayout>
答案 0 :(得分:0)
您需要在findByViewID()
方法的前面加上两行:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup_ac);
希望有帮助!