FragmentTransaction没有显示替换的片段

时间:2015-06-19 08:22:57

标签: android android-fragments fragmenttransaction

在主视图中使用另一个片段类的新实例替换my fragment容器后,似乎没有发生任何事情

 case 2:
        Log.d("Menu", "Sent clicked");
        try {
            android.support.v4.app.Fragment fragment = new SentFragment();
            FragmentTransaction fm = getSupportFragmentManager().beginTransaction();
            fm.replace(R.id.frameLayout, fragment);
            fm.commit();
            getSupportFragmentManager().executePendingTransactions();
        }catch(Exception e){
            e.printStackTrace();
        }
        break;

我的Logcat是这样的

    06-19 09:12:29.487  19458-19458/com.pacentltd.mail D/Menu﹕ Sent clicked
    06-19 09:12:29.487  19458-19458/com.pacentltd.mail D/Frag﹕ Attached

在片段上调用onAttach后自己

1 个答案:

答案 0 :(得分:1)

检查以下修改

case 2:
    Log.d("Menu", "Sent clicked");
    try {
        SentFragment fragment = new SentFragment(); // updated line
        FragmentTransaction fm = getSupportFragmentManager().beginTransaction();
        fm.replace(R.id.frameLayout, fragment);
        fm.commit();
        getSupportFragmentManager().executePendingTransactions();
    }catch(Exception e){
        e.printStackTrace();
    }
    break;

创建SentFragment的实例而不是android.support.v4.app.Fragment的实例。并确保您的SentFragment类扩展android.support.v4.app.Fragment