片段导入问题与FragmentTransaction.replace混乱

时间:2015-02-05 17:46:50

标签: android android-fragments

这里有趣的问题。我有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);
    }

    ...

}

1 个答案:

答案 0 :(得分:0)

根据FragmentActivity documentation

  

使用此类而不是新平台的内置片段和加载程序支持时,您必须分别使用getSupportFragmentManager()getSupportLoaderManager()方法来访问这些功能。

请将getFragmentManager()替换为getSupportFragmentManager()并确保所有导入(例如FragmentTransaction)指向支持库版本。