2017-04-02 50 views
-2

我在使用firebase製作購物應用程序。我使用Recycler View顯示數據。每個功能工作正常,但每當我添加到了indivisual商品購物車按鈕點擊它拋出這樣一個異常:Found DatabaseException:發現名稱爲衝突的獲取者:isImportantForAccessibility

登錄貓

04-02 11:41:57.480 1418-1418/com.app.demo E/Zygote: no v2 
04-02 11:42:08.550 1418-1418/com.app.demo E/ViewRootImpl: sendUserActionEvent() mView == null 
04-02 11:42:16.368 1418-1418/com.app.hdservices E/AndroidRuntime: FATAL EXCEPTION: main Process: com.app.demo, 
    PID: 1418 com.google.firebase.database.DatabaseException: 
    Found conflicting getters for name: isImportantForAccessibility 

    at com.google.android.gms.internal.zzbtg$zza.<init>(Unknown Source) 
    at com.google.android.gms.internal.zzbtg.zzi(Unknown Source) 
    at com.google.android.gms.internal.zzbtg.zzaz(Unknown Source) 
    at com.google.android.gms.internal.zzbtg.zzay(Unknown Source) 
    at com.google.firebase.database.DatabaseReference.zza(Unknown Source) 
    at com.google.firebase.database.DatabaseReference.setValue(Unknown Source) 
    at com.app.hdservices.Veglist$VegViewHolder$3.onClick(Veglist.java:258) 
    at android.view.View.performClick(View.java:5716) 
    at android.widget.TextView.performClick(TextView.java:10926) 
    at android.view.View$PerformClick.run(View.java:22596) 
    at android.os.Handler.handleCallback(Handler.java:739) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:7325) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

Veglist.java

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

     mAuth=FirebaseAuth.getInstance(); 
     mAuthListener=new FirebaseAuth.AuthStateListener() { 
      @Override 
      public void onAuthStateChanged(FirebaseAuth firebaseAuth) { 

       if(firebaseAuth.getCurrentUser()==null) 
       { 
        Intent i=new Intent(Veglist.this,Login.class); 
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
        startActivity(i); 

       } 

      } 
     }; 

     mainReference = FirebaseDatabase.getInstance().getReference().child("Vegetable"); 
     cartReference = FirebaseDatabase.getInstance().getReference().child("Cart"); 
     cartobject = cartReference.child(mAuth.getCurrentUser().getUid()).push(); 

     mainRecyclerView=(RecyclerView)findViewById(R.id.recyclerview); 
     mainRecyclerView.setHasFixedSize(true); 
     mainRecyclerView.setLayoutManager(new LinearLayoutManager(this)); 

     mainReference.keepSynced(true); 
     cartReference.keepSynced(true); 
     cartobject.keepSynced(true); 

    Toolbar toolbar = (Toolbar) findViewById(R.id.newToolbar); 
     setSupportActionBar(toolbar); 

     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     getSupportActionBar().setTitle("Fresh Vegetables"); 
     getSupportActionBar().getThemedContext(); 
     toolbar.setTitleTextColor(0xFFFFFFFF); 

    } 

public static class VegViewHolder extends RecyclerView.ViewHolder { 

     public View mView; 
     private ImageView postImage; 
     private TextView quantityBox,postVegName,postPrice,postPriceTag; 
     private ImageButton reducequantityButton,increaseQuantityButton; 
     private Button add; 
     private String mPostKey=null; 
     String totalCost; 
     private DatabaseReference cartReference; 
     private DatabaseReference cartobject; 
     private FirebaseAuth mAuth; 


     public VegViewHolder(final View itemView) { 
      super(itemView); 
      mView = itemView; 

      mAuth=FirebaseAuth.getInstance(); 

      cartReference = FirebaseDatabase.getInstance().getReference().child("Cart"); 
      cartobject = cartReference.child(mAuth.getCurrentUser().getUid()).push(); 

      add=(Button)mView.findViewById(R.id.add); 
      quantityBox=(TextView)mView.findViewById(R.id.quantity); 
      reducequantityButton = (ImageButton) mView.findViewById(R.id.downQuantitybutton); 
      increaseQuantityButton = (ImageButton) mView.findViewById(R.id.upQuantitybutton); 


        quantityBox.setText("1"); 

      increaseQuantityButton.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        try { 

         int i = Integer.parseInt(quantityBox.getText().toString()); 
         int j = i + 1; 
         String k = String.valueOf(j); 
         quantityBox.setText(k); 
        } catch (Exception e) { 
         Toast.makeText(itemView.getContext(), "Quantity can only be a number", Toast.LENGTH_SHORT).show(); 
        } 
       } 
      }); 

     reducequantityButton.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       try { 

        if (Integer.parseInt(quantityBox.getText().toString()) <= 1) { 
         Toast.makeText(itemView.getContext(), "Quantity can't be less than 1", Toast.LENGTH_SHORT).show(); 
        } else { 
         int i = Integer.parseInt(quantityBox.getText().toString()); 
         int j = i - 1; 
         String k = String.valueOf(j); 
         quantityBox.setText(k); 
        } 

       } catch (Exception e) { 
        Toast.makeText(itemView.getContext(), "Quantity can only be a number", Toast.LENGTH_SHORT).show(); 
       } 
      } 
     }); 




       add.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
        final ProgressDialog p=new ProgressDialog(mView.getContext()); 
         p.setMessage("Wait! Adding to cart .."); 
         p.show(); 


         cartobject.child("Image").setValue(postImage); 
         cartobject.child("Name").setValue(postVegName); 
         cartobject.child("Price").setValue(postPrice); 
         cartobject.child("PriceTag").setValue(postPriceTag); 
         cartobject.child("Quantity").setValue(quantityBox.getText().toString()); 

         totalCost = String.valueOf(Integer.parseInt(quantityBox.getText().toString()) * Integer.parseInt(postPrice.getText().toString())); 

         cartobject.child("Total").setValue(totalCost).addOnSuccessListener(new OnSuccessListener<Void>() { 
          @Override 
          public void onSuccess(Void aVoid) { 
           p.dismiss(); 
           Toast.makeText(mView.getContext(), postVegName + " successfully added to cart! ", Toast.LENGTH_SHORT).show(); 
          } 

         }); 
        } 
       }); 





      } 

     public void setName(String name) { 
      postVegName = (TextView) mView.findViewById(R.id.vegName); 
      postVegName.setText(name); 

     } 

     public void setPrice(String price) { 
      postPrice = (TextView) mView.findViewById(R.id.VegPrice); 
      postPrice.setText(price); 


     } 

     public void setPriceTag(String priceTag) 
     { 
      postPriceTag=(TextView)mView.findViewById(R.id.PriceTag); 
      postPriceTag.setText(priceTag); 
     } 

     public void setImage(Context context, String image) { 
      postImage = (ImageView) mView.findViewById(R.id.VegImg); 
      Picasso.with(context).load(image).into(postImage); 
     } 






    } 
+2

兩個目標文件改變method.I名認爲這解決您的問題。 –

回答

0

這些調用setValue()是問題:

cartobject.child("Image").setValue(postImage); 
cartobject.child("Name").setValue(postVegName); 
cartobject.child("Price").setValue(postPrice); 
cartobject.child("PriceTag").setValue(postPriceTag); 

您不能存儲Firebase數據庫中的。你可能意味着存儲包含在視圖中的值,例如:具有相同的getName()的setName()method.Try

cartobject.child("Name").setValue(postVegName.getText().toString().trim()); 
+0

謝謝Bob Snyder先生。現在一切正常,但我想從你那裏得到一點幫助,我如何獲得圖像URL,因爲gettext()方法不能用於圖像,請上次幫助我。我使用childobject.child(「Image」)。setValue(mView.getContext(),postImage); – Suresh

+0

是的,一個'ImageView'不知道你用來加載它的URL。當您從URL設置ImageView時,您需要將URL保存在其他地方。 –

+0

先生,我試過但沒有結果。請你告訴我如何從上面的代碼獲取圖像URL鏈接。請給我代碼片段。並感謝先進的先生。請幫助我。 – Suresh