2013-06-02 43 views
0

我想讓我的應用程序從fragmentactivity中的我的片段類中調用函數。不過,我發現很多問題只是找到片段。現在我可以通過id找到一個片段,我認爲它是託管標籤的片段。但是當我得到子片段管理器時,我返回null。誰能幫忙?findFragmentByTag返回null

這是片段活性

package com.example.profileactivity; 

import android.os.Bundle; 
import android.app.Activity; 
import android.util.Log; 
import android.view.Menu; 
import android.view.View; 
import android.widget.TabHost.TabSpec; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentTabHost; 

public class ProfileActivity extends FragmentActivity { 

    // Fragment TabHost as mTabHost 
    private FragmentTabHost mTabHost; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_profile); 
     mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost); 

     mTabHost.setup(this,getSupportFragmentManager(), R.id.realtabcontent); 

     mTabHost.addTab(mTabHost.newTabSpec("user").setIndicator("User", 
       getResources().getDrawable(R.drawable.ic_user_tab)), 
       UserProfileTab.class,null); 

     mTabHost.addTab(mTabHost.newTabSpec("payment").setIndicator("Payment", 
       getResources().getDrawable(R.drawable.ic_payment_tab)), 
       PaymentInfoTab.class,null); 


    } 

    public void buttonClick(View view){ 
     FragmentManager fm = getSupportFragmentManager(); 
     if(fm.findFragmentById(R.id.realtabcontent) == null){ 
      Log.d("TABHOST", "didnt find fragment"); 
     } 
     else{ 
      Log.d("TABHOST", "found fragment"); 
      //PaymentInfoTab paymentTab = (PaymentInfoTab)fm.findFragmentByTag("payment"); 
      //paymentTab.boom(); 
      if(fm.findFragmentById(R.id.realtabcontent).getChildFragmentManager().findFragmentByTag("payment") != null){ 
       PaymentInfoTab paymentTab = (PaymentInfoTab)fm 
         .findFragmentById(R.id.realtabcontent) 
         .getChildFragmentManager() 
         .findFragmentByTag("payment"); 
       paymentTab.boom(); 
      } 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.profile, menu); 
     return true; 
    } 

} 

這是fragmentactivity的XML。

<android.support.v4.app.FragmentTabHost 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <TabWidget 
      android:id="@android:id/tabs" 

      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="0"/> 

     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="0dp" 
      android:layout_height="0dp" 
      android:layout_weight="0"/> 

     <FrameLayout 
      android:id="@+id/realtabcontent" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1"/> 

    </LinearLayout> 
</android.support.v4.app.FragmentTabHost> 
+0

簡單的答案是,該片段實例不能由FragmentManager定位。可能是因爲託管此片段的活動已被停止或銷燬。 –

回答

0

嘗試直接在活動的FragmentManager上使用標記。

您的示例的完整參考似乎是this。按照這個例子的更多細節,你會看到作者跟蹤標籤及其標籤。

+0

我有。我只能找到真正的標籤內容的ID。我也嘗試使用getFragmentManager而不是getChildFragmentManager,兩者都不成功。 – ctng1213

0

我有類似的問題,在總是返回null的標籤更改之後獲取片段。發佈Runnable接口TabHost消息隊列的末尾沒有訣竅:

tabHost.post(new Runnable() { 
     @Override 
     public void run() { 
      Fragment fragment = (Fragment) getChildFragmentManager().findFragmentByTag(tag); 
      fragment.updateList(newList); 
     } 
    });