我正在我的Android应用程序中创建一个登录/注册活动。当用户注册时,Firebase身份验证工作正常。注册后,使用将直接进入设置活动。现在,设置活动是将数据存储到Firebase数据库中。每当用户完成设置并单击按钮以更新数据库时。该应用程序停止运行。
private Button SaveInformation;
SaveInformation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
SaveAccountSetupInformation();
}
});
When the app is crashing the logcat is showing this error
我的设置活动代码为:
package com.example.application;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import java.util.HashMap;
public class SetupActivity2 extends AppCompatActivity {
private EditText FirstName, LastName, Day, Month, Year;
private EditText Country, State;
private ImageView ProfilePic;
private Button SaveInformation;
private FirebaseAuth mAuth;
private DatabaseReference UsersRef;
private ProgressDialog loadingBar;
String currentUserID;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setup2);
mAuth = FirebaseAuth.getInstance();
currentUserID = mAuth.getCurrentUser().getUid();
UsersRef = FirebaseDatabase.getInstance().getReference().child("Users").child(currentUserID);
FirstName = (EditText) findViewById(R.id.setup_FirstName);
LastName = (EditText) findViewById(R.id.setup_LastName);
Day = (EditText) findViewById(R.id.setup_date);
Month = (EditText) findViewById(R.id.setup_month);
Year = (EditText) findViewById(R.id.setup_year);
Country = (EditText) findViewById(R.id.setup_country);
State = (EditText) findViewById(R.id.setup_state);
ProfilePic = (ImageView) findViewById(R.id.setup_profilePic) ;
SaveInformation = (Button) findViewById(R.id.setup_button);
loadingBar = new ProgressDialog(this);
SaveInformation.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
SaveAccountSetupInformation();
}
});
}
private void SaveAccountSetupInformation()
{
String firstName = FirstName.getText().toString().trim();
String lastName = LastName.getText().toString().trim();
String day = Day.getText().toString().trim();
String month = Month.getText().toString().trim();
String year = Year.getText().toString().trim();
String country = Country.getText().toString().trim();
String state = State.getText().toString().trim();
if(TextUtils.isEmpty(firstName))
{
Toast.makeText(this, "Please write your username...", Toast.LENGTH_SHORT).show();
}
if(TextUtils.isEmpty(lastName))
{
Toast.makeText(this, "Please write your full name...", Toast.LENGTH_SHORT).show();
}
if(TextUtils.isEmpty(country))
{
Toast.makeText(this, "Please write your country...", Toast.LENGTH_SHORT).show();
}
if(TextUtils.isEmpty(state))
{
Toast.makeText(this, "Please write your state...", Toast.LENGTH_SHORT).show();
}
if(TextUtils.isEmpty(day))
{
Toast.makeText(this, "Please write your full name...", Toast.LENGTH_SHORT).show();
}
if(TextUtils.isEmpty(month))
{
Toast.makeText(this, "Please write your month...", Toast.LENGTH_SHORT).show();
}
if(TextUtils.isEmpty(year))
{
Toast.makeText(this, "Please write your year...", Toast.LENGTH_SHORT).show();
}
else
{
loadingBar.setTitle("Saving Information");
loadingBar.setMessage("Please wait, while we are creating your new Account...");
loadingBar.show();
loadingBar.setCanceledOnTouchOutside(true);
HashMap userMap = new HashMap<>();
userMap.put("FirstName",firstName);
userMap.put("LastName",lastName);
userMap.put("day",Day);
userMap.put("month",Month);
userMap.put("year",Year);
userMap.put("country",country);
userMap.put("state",State);
userMap.put("Status","Hey There !! I am using Toodle.");
userMap.put("Gender","Default");
userMap.put("Institution","Default");
UsersRef.updateChildren(userMap).addOnCompleteListener(new OnCompleteListener()
{
@Override
public void onComplete(@NonNull Task task)
{
if(task.isSuccessful())
{
SendUserToMainActivity();
Toast.makeText(SetupActivity2.this, "your Account is created Successfully.", Toast.LENGTH_LONG).show();
loadingBar.dismiss();
}
else
{
String message = task.getException().getMessage();
Toast.makeText(SetupActivity2.this, "Error Occured: " + message, Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
}
}
});
}
}
private void SendUserToMainActivity()
{
Intent mainIntent = new Intent(SetupActivity2.this, MainActivity.class);
mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(mainIntent);
finish();
}
}
应用崩溃时,我的LOGCAT错误。
2020-09-13 11:05:00.540 20182-20182/com.example.application E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.application, PID: 20182
com.google.firebase.database.DatabaseException: Found conflicting getters for name: getText
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.<init>(CustomClassMapper.java:477)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.loadOrCreateBeanMapperForClass(CustomClassMapper.java:329)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.serialize(CustomClassMapper.java:166)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.serialize(CustomClassMapper.java:141)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToPlainJavaTypes(CustomClassMapper.java:65)
at com.google.firebase.database.DatabaseReference.updateChildrenInternal(DatabaseReference.java:412)
at com.google.firebase.database.DatabaseReference.updateChildren(DatabaseReference.java:392)
at com.example.application.SetupActivity2.SaveAccountSetupInformation(SetupActivity2.java:141)
at com.example.application.SetupActivity2.access$000(SetupActivity2.java:26)
at com.example.application.SetupActivity2$1.onClick(SetupActivity2.java:72)
at android.view.View.performClick(View.java:6312)
at android.view.View$PerformClick.run(View.java:24811)
at android.os.Handler.handleCallback(Handler.java:794)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:6651)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)
答案 0 :(得分:0)
您在logcat中显示的错误是
Found conflicting getters for name: getText
Firebase数据库只能存储JSON类型,但是您向其发送了一个“编辑文本” 错误恰好在这里
userMap.put("day",Day);
userMap.put("month",Month);
userMap.put("year",Year);
userMap.put("state",State);
您将“天,月,年和状态”发送为“编辑文本”而不是字符串 像这样
userMap.put("day",day);
userMap.put("month",month);
userMap.put("year",year);
userMap.put("state",state);
它将正常工作