访问片段组件

时间:2017-08-05 13:24:07

标签: android android-fragments

我在android中创建了选项卡式活动应用程序。我想在应用程序启动或按下按钮时更改textview的文本。 (Button在Tab1Fragment中,TextView在Tab2Fragment中。)我创建了一个接口并在MainActivity中实现了这个接口。但当我点击按钮时,应用程序关闭。这是我的代码

MainActivity(响应是Communicator的方法)

@Override
public void respond(String s) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    Tab2 testFragment = (Tab2)fragmentManager.findFragmentById(R.id.tab2Fragment);
    testFragment.update(s);
}

TAB1

Communicator comm;
private static int counter;
public Tab1() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_tab1, container, false);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    comm = (Communicator)getActivity();
    Button button = (Button)getActivity().findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            comm.respond("" + counter);
        }
    });
}

TAB2

public class Tab2 extends Fragment {
TextView textView;
public Tab2() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_tab2, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    textView = (TextView)getActivity().findViewById(R.id.textView);
}
public void update(String s){
    textView.setText(s);
}

}

activity_main

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.oguz.faircontrol_v11.MainActivity">
    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/appbar_padding_top"
        android:theme="@style/AppTheme.AppBarOverlay">
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/AppTheme.PopupOverlay">
        </android.support.v7.widget.Toolbar>
        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </android.support.design.widget.AppBarLayout>
    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>

fragment_tab1

    <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="com.oguz.faircontrol_v11.Tab1">
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        android:text="Tikla"
        android:gravity="center"/>
</FrameLayout>

fragment_tab2

    <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="com.oguz.faircontrol_v11.Tab2"
    android:id="@+id/tab2Fragment">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Test"
        android:id="@+id/textView"
        android:textSize="30dp"
        android:textStyle="bold"
        android:gravity="center"/>
</FrameLayout>

1 个答案:

答案 0 :(得分:0)

清除此代码;

 Button button = (Button)getActivity().findViewById(R.id.button);
像这样;

 Button button = (Button)view.findViewById(R.id.button);

它将解决您的问题。