当用户点击ListView中的项目时,我试图在Android中显示一个新片段。这是我的代码:
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
final FragmentManager fm = getFragmentManager();
final FragmentTransaction ft = fm.beginTransaction();
DetailFragment df = new DetailFragment();
Toast.makeText(getActivity().getBaseContext(), "Clicked with R.id.list=" + R.id.list, Toast.LENGTH_LONG).show();
ft.replace( R.layout.fragment_favorites, df, "DetailFragment" ).addToBackStack(null).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE).commit();
}
当我运行此代码并点击列表视图中的项目时,我得到例外:
10-09 22:39:05.336: E/AndroidRuntime(25157): java.lang.IllegalArgumentException: No view found for id 0x7f030029 (com.example.proj1:layout/fragment_favorites) for fragment DetailFragment{42430970 #3 id=0x7f030029 DetailFragment}
答案 0 :(得分:0)
ft.replace(int containerViewId,Fragment fragment,String tag) 替换添加到容器的现有片段。 containerViewId是要替换其片段的容器的标识符。它不是布局。
检入你的活动布局xml,找到片段的id
替换
ft.replace(R.layout.fragment_favorites,df,&#34; DetailFragment&#34;)。addToBackStack(null).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE).commit();
与
ft.replace(&#34;您的片段ID&#34;,df,&#34; DetailFragment&#34;)。commit();