我是Firebase和Android的新手。我想在Firebase中有两个表,
Users
Guides
以获取指南详情我正在使用两个类,一个用户,一个用于通过getter和setter获取和设置值的指南。
我已经为两个按钮创建了databaseReference的两个对象。 它与用户合作,但不与指南合作。当我点击指南按钮获取注册表格时,显示遗憾的是导游已停止。并返回第一页。
我认为这是因为API,首先我将目标Android API级别设置为27,然后我在Android API级别22上运行,因此使其达到API级别22并再次弹出错误。
有人可以帮助我吗?
这是我的代码,
Guides.java
public class Guides {
String guideName;
String guidePhoneNo;
String guideLanguages;
String guideMail;
String guidePwd;
public Guides(){
}
public Guides(String guideName,String guidePhoneNo,String guideLanguages,String guideMail, String guidePwd){
guideName=guideName;
guidePhoneNo=guidePhoneNo;
guideLanguages=guideLanguages;
guideMail=guideMail;
guidePwd=guidePwd;
}
public String getGuideName() {
return guideName;
}
public void setGuideName(String guideName) {
this.guideName = guideName;
}
public String getGuidePhoneNo() {
return guidePhoneNo;
}
public void setGuidePhoneNo(String guidePhoneNo) {
this.guidePhoneNo = guidePhoneNo;
}
public String getGuideLanguages() {
return guideLanguages;
}
public void setGuideLanguages(String guideLanguages) {
this.guideLanguages = guideLanguages;
}
public String getGuideMail() {
return guideMail;
}
public void setGuideMail(String guideMail) {
this.guideMail = guideMail;
}
public String getGuidePwd() {
return guidePwd;
}
public void setGuidePwd(String guidePwd) {
this.guidePwd = guidePwd;
}}
GuideRegActivity.java
public class GuideRegActivity extends AppCompatActivity {
EditText guideName,guidePhoneNo,guideLanguages,guideMail,guidePwd;
Button btnGuideReg;
FirebaseDatabase FirebaseDatabase;
DatabaseReference databseReference1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_guide_reg);
guideName=(EditText)findViewById(R.id.guideName);
guidePhoneNo=(EditText)findViewById(R.id.guidePhoneNo);
guideLanguages=(EditText)findViewById(R.id.guideLanguages);
guideMail=(EditText)findViewById(R.id.guideMail);
guidePwd=(EditText)findViewById(R.id.guidePwd);
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
databseReference1 = FirebaseDatabase.getInstance().getReference("Guides");
btnGuideReg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addArrayList();
}
});
}
private void addArrayList() {
String gName= guideName.getText().toString().trim();
String gPNo=guidePhoneNo.getText().toString().trim();
String gLang=guideLanguages.getText().toString().trim();
String gMail=guideMail.getText().toString().trim();
String gPwd=guidePwd.getText().toString().trim();
if(TextUtils.isEmpty(gName)){
Toast.makeText(this,"Please Enter Guide Name",Toast.LENGTH_LONG).show();
}else if(TextUtils.isEmpty(gPNo)){
Toast.makeText(this,"Please Enter Contact No",Toast.LENGTH_LONG).show();
}else if(TextUtils.isEmpty(gLang)){
Toast.makeText(this,"Please Enter Languages You can",Toast.LENGTH_LONG).show();
}else if(TextUtils.isEmpty(gMail)){
Toast.makeText(this,"Please Enter Your E Mail",Toast.LENGTH_LONG).show();
}else if(TextUtils.isEmpty(gPwd)){
Toast.makeText(this,"Please Enter Password",Toast.LENGTH_LONG).show();
}else{
String id =databseReference1.push().getKey();
Guides guides = new Guides(gName,gPNo,gLang,gMail,gPwd);
databseReference1.child(id).child("Guide Name").setValue(gName.toString());
databseReference1.child(id).child("Contact No").setValue(gPNo.toString());
databseReference1.child(id).child("Vehicle Type").setValue(gName.toString());
databseReference1.child(id).child("Mail").setValue(gPNo.toString());
databseReference1.child(id).child("Password").setValue(gPwd.toString());
Toast.makeText(this,"Guide Added",Toast.LENGTH_LONG).show();
ClearText();
}
}
private void ClearText() {
guideName.setText("");
guideLanguages.setText("");
guidePwd.setText("");
guideMail.setText("");
guidePhoneNo.setText("");
}}
Users.java
public class Users {
String userName;
String userMail;
String userPwd;
public Users(){
}
public Users(String userName , String userMail,String userPwd){
userName=userName;
userMail=userMail;
userPwd=userPwd;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserMail() {
return userMail;
}
public void setUserMail(String userMail) {
this.userMail = userMail;
}
public String getUserPwd() {
return userPwd;
}
public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}}
UserRegActivity.java
public class UserRegActivity extends AppCompatActivity {
EditText userName,userMail,userPwd;
Button btnRegUser;
FirebaseDatabase FirebaseDatabase;
DatabaseReference databseReference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_reg);
userName=(EditText)findViewById(R.id.userName);
userMail=(EditText)findViewById(R.id.userMail);
userPwd=(EditText)findViewById(R.id.userPwd);
btnRegUser=(Button)findViewById(R.id.btnUserReg);
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
databseReference = FirebaseDatabase.getInstance().getReference("Users");
btnRegUser.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addArrayList();
}
});
}
private void addArrayList() {
String uName= userName.getText().toString().trim();
String uMail=userMail.getText().toString().trim();
String uPwd=userPwd.getText().toString().trim();
if(TextUtils.isEmpty(uName)){
Toast.makeText(this,"Please Enter User Name",Toast.LENGTH_LONG).show();
}else if(TextUtils.isEmpty(uMail)){
Toast.makeText(this,"Please Enter Your Mail",Toast.LENGTH_LONG).show();
}else if(TextUtils.isEmpty(uPwd)){
Toast.makeText(this,"Please Enter Password",Toast.LENGTH_LONG).show();
}else{
String id =databseReference.push().getKey();
Users users = new Users(uName,uMail,uPwd);
databseReference.child(id).child("UserName").setValue(uName.toString());
databseReference.child(id).child("Email").setValue(uMail.toString());
databseReference.child(id).child("Password").setValue(uPwd.toString());
Toast.makeText(this,"User Added",Toast.LENGTH_LONG).show();
ClearText();
}
}
private void ClearText(){
userName.setText("");
userMail.setText("");
userPwd.setText("");
}}
上传: 我目前的数据库结构, here Only Users will get
这是我的错误日志,
05-17 19:39:56.489 20503-20503/com.example.hansi.tourguide E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.hansi.tourguide, PID: 20503
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hansi.tourguide/com.example.hansi.tourguide.GuideRegActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2695)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2769)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1430)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5910)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.hansi.tourguide.GuideRegActivity.onCreate(GuideRegActivity.java:36)
at android.app.Activity.performCreate(Activity.java:6178)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2648)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2769)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1430)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5910)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
答案 0 :(得分:0)
我不是当然但是我在android中经常使用firebase大约一个月,我认为你应该使用单个Databae Reference然后你可以对不同的表使用两个查询
答案 1 :(得分:0)
Firebase数据库不是关系数据库,因此表,列和行的概念不适用于此处。这是一个以Json格式发送数据的NoSQL数据库。