您好,我一直在研究问题并找到了将字符串从 Activity (不是 fragment Activity )传递到 fragment 的解决方案。我想在点击时将文档 ID 从 RestaurantList.class 传递到 Mainsfragment.class,在我的 onclick 中。
我收到的错误消息如下:
尝试在空对象引用上调用虚拟方法“android.content.Intent android.app.Activity.getIntent()”
这是我的 Activity 代码:
package com.example.hostapp;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.RelativeLayout;
import com.example.hostapp.Adapters.RestaurantAdapter;
import com.example.hostapp.Adapters.categoryCardAdapter;
import com.example.hostapp.MenuFragments.MainsFragment;
import com.example.hostapp.Models.Restaurant;
import com.example.hostapp.Models.categoryCard;
import com.firebase.ui.firestore.FirestoreRecyclerOptions;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;
public class RestaurantList extends AppCompatActivity {
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private CollectionReference restaurantref = db.collection("restaurants");
private RestaurantAdapter adapter;
String categoryid = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_restaurant_list);
setUpRecyclerView();
}
private void setUpRecyclerView() {
//Get intent
if (getIntent() != null)
categoryid = getIntent().getStringExtra("categoryid");
if (!categoryid.isEmpty() && categoryid != null) {
Query query = restaurantref.whereEqualTo("categoryid", categoryid).orderBy("name");
final FirestoreRecyclerOptions<Restaurant> options = new FirestoreRecyclerOptions.Builder<Restaurant>().setQuery(query, Restaurant.class).build();
adapter = new RestaurantAdapter(options);
RecyclerView recyclerView = findViewById(R.id.restaurant_recycler);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
adapter.setOnItemClickListerner(new RestaurantAdapter.OnItemClickListener() {
@Override
public void onItemClick(DocumentSnapshot documentSnapshot, int position) {
Restaurant restaurant = documentSnapshot.toObject(Restaurant.class);
Intent foodlist = new Intent(RestaurantList.this, Foodlist.class);
Bundle bundle = new Bundle();
bundle.putString("restaurantid", documentSnapshot.getId());
MainsFragment m4 = new MainsFragment();
m4.setArguments(bundle);
//foodlist.putExtra("restaurantid", documentSnapshot.getId());
startActivity(foodlist);
}
});
}
}
@Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
}
这是我的 fragment 代码:
package com.example.hostapp.MenuFragments;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.hostapp.Adapters.FoodAdapter;
import com.example.hostapp.Models.FoodModel;
import com.example.hostapp.R;
import com.example.hostapp.RestaurantList;
import com.firebase.ui.firestore.FirestoreRecyclerOptions;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;
public class MainsFragment extends Fragment {
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private FoodAdapter adapter;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_mains, container, false);
String restaurantNumber = getArguments().getString("restaurantid");
Query menuref = db.collectionGroup("Foods").whereEqualTo("menuid", restaurantNumber);
FirestoreRecyclerOptions<FoodModel> options = new FirestoreRecyclerOptions.Builder<FoodModel>().
setQuery(menuref, FoodModel.class)
.build();
adapter = new FoodAdapter(options);
RecyclerView recyclerView = view.findViewById(R.id.recycler_mains);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
recyclerView.setAdapter(adapter);
return view;
}
private void setUpRecyclerView() {
}
@Override
public void onStart() {
super.onStart();
adapter.startListening();
}
@Override
public void onStop() {
super.onStop();
adapter.stopListening();
}
}
最佳答案
我也有过这样的经历。以下是我的处理方法:
1、通过intent.putExtra()将您的数据从RestaurantList Activity 传递到您的foodlist Activity ;
Intent foodlist = new Intent(RestaurantList.this, Foodlist.class);
foodlist.putExtra("restaurantid", documentSnapshot.getId());
startActivity(foodlist);
2、通过getIntent()从Foodlist Activity 中获取值;
String id = getIntent().getStringExtra("restaurantid");
3、将数据传递给正在初始化适配器的适配器。 (像这样:)
YourAdapter adapter = new YourAdapter(getSupportFragmentManager(), tabLayout.getTabCount(),id);
4,在适配器的构造函数中获取数据(您必须创建一个全局变量并将其填充到构造函数中)并在 getItem() 方法中创建一个包并将数据传递给它并将参数设置给 fragment (如下所示)代码)
public class YourAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
String id;
public AdapterPagerShift(FragmentManager fm, int NumOfTabs, String id) {
super(fm);
this.mNumOfTabs = NumOfTabs;
this.id = id;
}
@Override
public Fragment getItem(int position) {
Bundle bundle = new Bundle();
bundle.putString("restaurantid", documentSnapshot.getId());
switch (position) {
case 0:
MainsFragment main = new MainsFragment();
main.setArguments(bundle);
return main;
case 1:
//...
case 2:
//...
default:
return null;
}
}
@Override
public int getCount() {
return mNumOfTabs;
}
}
5、在MainsFragment类中使用以下代码获取数据:
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String id = getArguments().getString("restaurantid");
}
希望对你有帮助!
关于java - 将字符串从 Activity 传递到 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61373574/