2017-08-05 72 views
0

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

MainActivity(响应是通讯的方法)

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

回答

0

清除此代码;

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

这样;

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

它会解决您的问题。

+0

@Vishnal我试过了,当我点击按钮时,应用程序仍然关闭。我认为故障是在这一行 'Tab2 testFragment =(Tab2)fragmentManager.findFragmentById(R.id.tab2Fragment); '因为我在fragment_tab1布局中添加了id片段。有没有其他的方式来添加ID到片段? – wooz