2015-11-01 47 views
1

我創建了動態片段,我想獲取片段的視圖。 這是我的片段:如何獲取動態片段的視圖?

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:id="@+id/fragmenttext" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

</LinearLayout> 

類片段:

package webviewtest.webviewtest; 


import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 


public class MyFragment extends Fragment { 
    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View myFragmentView = inflater.inflate(R.layout.my_fragment, container, false); 
     return myFragmentView; 
    } 
} 

與片段我的活動佈局:在 「的onCreate」

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity"> 


    <FrameLayout 
     android:id="@+id/myfragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</FrameLayout> 

然後方法我試圖獲得 「TextView的」片段

FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
ft.add(R.id.myfragment, new MyFragment()); 
ft.commit(); 

TextView textFragment = (TextView) findViewById(R.id.fragmenttext); 
textFragment.setText("Some text"); 

但我得到了「空指針異常」,我的錯誤在哪裏?

+0

嘗試myFragmentView.findViewById在你的片段類 – PatrickMA

回答

1

問題在這裏:ft.commit();

commit()documentation

時間表提交本次交易的。立即不會發生提交 ;它將被安排在主線程上工作,在下一次線程準備就緒時完成 。

所以,僅僅因爲你叫commit(),並不意味着該片段是在活動中充氣,即,onCreateView()不必立即調用。

那麼如何處理呢?

那麼,你需要通過Interface來處理。創建一個界面,並讓你的Activity實現它。然後在您的片段中的onActivityCreated()方法中調用接口中的方法。在該方法中,您將在TextView上執行findViewById()。我已經制定了如何實現這一目標的框架。

public interface IFragmentListener{ 
     void fragmentInitialized(); 
    } 


public MainActivity extends Activity implements IFragmentListener{ 

     public void onCreate(Bundle savedInstanceState){ 
      FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
      MyFragment myFragment = new MyFragment(); 
      myFragment.setFragmentListener(this); 
      ft.add(R.id.myfragment, myFragment); 
      ft.commit(); 
     } 

     public void fragmentInitialized(){ 
      TextView textFragment = (TextView) findViewById(R.id.fragmenttext); 
      textFragment.setText("Some text"); 
     } 
} 


public class MyFragment extends Fragment { 

    IFragmentListener listener; 

    public void setFragmentListener(IFragmentListener listener){ 
      this.listener = listener; 
    } 

    public void onActivityCreated(Bundle savedInstanceState) { 
      super.onActivityCreated(savedInstanceState); 
      if(listener != null){ 
        listener.fragmentInitialized(); 
      } 
    } 
} 

當然,你需要做的這一切,只是如果你想從你的Activity訪問FragmentTextView。如果你想訪問如果從你的片段,然後只需findViewById()onActivityCreated()方法Fragment

+0

太棒了!你最好! – TheMrbikus

+0

超喜歡..... –