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

时间:2015-11-01 17:48:56

标签: android android-fragments

我创建了动态片段,我希望获得片段视图。 这是我的片段:

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

和我的活动布局片段:

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

然后在“onCreate”方法中,我尝试获取片段

的“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");

但是我得到了“Null Pointer Exception”,我的错误在哪里?

1 个答案:

答案 0 :(得分:1)

问题在于:ft.commit();

来自commit()的{​​{3}}:

  

计划此交易的提交。提交不会发生   立即;它将被安排为主线程上的工作   下次线程准备就绪时完成。

因为你打电话给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();
            }
     }
}

当然,只有当您想要从TextView访问Fragment的{​​{1}}时,才需要执行此操作。如果您想要访问片段,请在Activity的{​​{1}}方法中执行findViewById()