片段添加或更换不工作 [英] Fragment add or replace not working

查看:118
本文介绍了片段添加或更换不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是从这个参考code。

当我把在code在我的节目,我得到一个错误,如下面的图片看到。

有关错误的任何原因? 的方法的类型FragmentTransaction替换(INT,片段)是不适用的参数(INT,ExampleFragments)

这是我的主要活动code:

 公共无效红色(查看视图){
        android.app.FragmentManager fragmentManager = getFragmentManager();
                android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        ExampleFragments片段=新ExampleFragments();
        fragmentTransaction.replace(R.id.frag,片段);
        fragmentTransaction.commit();
    }
 

ExampleFragments.java

 包com.example.learn.fragments;

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

公共类ExampleFragments扩展片段{
    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
                             捆绑savedInstanceState){
        //充气的布局该片段
        返回inflater.inflate(R.layout.blue_pill_frag,集装箱,假);
    }
}
 

下面:

 包com.example.learn.fragments;

进口android.app.Activity;
进口android.os.Bundle;
进口android.support.v4.app.Fragment;
进口android.support.v4.app.FragmentManager;
进口android.support.v4.app.FragmentTransaction;
进口android.view.LayoutInflater;
进口android.view.Menu;
进口android.view.View;
进口android.view.ViewGroup;
 

解决方案

这里的问题是,你混 android.support.v4.app.Fragment android.app.Fragment 。你需要将所有的用途使用支持库,这也意味着调用 getSupportFragmentManager()

这样的事情,例如:

  android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
    android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    ExampleFragments片段=新ExampleFragments();
    fragmentTransaction.replace(R.id.frag,片段);
    fragmentTransaction.commit();
 

要注意的是,支持库片段和正常片段是不能互换是非常重要的。它们达到相同的目的,但它们不能被替换,彼此在code

I'm using the code from this reference.

When I put in that code in my program I get an error as seen in the picture below.

Any reasons for the error? The method replace(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, ExampleFragments)

Code from my main activity:

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();
    }

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 fragment
        return inflater.inflate(R.layout.blue_pill_frag, container, false);
    }
}

Here:

package com.example.learn.fragments;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;

解决方案

The problem here is that you're mixing android.support.v4.app.Fragment and android.app.Fragment. You need to convert all uses to use the support library, which also means calling getSupportFragmentManager().

Something like this, for example:

    android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
    android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    ExampleFragments fragment = new ExampleFragments();
    fragmentTransaction.replace(R.id.frag, fragment);
    fragmentTransaction.commit();

It is important to note that the support library Fragment and the normal Fragment are NOT interchangeable. They achieve the same purpose, but they cannot be replaced with one another in code.

这篇关于片段添加或更换不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆