Android Studio“尚未附加片段”
Posted
技术标签:
【中文标题】Android Studio“尚未附加片段”【英文标题】:Android Studio "Fragment has not been attached yet" 【发布时间】:2020-08-27 06:29:16 【问题描述】:Process: beermap, PID: 9969
java.lang.RuntimeException: Unable to start activity ComponentInfobeermap/beermap.Main2Activity: java.lang.IllegalStateException: Fragment tabOnee379756 (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 tabOnee379756 (ba34244e-5562-4ef1-86c6-2cfa9113fc92) has not been attached yet.
所以这是 logcat...
在此错误之前,我收到“java.lang.RuntimeException: Unable to start activity ComponentInfobeermap/beermap.Main2Activity: java.lang.IllegalArgumentException: No view found for id 0x7f08006e (beermap:id/frameLayout1) ”
以下是类
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(片段)
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);
布局/activity_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_
android:layout_
android:orientation="vertical"
tools:context=".Main2Activity">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_
android:layout_>
<com.google.android.material.tabs.TabItem
android:id="@+id/tab_Description"
android:layout_
android:layout_
android:text="Description" />
<com.google.android.material.tabs.TabItem
android:id="@+id/tab_Interesting"
android:layout_
android:layout_
android:text="Interesting" />
</com.google.android.material.tabs.TabLayout>
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewPager"
android:layout_
android:layout_>
</androidx.viewpager.widget.ViewPager>
有谁知道问题出在哪里?
【问题讨论】:
嗯,问题可能比我一开始想的要多。删除从tabOne tabone = new tabOne();
到tabone.setArguments(bundle);
的所有八行怎么样?我怀疑它是必要的。
好吧,我试图将字符串从 Main2Activity 传递到 tabOne 片段...应用程序在没有这些行的情况下工作,但我需要 tabOne 片段中的那个字符串。 @hata
我重写了我的答案。请尝试。
它正在工作。太感谢了! @hata
【参考方案1】:
如果你删除这六行后你的应用运行没有问题:
tabOne tabone = new tabOne();
FragmentManager manager = tabone.getChildFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
Bundle bundle = new Bundle();
bundle.putString("Description", BeerDescription);
tabone.setArguments(bundle);
而您只想传递参数字符串"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();
除此之外,您必须将字符串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;
然后,最后你可以在 Main2Activity 中将 BeerDescripion
作为 PageAdapter 的构造函数参数传递:
@Override
protected void onCreate(Bundle savedInstanceState)
(...)
pagerAdapter = new PageAdapter(getSupportFragmentManager(), tabLayout.getTabCount(), BeerDescription);
(...)
【讨论】:
在 Main2Activity 中? @hata以上是关于Android Studio“尚未附加片段”的主要内容,如果未能解决你的问题,请参考以下文章
Android studio调试时遇见Install Repository and sync project的问题
Android studio2.2 ndk 错误 :format not a string literal and no format arguments!
Android Studio升级到3.1.4后打开旧项目警告:The `android.dexOptions.incremental` property is deprecated and it ha
Android studio导入项目时的问题(Re-download dependencies and sync project (requires network))
错误记录Android Studio 编译报错 ( kotlin library {0} was compiled with a newer kotlin compiler and not b )
Android Studio 虚拟机运行错误(The ADB binary found at XX is obsolete and ...)索性换成真机调试