相关疑难解决方法(0)

片段添加或替换不起作用

我正在使用此参考中的代码.

当我在我的程序中输入该代码时,我得到一个错误,如下图所示. 在此输入图像描述

出错的原因是什么? The method replace(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, ExampleFragments)

我的主要活动代码:

public void red(View view) {
        android.app.FragmentManager fragmentManager = getFragmentManager();
                android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        ExampleFragments fragment = new ExampleFragments();
        fragmentTransaction.replace(R.id.frag, fragment);
        fragmentTransaction.commit();
    }
Run Code Online (Sandbox Code Playgroud)

ExampleFragments.java

package com.example.learn.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ExampleFragments extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this …
Run Code Online (Sandbox Code Playgroud)

java android android-fragments

57
推荐指数
2
解决办法
8万
查看次数

我可以删除layout.xml文件中定义的片段吗?

是否可以使用FragmentTransaction和remove()方法来摆脱layout.xml中定义的片段(使用片段标记)?

我没有使用支持库v4来使用它.在调用remove()之后,在提交FragmentTransaction之后片段保持不变.谁能告诉我这是设计,错误还是功能?

可以替换lyaout.xml中定义的片段,所以我觉得有点奇怪的是它不应该删除它?

android android-fragments android-3.0-honeycomb

12
推荐指数
1
解决办法
4460
查看次数