片段导入问题 FragmentTransaction.replace

Fragment Import problems messing with FragmentTransaction.replace

这里有一个有趣的问题。我有 2 个相同的 Fragments,它们都 import android.support.v4.app.Fragment 但是当我尝试对它们执行 FragmentTransaction.replace() 时,编译器接受 MyFrag2 但在 MyFrag1 上给出错误。

我知道是什么原因造成的,但我不知道为什么。如果我将 MyFrag1 中的导入从 android.support.v4.app.Fragment 更改为 android.app.Fragment,错误就会消失。如果我在两个 类 上导入 android.app.Fragment,错误翻转,我在 MyFrag2 上得到错误。片段 类 在片段设置方面是相同的。给出了什么?

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;

public class SettingsFragmentActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings_fragmentactivity);

        if(findViewById(R.id.fragment_container) != null) {
            if(savedInstanceState != null) return;

            MyFrag1 frag = new MyFrag1();

            android.app.FragmentTransaction transaction;
            transaction = getFragmentManager().beginTransaction();



//ERROR ON THIS LINE
//"replace (int, android.app.Fragment) to (int, MyFrag1)"
                transaction.replace(R.id.fragment_container, frag)


            transaction.addToBackStack(null);
            transaction.commit();
        }
    }

    public void someOtherMethod(long id) {
        MyFrag2 frag = new MyFrag2();
        Bundle args = new Bundle();
        args.putLong("id", id);
        frag.setArguments(args);

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();


//NO ERROR!? The compiler happily accepts my offering.
            transaction.replace(R.id.fragment_container, frag);


        transaction.addToBackStack(null);
        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        transaction.commit();
    }
}

MyFrag1

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;


public class MyFrag1 extends Fragment {
    private ListView listView;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        ...
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.frag1_layout, container, false);

        ...//setup listadapter etc.

        return view;
    }

    @Override
    public void onCreate(Bundle savedState) {
        super.onCreate(savedState);
        //setContentView(R.layout.settings_devicelist);
    }

    ...

}

MyFrag2

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;

public class MyFrag2 extends Fragment {
    ListView listView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.frag2_layout, container, false);

    ...//setup listadapter etc.

        return view;
    }

    @Override
    public void onCreate(Bundle savedState) {
        super.onCreate(savedState);
        //setContentView(R.layout.settings_nodedmxdevice);
    }

    ...

}

根据 FragmentActivity documentation:

When using this class as opposed to new platform's built-in fragment and loader support, you must use the getSupportFragmentManager() and getSupportLoaderManager() methods respectively to access those features.

所以用 getSupportFragmentManager() 替换你的 getFragmentManager() 并确保你所有的导入(例如,FragmentTransaction)指向支持库版本。