我一直在尝试使用Firebase将数据从Recyclerview传递到详细活动,但失败了.我总是出错



android编程新手,开始学习。我希望任何人都能指导我解决这个问题。谢谢我遇到的问题是,当用户单击列表时,我似乎无法在RestaurantInfoActivity中显示数据。它将不断提示该错误。如果你需要更多信息,请告诉我

餐厅适配器

@Override
protected void onBindViewHolder(@NonNull RestaurantViewHolder holder, final int position, @NonNull 
final RestaurantDetails details) {
holder.restaurant_Name.setText(details.getName());
holder.restaurant_Category.setText(details.getCategory());
holder.restaurant_Location.setText(details.getLocation());
holder.itemView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
String post_key = getRef(position).getKey();
Intent intent = new Intent(view.getContext(), RestaurantInfoActivity.class);
intent.putExtra("Post_key", position);
view.getContext().startActivity(intent);
}
});
}

''

餐厅信息活动

public class RestaurantInfoActivity extends AppCompatActivity{
TextView iRestaurantName, iRestaurantCategory, iRestaurantAddress, iRestaurantLocation;
private static final String TAG = RestaurantInfoActivity.class.getSimpleName();
DatabaseReference mReference;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.restaurant_details);
Log.d(TAG, "onCreate: started.");
FirebaseRecyclerOptions<RestaurantDetails> options =
new FirebaseRecyclerOptions.Builder<RestaurantDetails>()
.setQuery(FirebaseDatabase.getInstance().getReference().child("Restaurant"), RestaurantDetails.class)
.build();
//Toast.makeText(RestaurantInfoActivity.this, post_key,Toast.LENGTH_LONG).show();
//Log.d(TAG,"onClick" + post_key );

mReference = FirebaseDatabase.getInstance().getReference().child("Restaurant");
final String post_key = getIntent().getExtras().get("post_key").toString();
iRestaurantName = (TextView) findViewById(R.id.restaurantName);
iRestaurantAddress = (TextView) findViewById(R.id.restaurantAddress);
iRestaurantCategory = (TextView) findViewById(R.id.restaurantCategory);
iRestaurantLocation = (TextView) findViewById(R.id.restaurantLocation);
mReference.child(post_key).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
RestaurantDetails details = dataSnapshot.getValue(RestaurantDetails.class);
if(dataSnapshot.exists()){
RestaurantDetails nameRestaurant = dataSnapshot.child("name").getValue().toString();
RestaurantDetails locationRestaurant = dataSnapshot.child("location").getValue().toString();
RestaurantDetails addressRestaurant = dataSnapshot.child("address").getValue().toString();
RestaurantDetails categoryRestaurant = dataSnapshot.child("category").getValue().toString();

iRestaurantName.setText( nameRestaurant);
iRestaurantLocation.setText(locationRestaurant);
iRestaurantAddress.setText(addressRestaurant);
iRestaurantCategory.setText(categoryRestaurant);

}

''

错误日志

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.isaac.foodie/com.isaac.foodie.RestaurantInfoActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3121)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3264)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1955)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7078)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at com.isaac.foodie.RestaurantInfoActivity.onCreate(RestaurantInfoActivity.java:49)
at android.app.Activity.performCreate(Activity.java:7327)
at android.app.Activity.performCreate(Activity.java:7318)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1275)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3101)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3264) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1955) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7078) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964) 

我在你的点击事件中看到,你发送post key并不是有意的,而是在发送位置。将您的点击事件更改为此

holder.itemView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
String post_key = getRef(position).getKey();
Intent intent = new Intent(view.getContext(), RestaurantInfoActivity.class);
intent.putExtra("Post_key", post_key);
view.getContext().startActivity(intent);
}
});

差异:

intent.putExtra("Post_key", position);

final String post_key = getIntent().getExtras().get("post_key").toString();

Post_key而不是Post_keyTR

最新更新