无法通过标签找到片段

时间:2015-04-21 17:38:58

标签: android android-fragments

这是我的代码......我无法通过标签找到片段

 transaction = getFragmentManager();
        transaction.beginTransaction()
                .replace(R.id.container,tachoFrag,"m1");
        transaction.beginTransaction()
                .replace(R.id.container,overviewFrag,"m2");
        transaction.beginTransaction()
                .replace(R.id.container,dataLogFrag,"m3")
                .commit();

        tachoFrag = (menu1_Fragment) getFragmentManager().findFragmentByTag("m1");
        overviewFrag = (menu2_Fragment) getFragmentManager().findFragmentByTag("m2");
        dataLogFrag = (menu3_Fragment) getFragmentManager().findFragmentByTag("m3");


        if (D) Log.i(TAG, "run() -> LogicAnalizerView - transaction.replace(R.id.container, tachoFrag)");
        if (tachoFrag != null && tachoFrag.isVisible())
        {
            tachoFrag.setRevs(revs);
            tachoFrag.setSpeed(speed);
            tachoFrag.setFuelGauge(fuel);
            if(engTemp>60)
            {
                tachoFrag.setEngTemp(engTemp);
            }
            else
            {
                engTemp = 60;
                tachoFrag.setEngTemp(engTemp);
            }
            tachoFrag.setMileage(mileage);
            tachoFrag.setAmbientTemp(ambientTemp);
            tachoFrag.setGear(gear);

            transaction.beginTransaction().replace(R.id.container, tachoFrag,"m1");
            transaction.beginTransaction().commit();
       }

       ...

        if (D) Log.i(TAG, "run() -> LogicAnalizerView - transaction.replace(R.id.container, dataLogFrag)");

            dataLogFrag.setData(time, speed, revs, gear);

            transaction.beginTransaction().replace(R.id.container, dataLogFrag,"Data logger");
            transaction.beginTransaction().commit();

当我添加替换事务以添加标记时,overviewfrag的第二个替换会导致空指针问题...请帮助

1 个答案:

答案 0 :(得分:0)

transaction.beginTransaction()
                .replace(R.id.container,tachoFrag,"m1").commit(
        transaction.beginTransaction()
                .replace(R.id.container,overviewFrag,"m2").commit();

您不会通过第一个片段提交更改。