簡體   English   中英

單擊 RecyclerView 后,當我想在新活動中顯示詳細信息時出現錯誤。 使用 Firebase

[英]Get error when I want to display detail in new activity after click on RecyclerView. Using Firebase

我希望ViewProduct活動顯示在recyclerView上單擊的項目的詳細信息。 我稍后的目標是將textView更改為editText以編輯產品,但現在我希望它顯示。

我的問題是用來自 Firebase 的特定產品的數據填充textView

您會看到有一條 toast 消息被注釋掉,該消息有效並顯示 position,所以我知道我以某種方式走在正確的軌道上。 現在,如果我單擊該項目,應用程序就會崩潰,並給出錯誤:

無法啟動活動 ComponentInfo

Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference

我現在需要的是用來自 Firebase 的數據填充textfield字段。

這是我的 RecyclerAdapter 中的 itemView:

            itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = getAdapterPosition();
                //Toast.makeText(context,"Position"+ position, Toast.LENGTH_SHORT).show();
                if (position != RecyclerView.NO_POSITION && listener != null) {
                    //listener.onItemClick(getSnapshots().getSnapshot(position), position);
                    listener.onItemClick(getSnapshots().getSnapshot(position),(position));
                    Intent intent = new Intent(itemView.getContext(), ViewProduct.class);
                    intent.putExtra("product_ID:", position);
                    itemView.getContext().startActivity(intent);

                }
            }
        });

這是我想查看詳細信息的 ViewProduct Activity: public class ViewProduct extends AppCompatActivity {

private String edit_product_ID;

private TextView mProduct, mPrice;
private TextView mSave, mDelete;

private DatabaseReference product_ref;



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

product_ref = FirebaseDatabase.getInstance().getReference().child("ultra_liquors");

edit_product_ID = getIntent().getExtras().get("product_ID").toString();


    //Toast.makeText(this, "The Product ID:" + edit_product_ID, Toast.LENGTH_SHORT).show();

    mProduct = (TextView) findViewById(R.id.product);
    mPrice = (TextView) findViewById(R.id.price);
    mSave = (TextView) findViewById(R.id.save);
    mDelete = (TextView) findViewById(R.id.delete);

    RetrieveProductInfo();

}

private void RetrieveProductInfo() {
    product_ref.child(edit_product_ID).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if ((dataSnapshot.exists()) && (dataSnapshot.hasChild(edit_product_ID))) {
                String product_name = dataSnapshot.child("product").getValue().toString();
                String product_price = dataSnapshot.child("price").getValue().toString();

                mProduct.setText(product_name);
                mPrice.setText(product_price);
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {

        }
    });
}

intent.putExtra("product_ID:", position);中的字符串后面有一個尾隨冒號符號。 這會導致getExtras().get(...)返回 null。 null.toString() 拋出錯誤

用 Intent 添加這一行

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

我認為這是問題嘗試並讓我知道!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM