我在android系統是新的。 我想用代碼替換一個片段。這裏是我的代碼。 MainActivity:錯誤充氣類片段,當我更換片段
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setDefaultFragement();
}
private void setDefaultFragement() {
ContentFragment fragment = new ContentFragment();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.fra_content,fragment).commit();
}
}
這裏是我的ContentFragment
public class ContentFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_content, container, false);
}
}
mylayout:
<fragment
android:id="@+id/fra_content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/fra_title"
/>
但是我得到這些錯誤
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.administrator.myapplication/com.example.administrator.myapplication.MainActivity}: android.view.InflateException: Binary XML file line #13: Error inflating class fragmet
非常感謝。我更改了我的代碼,並且工作正常。 –