【问题标题】:java.lang.RuntimeException: Found conflicting getters for name isImportantForAccessibility on class androidx.appcompat.widget.AppCompatImageViewjava.lang.RuntimeException:在类 androidx.appcompat.widget.AppCompatImageView 上发现名称 isImportantForAccessibility 的 getter 冲突
【发布时间】:2020-07-10 20:06:55
【问题描述】:

我想将我的图像保存到哈希映射中,以便可以在 CartActivity 中显示它,但它显示此错误

2020-03-30 17:26:55.420 28869-28869/com.example.naturalmart E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.naturalmart, PID: 28869
    java.lang.RuntimeException: Found conflicting getters for name isImportantForAccessibility on class androidx.appcompat.widget.AppCompatImageView
        at com.google.firebase.firestore.util.CustomClassMapper$BeanMapper.<init>(com.google.firebase:firebase-firestore@@21.4.1:629)
        at com.google.firebase.firestore.util.CustomClassMapper.loadOrCreateBeanMapperForClass(com.google.firebase:firebase-firestore@@21.4.1:377)
        at com.google.firebase.firestore.util.CustomClassMapper.serialize(com.google.firebase:firebase-firestore@@21.4.1:177)
        at com.google.firebase.firestore.util.CustomClassMapper.serialize(com.google.firebase:firebase-firestore@@21.4.1:140)
        at com.google.firebase.firestore.util.CustomClassMapper.serialize(com.google.firebase:firebase-firestore@@21.4.1:104)
        at com.google.firebase.firestore.util.CustomClassMapper.convertToPlainJavaTypes(com.google.firebase:firebase-firestore@@21.4.1:78)
        at com.google.firebase.firestore.UserDataReader.convertAndParseDocumentData(com.google.firebase:firebase-firestore@@21.4.1:231)
        at com.google.firebase.firestore.UserDataReader.parseSetData(com.google.firebase:firebase-firestore@@21.4.1:75)
        at com.google.firebase.firestore.DocumentReference.set(com.google.firebase:firebase-firestore@@21.4.1:166)
        at com.google.firebase.firestore.DocumentReference.set(com.google.firebase:firebase-firestore@@21.4.1:146)
        at com.example.naturalmart.ProductDetailsActivity.addingToCartList(ProductDetailsActivity.java:200)
        at com.example.naturalmart.ProductDetailsActivity.access$300(ProductDetailsActivity.java:60)
        at com.example.naturalmart.ProductDetailsActivity$2.onClick(ProductDetailsActivity.java:124)
        at android.view.View.performClick(View.java:6319)
        at android.widget.TextView.performClick(TextView.java:11202)
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:967)
        at android.view.View$PerformClick.run(View.java:23995)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6816)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1567)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1455)

我的 ProductDetailsActivity.java 文件

package com.example.naturalmart;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.icu.text.CaseMap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Parcelable;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.cepheuen.elegantnumberbutton.view.ElegantNumberButton;
import com.example.naturalmart.Model.Products;
import com.example.naturalmart.Prevalent.Prevalent;
import com.facebook.shimmer.ShimmerFrameLayout;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
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 com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.EventListener;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreException;
import com.google.firebase.firestore.Query;
import com.google.firebase.firestore.QuerySnapshot;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
import com.squareup.picasso.Downloader;
import com.squareup.picasso.Picasso;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;

public class ProductDetailsActivity extends AppCompatActivity {
    private Button addToCartButton;
    private FirebaseAuth fAuth;
    private FirebaseUser fUser;
    private FirebaseFirestore firebaseFirestore;
    private ImageView productImage;
    private Products products;
    private ElegantNumberButton numberButton;
    private TextView productPrice, productDescription, productName;
    private String productID = "", state = "Normal";
    private FloatingActionButton addToWishlistButton;
    private ShimmerFrameLayout shimmerDetails;

    private static boolean ALREADY_ADDED_TO_WISHLIST = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_details);

        Toolbar toolbar3 = (Toolbar) findViewById(R.id.toolbar3);
        setSupportActionBar(toolbar3);

        String title3 = getIntent().getStringExtra("pname");
        toolbar3.setTitle(title3);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

        productID = getIntent().getStringExtra("pid");

        addToCartButton = (Button) findViewById(R.id.pd_add_to_cart_button);
        numberButton = (ElegantNumberButton) findViewById(R.id.number_btn);
        productImage = (ImageView) findViewById(R.id.product_image_details);
        productName = (TextView) findViewById(R.id.product_name_details);
        productDescription = (TextView) findViewById(R.id.product_description_details);
        productPrice = (TextView) findViewById(R.id.product_price_details);

        fAuth = FirebaseAuth.getInstance();
        fUser = fAuth.getCurrentUser();
        firebaseFirestore = FirebaseFirestore.getInstance();


        addToWishlistButton = findViewById(R.id.add_to_wishlist_btn);
        shimmerDetails = findViewById(R.id.shimmer_details);

        shimmerDetails.startShimmer();

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {

                getProductDetails(productID);
            }
        }, 2000);


        addToCartButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (state.equals("Order Placed")) {
                    Toast.makeText(ProductDetailsActivity.this, "you can not purchase more products, until your order is shipped or confirmed.", Toast.LENGTH_LONG).show();
                } else {
                    addingToCartList();
                }
                addingToCartList();
            }
        });
        addToWishlistButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ALREADY_ADDED_TO_WISHLIST) {
                    ALREADY_ADDED_TO_WISHLIST = false;
                    addToWishlistButton.setSupportImageTintList(ColorStateList.valueOf(Color.parseColor("#B5B5B5")));
                } else {
                    ALREADY_ADDED_TO_WISHLIST = true;
                    addToWishlistButton.setSupportImageTintList(getResources().getColorStateList(R.color.colorPrimary));
                }
            }
        });
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.search_and_cart, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_cart) {
            Intent intent = new Intent(ProductDetailsActivity.this, CartActivity.class);
            startActivity(intent);
        } else if (id == R.id.action_search) {
            Intent intent = new Intent(ProductDetailsActivity.this, SearchProductsActivity.class);
            startActivity(intent);
        }

        super.onBackPressed();
        return true;
    }


    @Override
    protected void onStart() {
        super.onStart();

        CheckOrderState();
    }

    private void addingToCartList()
    {
        String saveCurrentTime, saveCurrentDate;

        FirebaseFirestore firebaseFirestore = FirebaseFirestore.getInstance();
        CollectionReference cartListRef = firebaseFirestore.collection("Cart List");

        Calendar calForDate = Calendar.getInstance();
        SimpleDateFormat currentDate = new SimpleDateFormat("MMM dd, yyyy");
        saveCurrentDate = currentDate.format(calForDate.getTime());

        SimpleDateFormat currentTime = new SimpleDateFormat("HH:mm:ss a");
        saveCurrentTime = currentDate.format(calForDate.getTime());


        final HashMap<String, Object> cartMap = new HashMap<>();
        cartMap.put("pid", productID);
        cartMap.put("pname", productName.getText().toString());
        cartMap.put("price", productPrice.getText().toString());
        cartMap.put("date", saveCurrentDate);
        cartMap.put("time", saveCurrentTime);
        cartMap.put("quantity", numberButton.getNumber());
        cartMap.put("discount", "");
        cartMap.put("image", productImage);


        cartListRef.document(fAuth.getCurrentUser().getPhoneNumber())
                .set(cartMap)
                .addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        if (task.isSuccessful()) {
                            cartListRef.document(fAuth.getCurrentUser().getPhoneNumber())
                                    .set(cartMap)
                                    .addOnCompleteListener(new OnCompleteListener<Void>() {
                                        @Override
                                        public void onComplete(@NonNull Task<Void> task) {
                                            if (task.isSuccessful()) {
                                                Toast.makeText(ProductDetailsActivity.this, "Added to Cart List.", Toast.LENGTH_SHORT).show();

                                                Intent intent = new Intent(ProductDetailsActivity.this, HomeActivity.class);
                                                startActivity(intent);
                                            }
                                            else
                                            {
                                                Toast.makeText(ProductDetailsActivity.this, "error!!!", Toast.LENGTH_SHORT).show();
                                            }
                                        }
                                    });
                        }
                    }
                });
    }


    private void getProductDetails(String productID) {

        firebaseFirestore = FirebaseFirestore.getInstance();
        DocumentReference documentReference = firebaseFirestore.collection("Products").document(productID);

        documentReference.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
            @Override
            public void onSuccess(DocumentSnapshot documentSnapshot) {
                if (documentSnapshot.exists()) {
                    Products products = documentSnapshot.toObject(Products.class);

                    productName.setText(documentSnapshot.getString("pname"));
                    productPrice.setText(products.getPrice());
                    productDescription.setText(products.getDescription());
                    Picasso.get().load(products.getImage()).into(productImage);
                }
                else
                {
                    Toast.makeText(ProductDetailsActivity.this, "error!", Toast.LENGTH_SHORT).show();

                }            }
        });
        shimmerDetails.stopShimmer();
        shimmerDetails.setVisibility(View.INVISIBLE);
        addToCartButton.setVisibility(View.VISIBLE);
        numberButton.setVisibility(View.VISIBLE);
    }

    private void CheckOrderState() {
        DocumentReference ordersRef;
        ordersRef = FirebaseFirestore.getInstance().collection("Orders").document(fAuth.getCurrentUser().getPhoneNumber());

        ordersRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
            @Override
            public void onSuccess(DocumentSnapshot documentSnapshot) {
                if (documentSnapshot.exists()) {
                    String shippingState = documentSnapshot.getString("state");
                    if (shippingState.equals("shipped"))  {
                        state = "Order Shipped";
                    } else if (shippingState.equals("not shipped")) {
                        state = "Order Placed";
                    }
                }

            }
        });

    }


}

请告诉我应该如何在我的 HashMap 中保存图像 URL

我不太懂编码,所以我从 Youtube 视频和博客中获取内容,但我找不到这个问题

我在 HashMap 处收到错误:

final HashMap<String, Object> cartMap = new HashMap<>();
        cartMap.put("pid", productID);
        cartMap.put("pname", productName.getText().toString());
        cartMap.put("price", productPrice.getText().toString());
        cartMap.put("date", saveCurrentDate);
        cartMap.put("time", saveCurrentTime);
        cartMap.put("quantity", numberButton.getNumber());
        cartMap.put("discount", "");
        cartMap.put("image", productImage);

在线:

 cartMap.put("image, productImage);

我的 Build.Gradle 文件:

我的 Build.Gradle 文件是:

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

android {

    compileSdkVersion 29
    buildToolsVersion "28.0.3"

    defaultConfig {
        applicationId "com.example.naturalmart"
        minSdkVersion 23
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility = 1.8
        targetCompatibility = 1.8
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'com.nineoldandroids:library:2.4.0'
    implementation 'com.daimajia.slider:library:1.1.5@aar'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.firebase:firebase-analytics:17.2.3'
    implementation 'com.google.firebase:firebase-core:17.2.3'
    implementation 'com.google.firebase:firebase-database:19.2.1'
    implementation 'com.google.firebase:firebase-storage:19.1.1'
    implementation 'com.firebaseui:firebase-ui-database:3.2.2'
    implementation 'com.firebaseui:firebase-ui-firestore:6.2.1'
    implementation 'com.squareup.picasso:picasso:2.71828'
    implementation 'com.github.firdausmaulan:GlideSlider:1.5.1'
    implementation 'net.steamcrafted:materialiconlib:1.1.5'
    implementation 'com.github.rey5137:material:1.2.5'
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'androidx.recyclerview:recyclerview:1.1.0'
    implementation 'io.paperdb:paperdb:2.6'
    implementation 'com.cepheuen.elegant-number-button:lib:1.0.2'
    implementation 'com.google.android.material:material:1.2.0-alpha02'
    implementation 'de.hdodenhof:circleimageview:3.1.0'
    implementation 'com.theartofdev.edmodo:android-image-cropper:2.7.+'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.navigation:navigation-fragment:2.2.1'
    implementation 'androidx.navigation:navigation-ui:2.2.1'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.firebase:firebase-firestore:21.4.1'
    implementation 'com.github.bumptech.glide:glide:4.11.0'
    implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
    implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
    implementation 'com.facebook.shimmer:shimmer:0.5.0'
    implementation 'io.reactivex.rxjava3:rxjava:3.0.1'
    api 'com.google.android.material:material:1.2.0-alpha05'
    implementation 'com.google.firebase:firebase-auth:19.3.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
    testImplementation 'junit:junit:4.13'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

【问题讨论】:

  • 你的gradle.build文件可能没有为androidX正确配置
  • 您在哪一行代码中遇到了该错误?还请添加模型类的内容。

标签: java firebase google-cloud-firestore hashmap getter


【解决方案1】:

问题出在这一行:

cartMap.put("image", productImage);

您已将productImage 声明为:

private ImageView productImage;

而且无法将ImageView 保存到 Firestore,因为 Firestore 只能包含列出的数据类型 here。您收到的错误是 Firestore 客户端表示它不知道如何处理您传入的类型。


如果您想将图像数据本身存储在 Firestore 中,则必须 get the bytes from the image view 并存储这些数据。但现在更常见的是store the image data itself in Cloud Storage through Firebase,然后将该图像的路径(或其下载 URL)存储在 Firestore 中。

【讨论】:

  • 我已经为此活动从 Firebase 存储中加载了图像,但我想在添加到购物车 Btn 上添加 OnClick 侦听器,以便将此特定产品的数据保存在 Firestore 文档中
  • 我不确定这意味着什么。但我知道你得到的错误是由我在这里描述的:你不能将ImageView 字段写入数据库。
  • 我使用上传任务将图像从一个活动上传到 firebase 存储,然后我在我的主要产品活动中使用“model.getImage”方法显示该图像,现在我想在上显示特定产品的图像购物车活动
  • 这是一个用例。但是,如果您查看您的问题,您发布的异常来自您尝试向 Firestore 写入 ImageView 的事实,这是不受支持的。要删除异常,请删除此行 cartMap.put("image", productImage)
  • 放置后如何从 Firestore 加载该位图。
猜你喜欢
  • 1970-01-01
  • 2018-08-24
  • 2019-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-18
相关资源
最近更新 更多