如何获取动态片段的视图?

问题描述:

我创建了动态片段,我想获取片段的视图。 这是我的片段:如何获取动态片段的视图?

<?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

问题在这里: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

超喜欢..... –