新手到Android编程和开始学习。我希望任何人都能指导我解决这个问题。谢谢。我遇到的问题是,当用户单击列表时,我似乎无法获得显示在餐厅信息活动中的数据。它会一直提示这个错误。请让我知道如果你需要进一步的信息
餐厅公寓
@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)
区别:
intent.putExtra("Post_key", position);
和
final String post_key = getIntent().getExtras().get("post_key").toString();
Post_键而不是Post_键
我在你的点击事件中看到,你不是有意发送post键,而是发送位置。将单击事件更改为此
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);
}
});
我想把数据RC_1RC_2。我怎么能这样做(猫,信息,orderid)回收站视图到回收站视图?Examp.pic_1 我的firebase节点与图片类似。我的firebase节点 //RC_1结构 //RC_2结构 RC_1用于//我的活动1 RC_1;//Myadapter_1 @在BindViewHolder上重写公共无效(@NonNull ViewHolder持有者,@SuppressLin
问题内容: 我需要在单击recyclerview的图像时将数据从传递 到 。有人可以帮忙吗? 问题答案: 创建一个侦听器接口,然后让您的MainActivity实现它。这样,您可以在onClick方法中调用回调方法。 接口: 主要活动: 您的VideoAdapter:
我尝试使用 如有任何帮助,不胜感激,谢谢。
问题内容: 将变量数据从传递到类的最佳方法是什么?数据是在第一个类中创建的,我需要将该数据传递给第二个类而不使用Intent。任何代码示例将不胜感激。 自从我开始工作以来,我就打算将其发布。就我而言,我已经有一个,而我正在做的事情我需要另一个相同的类型。因此,这里去: 班级活动 我的主要活动 请注意,我必须先创建主类的实例 问题答案: 如果您的班级名称是为此创建的,并将值作为参数传递,则下面是一个
我有三节课 1.菜单活动 2.LocationUpdateService 3.多重标记器 1.菜单活动 2、LocationUpdateService:(这是服务类) 3、多重标记(活动) 我的问题是:当我打开我的菜单活动我的Toast消息打印发送数据到广播接收器,然后点击按钮我调用MultipleMarker。我无法从服务中获取值。。。但当我按下后退按钮时,我重定向到MenuActivity,此
我想问一下如何将值从片段a传递到活动B,然后再传递回片段。我尝试使用bundle来传递值,但是它会给出错误的数据。 非常感谢。