0

我在做什麼:從廣播接收器更新UI(片段UI)。但是,當找到片段時,我會得到空值。getSupportFragmentManager()。findFragmentById returns Null

的onReceive(廣播接收器):

if(MainActivity.getInstace()!=null){ 
       MainActivity.getInstace().updateUI(); 
      } 

MainActivity.class(片段活性):

 public void updateUI() { 
      MainActivity.this.runOnUiThread(new Runnable() { 
       public void run() { 

//getting null here 
        SlidingTab frag = (SlidingTab)getSupportFragmentManager().findFragmentByTag("slidingtab"); 
        frag.updateUI(); 
       } 
      }); 
     } 

代碼用於從活動呼叫片段:

FragmentManager fragmentManager = getSupportFragmentManager(); 
       FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
       fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE); 


       ft.setCustomAnimations(R.anim.glide_fragment_horizontal_in, R.anim.glide_fragment_horizontal_out); 
       ft.replace(R.id.content_frame1, new SlidingTab(), "slidingtab"); 
       ft.addToBackStack("slidingtab"); 
       // Start the animated transition. 
       ft.commit(); 
       break; 

SlidingTab.class(片段UI更新代碼):

public class SlidingTab extends Fragment { 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.sliding_tab, container, false);} 
    public void updateUI(){ 

       badge.setVisibility(View.VISIBLE); 
    } 
      } 

片段(SlidingTab.xml)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/frame_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@android:color/white" 
    android:orientation="vertical" 
    tools:context="com.RareMediaCompany.BDTrial.SlidingTab"> 

    <include 
     android:id="@+id/toolbar1" 
     layout="@layout/toolbar_job" /> 

    <com.RareMediaCompany.BDTrial.Utils.CustomTabLayout 
     android:id="@+id/sliding_tabs" 
     style="@style/CustomTabLayout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#eeeeee" 
     app:tabIndicatorColor="#f39220" 
     app:tabIndicatorHeight="3dp" 
     app:tabMaxWidth="0dp" 
     app:tabMode="fixed" 
     app:tabPaddingEnd="0dp" 
     app:tabPaddingStart="0dp" 
     app:tabTextAppearance="@android:style/TextAppearance.Widget.TabWidget" 
     app:tabSelectedTextColor="#808080" /> 

    <LinearLayout 
     android:id="@+id/linear1" 
     android:background="@android:color/white" 
     android:layout_width="match_parent" 
     android:layout_height="70dp" 
     android:layout_marginRight="8dp" 
     android:layout_marginLeft="5dp" 
     android:orientation="horizontal" 
     android:weightSum="1"> 

     <android.support.v7.widget.SearchView 
     android:layout_width="300dp" 
     android:layout_height="45dp" 
     android:id="@+id/searchView" 
     android:layout_weight="1" 
     android:layout_gravity="center" 
      android:layout_marginEnd="5dp" 
      android:clickable="true" 
     style="@style/CitySearchView" 
     android:background="@drawable/searchview" 
     android:layout_marginLeft="5dp"/> 

     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:weightSum="1" 
      android:orientation="horizontal"> 

     <LinearLayout 
      android:id="@+id/list_linearlayout" 
      android:layout_width="45dp" 
      android:layout_height="45dp" 
      android:layout_weight="0.5" 
      android:layout_gravity="center" 
      android:background="#f39220"> 
     <!--android:background="#75aadb">--> 
      <ImageView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="2dp" 
       android:layout_weight="0.3" 
       android:src="@drawable/listicon"/> 

     </LinearLayout> 

     <LinearLayout 
      android:id="@+id/maplist" 
      android:layout_width="45dp" 
      android:layout_height="45dp" 
      android:layout_gravity="center" 
      android:layout_weight="0.5" 
      android:background="#75aadb"> 
      <ImageView 
       android:layout_width="20dp" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="2dp" 
       android:layout_marginRight="3dp" 
       android:layout_weight="0.3" 
       android:src="@drawable/map_icon_1"/> 

     </LinearLayout> 
     </LinearLayout> 

    </LinearLayout> 
     <!--android:layout_width="320dp"--> 
     <!--android:layout_height="wrap_content"--> 
     <!--android:layout_marginLeft="10dp"--> 
     <!--android:layout_marginTop="10dp"--> 
     <!--android:id="@+id/searchview"/>--> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/view_pager" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:background="@android:color/white" /> 

</LinearLayout> 

片段(SlidingTab)由textviews和viewpager的。我在MainActivity.class中找不到它。每當我打電話給
SlidingTab frag = (SlidingTab)getSupportFragmentManager().findFragmentByTag("slidingtab") 碎片變爲空。

+0

你使用什麼導入片段? –

+0

@DennisvanOpstal import android.support.v4.app.Fragment; –

+0

好吧,這不是問題,只是想確保 –

回答

0

在片段事務之後使用getSupportFragmentManager()。executePendingTransactions()。調用此方法後,您可以使用findFragmentById()和findFragmentByTag()方法獲取片段。

另外請確保您傳遞有效標識或片段。

相關問題