Whenever I am clicking save button in this SetupActivity my app crashes. My app is crashing whenever I am going to WRITE in Firebase Database

huangapple go评论92阅读模式
英文:

Whenever I am clicking save button in this SetupActivity my app crashes. My app is crashing whenever I am going to WRITE in Firebase Database

问题

private Button SaveInformation;
SaveInformation.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        SaveAccountSetupInformation();
    }
});
@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);
    // ... (other EditText fields)

    SaveInformation = (Button) findViewById(R.id.setup_button);

    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();
    // ... (get other EditText values)

    if (TextUtils.isEmpty(firstName)) {
        Toast.makeText(this, "Please write your username...", Toast.LENGTH_SHORT).show();
    }
    // ... (other TextUtils checks)
    else {
        loadingBar.setTitle("Saving Information");
        loadingBar.setMessage("Please wait, while we are creating your new Account...");
        loadingBar.show();
        loadingBar.setCanceledOnTouchOutside(true);

        HashMap<String, Object> userMap = new HashMap<>();
        userMap.put("FirstName", firstName);
        userMap.put("LastName", lastName);
        // ... (put other data into the map)

        UsersRef.updateChildren(userMap).addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> 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();
}
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
// ... (rest of the error log)
英文:

I Am creating a Login/Register Activity in my android app. When a user is registering the Firebase Authentication is working fine. After Registering a use is directed into Setup activity. Now the setup activity is to store data into Firebase Database. Whenever the user is completing setup and clicking on the button to update in the database. The app stops working.

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

enter image description here

The App is showing this error

The Code of my Setup Activity is:

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(&quot;Users&quot;).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, &quot;Please write your username...&quot;, Toast.LENGTH_SHORT).show();
}
if(TextUtils.isEmpty(lastName))
{
Toast.makeText(this, &quot;Please write your full name...&quot;, Toast.LENGTH_SHORT).show();
}
if(TextUtils.isEmpty(country))
{
Toast.makeText(this, &quot;Please write your country...&quot;, Toast.LENGTH_SHORT).show();
}
if(TextUtils.isEmpty(state))
{
Toast.makeText(this, &quot;Please write your state...&quot;, Toast.LENGTH_SHORT).show();
}
if(TextUtils.isEmpty(day))
{
Toast.makeText(this, &quot;Please write your full name...&quot;, Toast.LENGTH_SHORT).show();
}
if(TextUtils.isEmpty(month))
{
Toast.makeText(this, &quot;Please write your month...&quot;, Toast.LENGTH_SHORT).show();
}
if(TextUtils.isEmpty(year))
{
Toast.makeText(this, &quot;Please write your year...&quot;, Toast.LENGTH_SHORT).show();
}
else
{
loadingBar.setTitle(&quot;Saving Information&quot;);
loadingBar.setMessage(&quot;Please wait, while we are creating your new Account...&quot;);
loadingBar.show();
loadingBar.setCanceledOnTouchOutside(true);
HashMap userMap = new HashMap&lt;&gt;();
userMap.put(&quot;FirstName&quot;,firstName);
userMap.put(&quot;LastName&quot;,lastName);
userMap.put(&quot;day&quot;,Day);
userMap.put(&quot;month&quot;,Month);
userMap.put(&quot;year&quot;,Year);
userMap.put(&quot;country&quot;,country);
userMap.put(&quot;state&quot;,State);
userMap.put(&quot;Status&quot;,&quot;Hey There !! I am using Toodle.&quot;);
userMap.put(&quot;Gender&quot;,&quot;Default&quot;);
userMap.put(&quot;Institution&quot;,&quot;Default&quot;);
UsersRef.updateChildren(userMap).addOnCompleteListener(new OnCompleteListener()
{
@Override
public void onComplete(@NonNull Task task)
{
if(task.isSuccessful())
{
SendUserToMainActivity();
Toast.makeText(SetupActivity2.this, &quot;your Account is created Successfully.&quot;, Toast.LENGTH_LONG).show();
loadingBar.dismiss();
}
else
{
String message =  task.getException().getMessage();
Toast.makeText(SetupActivity2.this, &quot;Error Occured: &quot; + 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();
}
}

Error in My LOGCAT when the app crashes.

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.&lt;init&gt;(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)

答案1

得分: 0

你在日志中看到的错误是:

 找到了冲突的名称为“getText”的获取器

Firebase数据库只能存储JSON类型,但您将一个编辑文本发送到它
错误确切位置在这里:

  userMap.put("day", Day);
userMap.put("month", Month);
userMap.put("year", Year);
userMap.put("state", State);

您将Day、Month、Year和State作为编辑文本发送,而不是字符串
请像这样做:

  userMap.put("day", day);
userMap.put("month", month);
userMap.put("year", year);
userMap.put("state", state);

这样就可以正常工作了。

英文:

your error as shown in logcat is

 Found conflicting getters for name: getText

the Firebase Database can only store JSON types but you send to it an Edit Text
the error exactly here

  userMap.put(&quot;day&quot;,Day);
userMap.put(&quot;month&quot;,Month);
userMap.put(&quot;year&quot;,Year);
userMap.put(&quot;state&quot;,State);

you send Day ,Month ,Year and State as Edit Text not String
do it like these

  userMap.put(&quot;day&quot;,day);
userMap.put(&quot;month&quot;,month);
userMap.put(&quot;year&quot;,year);
userMap.put(&quot;state&quot;,state);

and it will work fine

huangapple
  • 本文由 发表于 2020年9月13日 14:41:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63867931.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定