【问题标题】:Replace AppCompatActivity by Fragment, but not access old class用 Fragment 替换 AppCompatActivity,但不访问旧类
【发布时间】:2017-01-11 10:48:22
【问题描述】:

我尝试用这个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>)'

【问题讨论】:

  • 您可以使用活动上下文来打开新活动。

标签: java android android-fragments fragment appcompatactivity


【解决方案1】:

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

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

【讨论】:

  • 这项工作很棒!但是,在我现在的另一个类中: spinnerArray2 = mA.getSpinnerArray(); Csa = new CustomSpinnerAdapter(this, R.layout.content_spinner, spinnerArray2); spin2.setAdapter(Csa); idDefaultStr = mA.getIdSpin(); idDefault = Integer.parseInt(idDefaultStr)-1;我有一个错误:-mA.getSpinnerArray(); - mA.getiDSpin() 你知道为什么吗?
【解决方案2】:

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

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);

【讨论】:

  • 这项工作很棒!但是,在我现在的另一个类中: spinnerArray2 = mA.getSpinnerArray(); Csa = new CustomSpinnerAdapter(this, R.layout.content_spinner, spinnerArray2); spin2.setAdapter(Csa); idDefaultStr = mA.getIdSpin(); idDefault = Integer.parseInt(idDefaultStr)-1;我有一个错误:-mA.getSpinnerArray(); - mA.getiDSpin() 你知道为什么吗?
  • 毫安是多少?
  • 如果您在主要活动中定义了数组列表并尝试进入其他活动,这不是获取它的正确方法。
猜你喜欢
  • 1970-01-01
  • 2016-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-07
  • 2012-12-31
相关资源
最近更新 更多