2017-08-05 120 views
0

我试图通过片段做一个菜单应用程序在Android中错误:(26,24)错误:不兼容的类型:类不能转换到碎片

所有我送从一个延伸片段的类和这就是它要求我的数组片段,但仍然不是。

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

的问题是,你不能getFragmentManager()使用android.support.v4.app.Fragment空的一个。您需要改用getSupportFragmentManager()

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

+0

我想什么是有,但仍然没有工作,我会从错误图像,以便你无法检查 –

+0

确定我不能改变形象,但是!错误说的是“不兼容的类型”。必填项:adroid.app.Fragment –

+0

是的,你需要使用'android.app.Fragment'而不是'v4.Fragment' with'getFragmentManager()' –

相关问题