用Fragment替换AppCompatActivity,但不能访问旧类

时间:2017-01-11 10:48:22

标签: java android android-fragments fragment appcompatactivity

我尝试使用此video

创建BottomBar

但是,我有一个现有的项目,很少有类已经实现,当我尝试通过Fragment更改AppCompatActivity时,我的旧方法/类无法访问

老班的摘要:

public class ChoixFormulaire extends Fragment {


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView=inflater.inflate(R.layout.activity_choix_formulaire,container,false);

        return rootView;
    }

    public void formJ(View v){
        Intent i = new Intent(ChoixFormulaire.this,RechercheJournalier.class);
        startActivity(i);
    }
    public void formP(View v){
        Intent i = new Intent(ChoixFormulaire.this,FormulaireRecherche.class);
        startActivity(i);
    }
    public void retourF(View v){
        Intent i = new Intent(ChoixFormulaire.this,RechercheDuJour.class);
        startActivity(i);
    }

但是,这部分堡垒:

(ChoixFormulaire.this,RechercheJournalier.class);
(ChoixFormulaire.this,FormulaireRecherche.class);
(ChoixFormulaire.this,RechercheDuJour.class);

我有这个错误:

"Cannot resolve constructor 'Intent(com.appli.cci.ports2a.Recherches.ChoixFormulaire, java.lang.Class<com.appli.cci.ports2a.Recherches.RecherchesJournalier>)'

2 个答案:

答案 0 :(得分:0)

需要使用父活动或应用程序上下文创建意图。因此,您需要使用Fragment的父活动上下文

Intent intent = new Intent(getActivity(),MyClass.class);

你需要从第一个活动传递数组,如下所示: -

Bundle bundle=new Bundle();
bundle.putStringArray(key, new String[]{value1, value2});
Intent i=new Intent(context, Class);
i.putExtras(bundle);

您可以从其他活动中获取数组

Bundle bundle=this.getIntent().getExtras();
String[] array=bundle.getStringArray(key);

答案 1 :(得分:0)

你没有正确使用上下文检查这个。

 public void formJ(View v){
            Intent i = new Intent(getActivity(),RechercheJournalier.class);
            startActivity(i);
        }