getSupportFragmentManager().findFragmentById返回Null [英] getSupportFragmentManager().findFragmentById returning Null
问题描述
我在做什么:从广播接收器更新用户界面(片段用户界面).但是当找到Fragment时,我得到的是空值.
What I am doing: updating UI (Fragment UI) from Broadcast Receiver. But while finding Fragment I am getting null in return.
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")
Frag为空.
The fragment (SlidingTab) consist of textviews and viewpager . And i
am not able to find it in MainActivity.class. Every time i call
SlidingTab frag =
(SlidingTab)getSupportFragmentManager().findFragmentByTag("slidingtab")
Frag comes null.
推荐答案
在片段事务之后使用getSupportFragmentManager().executePendingTransactions().调用此方法后,您可以同时使用findFragmentById()和findFragmentByTag()方法来获取片段.
Use getSupportFragmentManager().executePendingTransactions() after fragment transaction. After calling this method you can get the fragment using both findFragmentById() and findFragmentByTag() methods.
还要确保为片段传递有效ID或标签.
Also make sure you pass Valid Id or Tag for the fragment.
这篇关于getSupportFragmentManager().findFragmentById返回Null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!