错误:(26,24)错误:不兼容的类型:类无法转换为片段

时间:2017-08-05 18:52:42

标签: android arrays android-fragments

我正在尝试通过片段在Android中执行菜单应用

我发送的所有类都是从片段中扩展而来的,这就是它要求我但仍然说不是的数组片段。

enter image description here

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class  Actividades extends AppCompatActivity implements InterfaceMenu {

    //Array para menu y fragments

    Fragment[] array_frag;

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

        //Obtener del menu el boton pulsado
        Bundle extras=getIntent().getExtras();
        menu(extras.getInt("BotonPulsado"));


        array_frag = new Fragment[3];
        array_frag[0]= new Lintern();
        array_frag[1]= new Nivel();
        array_frag[2]= new Service();
    }


    @Override
    public void menu(int frag_boton) {

        //obtener fragmentmanager y empezar transaccion
        FragmentManager mimanager = getFragmentManager();
        FragmentTransaction mitransaction = mimanager.beginTransaction();
        //mandamos el id de donde se van a
        // cargar los fragments y que fragment vamos a mandar
        mitransaction.replace(R.id.actividades,array_frag[frag_boton]);
        mitransaction.commit();
    }
}

这是我的一个课程,他们都是空的,就像这样

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


/**
 * A simple {@link Fragment} subclass.
 */
public class Lintern extends Fragment {


    public Lintern() {
        // Required empty public constructor
    }


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

}

1 个答案:

答案 0 :(得分:1)

问题是,您无法将android.support.v4.app.FragmentgetFragmentManager()一起使用。您需要使用getSupportFragmentManager()代替。

链接:https://stackoverflow.com/a/20237647/3758972

相关问题