Process: beermap, PID: 9969
java.lang.RuntimeException: Unable to start activity ComponentInfo{beermap/beermap.Main2Activity}: java.lang.IllegalStateException: Fragment tabOne{e379756 (ba34244e-5562-4ef1-86c6-2cfa9113fc92)} has not been attached yet.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.IllegalStateException: Fragment tabOne{e379756 (ba34244e-5562-4ef1-86c6-2cfa9113fc92)} has not been attached yet.
所以这是logcat...
在这个错误之前,我得到了"java.lang.RuntimeException: 无法启动活动 ComponentInfo{beermapbeermap.Main2Activity}: java.lang.IllegalArgumentException: 没有为id 0x7f08006e (beermap:idframeLayout1)找到视图。"
以下是班级
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);
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);
tabOne tabone = new tabOne();
FragmentManager manager = tabone.getChildFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
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;
}
页面适配器
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;
}
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 (Fragment)
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);
}
layoutactivity_main2.xml。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Main2Activity">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.tabs.TabItem
android:id="@+id/tab_Description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Description" />
<com.google.android.material.tabs.TabItem
android:id="@+id/tab_Interesting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Interesting" />
</com.google.android.material.tabs.TabLayout>
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.viewpager.widget.ViewPager>
有谁知道问题出在哪里?
如果你删除这六行,你的应用运行没有问题。
tabOne tabone = new tabOne();
FragmentManager manager = tabone.getChildFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
Bundle bundle = new Bundle();
bundle.putString("Description", BeerDescription);
tabone.setArguments(bundle);
而你只想传递参数String "Description"
,最好不要从 Main2Activity
但从 PageAdapter
.
修改PageAdapter的 getItem
并从Main2Activity中删除以上六行)。
public Fragment getItem(int position) {
switch (position){
case 0:
tabOne tabone = new tabOne();
Bundle bundle = new Bundle();
bundle.putString("Description", BeerDescription);
tabone.setArguments(bundle);
return tabone;
default:
return new tabTwo();
}
}
除此之外,你还必须传入 String BeerDescription
到Main2Activity的PageAdapter。要做到这一点,你可以修改PageAdapter的构造函数并添加一个成员变量 BeerDescription
还有
private int numberOfTabs;
private String BeerDescription;
public PageAdapter(@NonNull FragmentManager fm, int numberoftabs, String BeerDescription) {
super(fm);
this.numberOfTabs = numberoftabs;
this.BeerDescription = BeerDescription;
}
然后,你终于可以通过 BeerDescripion
作为Main2Activity中PageAdapter的构造函数参数。
@Override
protected void onCreate(Bundle savedInstanceState) {
(...)
pagerAdapter = new PageAdapter(getSupportFragmentManager(), tabLayout.getTabCount(), BeerDescription);
(...)
}