0

我正在尝试从 asynctask 类中替换一个片段 onPostExecute 方法,但它不会替换任何东西。这是我的标签监听器类:

public class TabListener implements ActionBar.TabListener
{
    protected ParentActivity parent;
    public FragmentTransaction fragmentTransaction;
    public ItemData item = new ItemData();

    public TabListener(ParentActivity parent)
    {
        this.parent = parent;
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {

        this.fragmentTransaction = ft;

        if(this.parent.isNetworkAvailable())
        {
            //ft.replace(android.R.id.content, new LoadingFragment());

            if(tab.getPosition() == 0)
            {
                new HttpData().execute(ParentActivity.centroUri);

            }
            else
            {
                new HttpData().execute(ParentActivity.nvomxliUri);
            }
        }
        else
        {
            ft.replace(android.R.id.content, new NetworkFragment());
        }

    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }


    private class HttpData extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... urls) {
            Parser p = new Parser();
            p.run(urls[0], item);
            return "";
        }

        @Override
        protected void onPostExecute(String result) {
            fragmentTransaction.replace(android.R.id.content, new LaneFragment(item));
        }

    }
}

我确认执行了 onPostExecute 方法并按预期检索了所需的数据。

谢谢你的帮助。

4

1 回答 1

0

您是否尝试在 PostExecute 中创建新的片段事务?您正在使用 onTabSelected 发送给您的相同事务,但我认为该事务在该方法结束时已提交。

    FragmentTransaction fragmentTransaction = mFragmentMgr.beginTransaction();

    // Add fragment to the container ContentView 
    fragmentTransaction.replace(R.id.fragment, tag);

    fragmentTransaction.addToBackStack(null);

    // Commit transaction
    fragmentTransaction.commit();
于 2013-01-10T20:29:41.680 回答