我试图从一个片段移动,即mycontacts extends Fragment到另一个片段friendfragment在点击按钮时扩展Fragment。我收到了No View found for Fragment
这样的错误。我的代码和错误日志如下。请指导我,因为我是Fragments的新手。
tasks.xml文件
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/tasky"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="@+id/fragment1"
android:name="com.example.taskmanager.friendfragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</FrameLayout>
mycontacts.class
Fragment fr;
fr=new friendfragment();
FragmentManager fm=getFragmentManager();
android.app.FragmentTransaction ft=fm.beginTransaction();
ft.replace(R.id.content_frame, fr);
ft.commit();
friendfragment.class
public class friendfragment extends Fragment {
public friendfragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tasks, container,
false);
return view;
}
}
tasks.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/tasky"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="@+id/fragment1"
android:name="com.example.taskmanager.friendfragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
一些错误日志
04-23 15:00:21.074: E/Trace(26708): error opening trace file: No such file or directory (2)
04-23 15:00:47.933: E/FragmentManager(26708): No view found for id 0x7f040051 (com.example.taskmanager:id/fragment1) for fragment friendfragment{40e187b8 #1 id=0x7f040051}
04-23 15:00:47.933: E/FragmentManager(26708): Activity state:
04-23 15:00:47.963: E/FragmentManager(26708): Local Activity 40cd4eb8 State:
04-23 15:00:47.973: E/FragmentManager(26708): mResumed=true mStopped=false mFinished=false
04-23 15:00:47.995: E/FragmentManager(26708): mLoadersStarted=true
04-23 15:00:47.995: E/FragmentManager(26708): mChangingConfigurations=false
04-23 15:00:48.003: E/FragmentManager(26708): mCurrentConfig={1.0 310mcc260mnc en_US ldltr sw320dp w320dp h455dp 160dpi nrml port finger qwerty/v/v -nav/h s.6}
答案 0 :(得分:1)
任务:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/frame_content"
android:layout_height="match_parent"
android:orientation="vertical" >
</FrameLayout>
fragment_friend.xml布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativewLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- Your views here -->
</RelativeLayout>
片段类:
public class friendfragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_friend, container,
false);
return view;
}
}
FragmentActivty:
Fragment fr = new friendfragment();
FragmentManager fm=getFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
ft.replace(R.id.fragment_content, fr).commit();