我在做什么:从广播接收器更新UI(片段UI)。但是在找到片段时我得到了无效的回报。
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无效。
答案 0 :(得分:0)
片段事务后使用getSupportFragmentManager()。executePendingTransactions()。调用此方法后,您可以使用findFragmentById()和findFragmentByTag()方法获取片段。
还要确保为片段传递有效ID或标记。