Я новичок в Android Studio и Java, и я не могу решить эту проблему, с которой столкнулся. Приложение продолжает вылетать всякий раз, когда я пытаюсь отправить данные из Activity в Fragment (tabOne) с помощью Bundle, без этого все работает нормально, но мне нужно отправить это «BeerDescription».
MainActivity
public class MainActivity extends AppCompatActivity implements NameClickListener{
List<String> titles = new ArrayList<>();
List<String> desc = new ArrayList<>();
private RecyclerView recycler;
private RecyclerAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupRecycler();
setupRecyclerData();
}
private void setupRecycler(){
recycler = findViewById(R.id.recyclerView);
recycler.setLayoutManager(new LinearLayoutManager(this));
adapter = new RecyclerAdapter(this);
recycler.setAdapter(adapter);
}
private void setupRecyclerData(){
titles.add("India Pale Ale");
titles.add("Weissbier");
titles.add("Lager");
titles.add("Pale Ale");
titles.add("Session Ipa");
titles.add("Hoppy Lager");
desc.add("Is a hoppy beer style within the broader category of pale ale. " +
"The pale ales of the early eighteenth century were lightly hopped and quite different from today's pale ales." +
"By the mid-eighteenth century, pale ale was mostly brewed with coke-fired malt, which produced less smoking and roasting of barley in the malting process, " +
"and hence produced a paler beer.");
desc.add("Usually top-fermented, which is brewed with a large proportion of wheat relative to the amount of malted barley. The two main varieties are Weißbier, based on " +
"the German tradition, and Witbier, based on the Belgian tradition; other types include Lambic (made with wild yeasts and bacteria), Berliner Weisse (a cloudy, sour beer), " +
"and Gose (a German-type sour, salty, herbal beer). ");
desc.add("Lager is a type of beer conditioned at low temperatures.[1] Lagers can be pale, amber, or dark. Pale lager is the most widely consumed and commercially available style of beer. " +
"Well-known brands include Pilsner Urquell, Molson Canadian, Miller, Stella Artois, Beck's, Brahma, Budweiser Budvar, Corona, Snow, Tsingtao, Singha, Kirin, Quilmes, Heineken, Kingfisher, " +
"Carlsberg, Birra Moretti and Tennents. ");
desc.add("Pale ale is a top-fermented beer made with predominantly pale malt.[1]"+
"The highest proportion of pale malts results in a lighter colour.[2][3] The term first appeared around 1703 for beers made from malts dried with high-carbon coke, which resulted in a lighter " +
"colour than other beers popular at that time. Different brewing practices and hop levels have resulted in a range of different tastes and strengths within the pale ale family.");
desc.add("It’s not a Full Fat IPA, nor is it a Pale Ale, nor a British-styled sessionable pale beer, instead it fills in the middle of a hoppy Venn diagram of those three beer types. Reduce " +
"the strength in an IPA, lose some of the maltier middle of a Pale Ale, load up the hops on a golden ale, dry it all out and give it a pokier bitterness and you’re in the Session IPA zone. " +
"Given their rate of growth and their now-ubiquitous presence in bars and bottles shops, we’re all in the Session IPA zone.");
desc.add("Clean personality of a lager with the hoppiness of an IPA.The basic idea behind a hoppy lager is pretty simple: a beer that mixes the best of a lager with the hoppy-quality often associated with " +
"something like an IPA. This creates a beer that is light, refreshing, and easy to drink like a lager, which makes it perfect for Texas, with a more distinctive and complex flavor thanks to a stronger " +
"hops presence. We find that our hoppy lager is easy to drink with a level of bitterness that is strong for a lager, but not quite as robust as an IPA.A good hoppy lager presents a middle way between a " +
"traditional lager and something with more bitterness to it. It drinks a little like each, but ultimately has a personality all its own.");
adapter.addData(titles, desc);
}
public void onNameClick(int position){
Intent intent = new Intent(this, Main2Activity.class);
intent.putExtra("Title", titles.get(position));
intent.putExtra("Description", desc.get(position));
startActivity(intent);
}
NameViewHolder
public class NameViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private ImageView ivCardImage;
private TextView tvCardTitle;
private TextView tvCardDescription;
private NameClickListener clickListener;
public NameViewHolder(@NonNull View itemView, NameClickListener listener) {
super(itemView);
ivCardImage = itemView.findViewById(R.id.iv_cardImage);
tvCardTitle = itemView.findViewById(R.id.tv_CardTitle);
tvCardDescription = itemView.findViewById(R.id.tv_CardDescription);
this.clickListener = listener;
itemView.setOnClickListener(this);
}
public void setTitle(String title){
tvCardTitle.setText(title);
}
public void setDescription(String description){
tvCardDescription.setText(description);
}
@Override
public void onClick(View view) {
clickListener.onNameClick(getAdapterPosition());
}
RecyclerAdapter
public class RecyclerAdapter extends RecyclerView.Adapter<NameViewHolder> {
private List<String> titleList = new ArrayList<>();
private List<String> descriptionList = new ArrayList<>();
private NameClickListener clickListener;
public RecyclerAdapter(NameClickListener listener){
this.clickListener = listener;
}
@NonNull
@Override
public NameViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View cellView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_item, parent, false);
return new NameViewHolder(cellView, clickListener);
}
@Override
public void onBindViewHolder(@NonNull NameViewHolder nameViewHolder, int position) {
nameViewHolder.setTitle(titleList.get(position));
nameViewHolder.setDescription(descriptionList.get(position));
}
@Override
public int getItemCount() {
return titleList.size();
}
public String fetchTitle(int position){
return this.titleList.get(position);
}
public String fetchDescription(int position){
return this.descriptionList.get(position);
}
public void addData(List<String> titles, List<String> descriptions){
this.titleList.clear();
this.descriptionList.clear();
this.titleList.addAll(titles);
this.descriptionList.addAll(descriptions);
notifyDataSetChanged();
}
Main2Activity
public class Main2Activity extends AppCompatActivity{
private String BeerName;
private String BeerDescription;
private TabLayout tabLayout;
private ViewPager viewpager;
private TabItem tabOne, tabTwo;
public PageAdapter pagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
BeerName = getIntent().getStringExtra("Title");
BeerDescription = getIntent().getStringExtra("Description");
setTitle(BeerName);
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
tabOne tabone = new tabOne();
setContentView(R.layout.activity_main2);
tabLayout = findViewById(R.id.tabLayout);
tabOne = findViewById(R.id.tab_Description);
tabTwo = findViewById(R.id.tab_Interesting);
viewpager = findViewById(R.id.viewPager);
pagerAdapter = new PageAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewpager.setAdapter(pagerAdapter);
Bundle bundle = new Bundle();
bundle.putString("Description", BeerDescription);
tabone.setArguments(bundle);
transaction.add(R.id.frameLayout1, tabone);
transaction.commit();
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener(){
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewpager.setCurrentItem(tab.getPosition());
if(tab.getPosition() == 0){
pagerAdapter.notifyDataSetChanged();
}
else if(tab.getPosition() == 1){
pagerAdapter.notifyDataSetChanged();
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
viewpager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
}
public String getBeerDescription(){
return this.BeerDescription;
}
Фрагмент TabOne
public class tabOne extends Fragment {
TextView tv_FragmentDescription;
String beerDesc;
public tabOne() {
// Required empty public constructor
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_tab_one, container, false);
tv_FragmentDescription = view.findViewById(R.id.tv_tabOne_Description);
Bundle bundle = getArguments();
beerDesc = bundle.getString("Description");
tv_FragmentDescription.setText(beerDesc);
return view;
}
Фрагмент TabTwo
public class tabTwo extends Fragment {
public tabTwo() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_tab_two, container, false);
}
PageAdapter
public class PageAdapter extends FragmentPagerAdapter {
private int numberOfTabs;
public PageAdapter(@NonNull FragmentManager fm, int numberoftabs) {
super(fm);
this.numberOfTabs = numberoftabs;
}
@NonNull
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new tabOne();
default:
return new tabTwo();
}
}
@Override
public int getCount() {
return numberOfTabs;
}
@Override
public int getItemPosition(@NonNull Object object) {
return POSITION_NONE;
}
NameClickListener
public interface NameClickListener {
void onNameClick(int position);
Спасибо!