0

현재 진행중인 작업 : 브로드 캐스트 수신기에서 UI (Fragment UI)를 업데이트 중입니다. 그러나 단편을 발견하는 동안 보답으로 null이 나옵니다.getSupportFragmentManager(). null을 반환하는 findFragmentById

OnReceive (방송 수신기) :

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

MainActivity.class (조각 작업) :

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; 
: Activity에서 조각을 호출하기위한이

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

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

코드

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

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

+0

그래, 그게 문제가 아니야 그냥 –

답변

0

조각 트랜잭션 후 getSupportFragmentManager(). executePendingTransactions()를 사용하십시오. 이 메서드를 호출하면 findFragmentById() 및 findFragmentByTag() 메서드를 사용하여 조각을 가져올 수 있습니다.

또한 유효 ID 또는 태그를 전달해야합니다.

관련 문제