简体   繁体   English

单击 RecyclerView 后,当我想在新活动中显示详细信息时出现错误。 使用 Firebase

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

I would like the ViewProduct activity to show the detail of the item clicked on the recyclerView .我希望ViewProduct活动显示在recyclerView上单击的项目的详细信息。 My aim later is to change the textView to a editText to edit the products, but for now I would like it to show.我稍后的目标是将textView更改为editText以编辑产品,但现在我希望它显示。

My problem is filling the textView with the data from the particular product from Firebase.我的问题是用来自 Firebase 的特定产品的数据填充textView

You will see there is a toast message that is commented out which works and show the position, so I know I am on the right track somehow.您会看到有一条 toast 消息被注释掉,该消息有效并显示 position,所以我知道我以某种方式走在正确的轨道上。 Now the app just crashes if I click on the item, giving error:现在,如果我单击该项目,应用程序就会崩溃,并给出错误:

Unable to start activity ComponentInfo无法启动活动 ComponentInfo

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

All I need now is to populate the textfield s with the data from Firebase.我现在需要的是用来自 Firebase 的数据填充textfield字段。

Here is my itemView in my RecyclerAdapter:这是我的 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);

                }
            }
        });

And here is my ViewProduct Activity where I want to see the details: public class ViewProduct extends AppCompatActivity {这是我想查看详细信息的 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) {

        }
    });
}

The string in intent.putExtra("product_ID:", position); intent.putExtra("product_ID:", position);中的字符串has a trailing colon-symbol behind it.后面有一个尾随冒号符号。 which causes getExtras().get(...) to return null.这会导致getExtras().get(...)返回 null。 null.toString() throws the error null.toString() 抛出错误

Add this line with Intent用 Intent 添加这一行

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

I think that was the issue try it and let me know!我认为这是问题尝试并让我知道!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我单击 RecyclerView 项目时,我希望 TextView 在同一活动中显示项目的标题 - When I click on RecyclerView Item I want the TextView in same activity to display Item's title 当我单击RecyclerView项时,我想获取ID - I want to get the id when I click a RecyclerView item 我想使用if语句单击导航抽屉上的项目时启动新的活动 - I want to launch new Activity when the Item on Navigation Drawer is click using if statement 当我单击 listView 项目时,如何在另一个活动中显示来自 firestore 的详细信息? - How to display detail informations from firestore in another activity when I click a listView item? 单击 RecyclerView 后,使用 Firebase 实时数据库填充详细信息活动 - Populate details activity with Firebase Real Time Database after click on RecyclerView PositionalDataSource和recyclerview。 为什么在invalidate()之后滚动? - PositionalDataSource and recyclerview. Why scroll after invalidate()? 创建新活动后如何显示 RecyclerView,Android Firebase - How to show RecyclerView after a new activity is created, Android Firebase 如何从RecyclerView获取值以进行详细活动 - How to get values from RecyclerView to detail activity 我想在 recyclerview 开始时显示上传到 firebase 存储的最新图像 - i want to display the newest image uploaded to firebase storage at the start of recyclerview 点击recyclerView new activity Error Attempt to invoke virtual method - Click recyclerView new activity Error Attempt to invoke virtual method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM