[英]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.