我正在使用Android应用程序,但出现以下错误:
2019-05-05 18:19:35.582 9087-9087/com.sale.organiccrops.organiccropsonsale E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.sale.organiccrops.organiccropsonsale, PID: 9087
com.google.firebase.database.DatabaseException: No properties to serialize found on class com.sale.organiccrops.organiccropsonsale.CartData
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.<init>(com.google.firebase:firebase-database@@16.1.0:535)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.loadOrCreateBeanMapperForClass(com.google.firebase:firebase-database@@16.1.0:317)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.serialize(com.google.firebase:firebase-database@@16.1.0:166)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.serialize(com.google.firebase:firebase-database@@16.1.0:152)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToPlainJavaTypes(com.google.firebase:firebase-database@@16.1.0:60)
at com.google.firebase.database.DatabaseReference.setValueInternal(com.google.firebase:firebase-database@@16.1.0:292)
at com.google.firebase.database.DatabaseReference.setValue(com.google.firebase:firebase-database@@16.1.0:166)
at com.sale.organiccrops.organiccropsonsale.CheckoutActivity$2.onClick(CheckoutActivity.java:64)
at android.view.View.performClick(View.java:6304)
at android.view.View$PerformClick.run(View.java:24803)
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:6635)
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:823)
CheckoutActivity的Java代码是:
package com.sale.organiccrops.organiccropsonsale;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
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.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.util.ArrayList;
import java.util.Objects;
public class CheckoutActivity extends AppCompatActivity {
private EditText address;
private DatabaseReference reference;
private TextView amount;
private Double sum;
private ArrayList<CartData> data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkout);
amount = findViewById(R.id.amount);
address = findViewById(R.id.address);
data = new ArrayList<>();
Button confirm = findViewById(R.id.confirm);
if (FirebaseAuth.getInstance().getCurrentUser() != null) {
reference = FirebaseDatabase.getInstance().getReference("users/" + FirebaseAuth.getInstance().getCurrentUser().getUid() + "/cart");
reference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
data.clear();
sum = 0.0;
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
data.add(new CartData(snapshot.child("english").getValue(String.class), snapshot.child("hindi").getValue(String.class), snapshot.child("quantity").getValue(String.class), snapshot.child("image").getValue(String.class), snapshot.child("price").getValue()));
sum += snapshot.child("price").getValue(Double.class);
}
amount.setText(new StringBuilder().append("₹ ").append(sum));
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
confirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (address.length() == 0) {
address.requestFocus();
address.setError("This Is A Required Field");
} else {
reference = FirebaseDatabase.getInstance().getReference("admins/orders/pending").push();
reference.child("user").setValue(FirebaseAuth.getInstance().getCurrentUser().getUid());
reference.child("address").setValue(address.getText().toString());
reference.child("items").setValue(data);
reference.child("price").setValue(Double.parseDouble(amount.getText().toString().replace("₹ ", "")));
reference = FirebaseDatabase.getInstance().getReference("users/" + FirebaseAuth.getInstance().getCurrentUser().getUid() + "/orders/ongoing").child(Objects.requireNonNull(reference.getKey()));
reference.child("address").setValue(address.getText().toString());
reference.child("items").setValue(data);
reference.child("price").setValue(Double.parseDouble(amount.getText().toString().replace("₹ ", ""))).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (FirebaseAuth.getInstance().getCurrentUser() != null) {
reference = FirebaseDatabase.getInstance().getReference("users/" + FirebaseAuth.getInstance().getCurrentUser().getUid() + "/cart");
reference.removeValue().addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Toast.makeText(getBaseContext(), "Order Placed Successfully", Toast.LENGTH_SHORT).show();
startActivity(new Intent(getBaseContext(), UserDashboardActivity.class));
finish();
}
});
}
}
});
}
}
});
}
@Override
public void onBackPressed() {
startActivity(new Intent(getBaseContext(), CartActivity.class));
finish();
}
@Override
protected void onStart() {
super.onStart();
if (FirebaseAuth.getInstance().getCurrentUser() == null) {
startActivity(new Intent(getBaseContext(), AuthenticationActivity.class));
finish();
}
}
@Override
protected void onRestart() {
super.onRestart();
if (FirebaseAuth.getInstance().getCurrentUser() == null) {
startActivity(new Intent(getBaseContext(), AuthenticationActivity.class));
finish();
}
}
@Override
protected void onResume() {
super.onResume();
if (FirebaseAuth.getInstance().getCurrentUser() == null) {
startActivity(new Intent(getBaseContext(), AuthenticationActivity.class));
finish();
}
}
}
CartData,CartActivity和CartAdapter的Java代码是:
package com.sale.organiccrops.organiccropsonsale;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.google.firebase.auth.FirebaseAuth;
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.util.ArrayList;
import java.util.Objects;
class CartData {
private String english, hindi, quantity, url;
private Object price;
CartData(String english, String hindi, String quantity, String url, Object price) {
this.english = english;
this.hindi = hindi;
this.quantity = quantity;
this.url = url;
this.price = price;
}
String getEnglish() {
return english;
}
String getHindi() {
return hindi;
}
String getQuantity() {
return quantity;
}
String getUrl() {
return url;
}
Object getPrice() {
return price;
}
void setPrice(Object price) {
this.price = price;
}
}
public class CartActivity extends AppCompatActivity {
private ProgressBar progress;
private ArrayList<CartData> data;
private TextView products;
private RecyclerView recyclerView;
private Button checkout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
progress = findViewById(R.id.progress);
progress.setVisibility(View.VISIBLE);
products = findViewById(R.id.products);
checkout = findViewById(R.id.checkout);
data = new ArrayList<>();
if (FirebaseAuth.getInstance().getCurrentUser() != null) {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("users/" + FirebaseAuth.getInstance().getCurrentUser().getUid() + "/cart");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
data.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
data.add(new CartData(snapshot.child("english").getValue(String.class), snapshot.child("hindi").getValue(String.class), snapshot.child("quantity").getValue(String.class), snapshot.child("image").getValue(String.class), snapshot.child("price").getValue()));
}
progress.setVisibility(View.GONE);
if (data.isEmpty()) {
products.setVisibility(View.VISIBLE);
checkout.setEnabled(false);
} else {
products.setVisibility(View.GONE);
checkout.setEnabled(true);
checkout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getBaseContext(), CheckoutActivity.class));
finish();
}
});
}
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getBaseContext()));
recyclerView.setAdapter(new CartAdapter(data));
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
@Override
public void onBackPressed() {
startActivity(new Intent(getBaseContext(), UserDashboardActivity.class));
finish();
}
@Override
protected void onStart() {
super.onStart();
if (FirebaseAuth.getInstance().getCurrentUser() == null) {
startActivity(new Intent(getBaseContext(), AuthenticationActivity.class));
finish();
}
}
@Override
protected void onRestart() {
super.onRestart();
if (FirebaseAuth.getInstance().getCurrentUser() == null) {
startActivity(new Intent(getBaseContext(), AuthenticationActivity.class));
finish();
}
}
@Override
protected void onResume() {
super.onResume();
if (FirebaseAuth.getInstance().getCurrentUser() == null) {
startActivity(new Intent(getBaseContext(), AuthenticationActivity.class));
finish();
}
}
}
class CartAdapter extends RecyclerView.Adapter<CartAdapter.ViewHolder> {
private ArrayList<CartData> data;
private DatabaseReference reference;
private String key;
CartAdapter(ArrayList<CartData> data) {
this.data = data;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
return new ViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_cart, viewGroup, false));
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, @SuppressLint("RecyclerView") final int i) {
viewHolder.english.setText(data.get(i).getEnglish());
viewHolder.hindi.setText(data.get(i).getHindi());
viewHolder.quantity.setText(data.get(i).getQuantity());
viewHolder.price.setText(new StringBuilder().append("₹ ").append(data.get(i).getPrice()));
GlideApp.with(viewHolder.image.getContext()).load(data.get(i).getUrl()).into(viewHolder.image);
viewHolder.delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (FirebaseAuth.getInstance().getCurrentUser() != null) {
reference = FirebaseDatabase.getInstance().getReference("users/" + FirebaseAuth.getInstance().getCurrentUser().getUid() + "/cart");
reference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
if (Objects.equals(snapshot.child("english").getValue(), data.get(i).getEnglish()) && Objects.equals(snapshot.child("hindi").getValue(), data.get(i).getHindi()) && Objects.equals(snapshot.child("quantity").getValue(), data.get(i).getQuantity()) && Objects.equals(snapshot.child("price").getValue(), data.get(i).getPrice())) {
key = snapshot.getKey();
}
}
if (key != null) {
reference.child(key).removeValue();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
});
}
@Override
public int getItemCount() {
return data.size();
}
static class ViewHolder extends RecyclerView.ViewHolder {
private TextView english, hindi, quantity, price;
private ImageButton delete;
private ImageView image;
ViewHolder(View itemView) {
super(itemView);
this.english = itemView.findViewById(R.id.english);
this.hindi = itemView.findViewById(R.id.hindi);
this.quantity = itemView.findViewById(R.id.quantity);
this.price = itemView.findViewById(R.id.price);
this.delete = itemView.findViewById(R.id.delete);
this.image = itemView.findViewById(R.id.image);
}
}
}
使用Android Studio安装应用程序时,它运行正常。但是在构建APK并使用APK安装应用后,出现上述错误。有人可以帮我解决这个问题吗?预先感谢。
答案 0 :(得分:0)
在您的CartData中,您可能会错过那些设置方法。
void setEnglish() {
this.english = english;
}
对Hindi
,Quantity
和Url
做同样的事情。