我正在尝试为我的父片段实现Tabs Fragment的接口侦听器。它给出Null值错误。 这是我的代码。 在下面给出的代码中,我创建了名为OnFeedItemClickListener的接口,并尝试将该方法重写为制表符片段。
parent_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
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"
app:layout_scrollFlags="scroll|enterAlways"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical">
<!-- TODO: Update blank fragment layout -->
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<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" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom|end"
app:layout_collapseMode="parallax"
app:layout_scrollFlags="scroll|enterAlways"
android:orientation="horizontal">
<Button
android:id="@+id/ButtonId"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Switch"
android:textAllCaps="false"
android:textColor="#FFFFFF" />
</LinearLayout>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
app:tabGravity="fill"
app:tabMode="fixed"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|enterAlways"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" >
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
fragment_one.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/navigation" />
</RelativeLayout>
ParentFragment.java
public class ParentFragment extends Fragment implements NavigationView.OnNavigationItemSelectedListener {
private List<Fragment> fragmentList = new ArrayList<>();
private List<String> titleList = new ArrayList<>();
private View rootView;
private ViewPager viewPager;
private TextTabsAdapter adapter;
private TabLayout tabLayout;
private Button BtnId;
private OnFeedItemClickListener onFeedItemClickListener;
public ParentFragment() {
// Required empty public constructor
}
private void initialise() {
Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
toolbar.setTitle("Dashboard");
DrawerLayout drawer = (DrawerLayout) rootView.findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
getActivity(), drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
drawer.setBackgroundColor(getResources().getColor(R.color.whiteColor));
toggle.syncState();
NavigationView navigationView = (NavigationView) rootView.findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
viewPager = (ViewPager) rootView.findViewById(R.id.viewPager);
tabLayout = (TabLayout) rootView.findViewById(R.id.tabs);
}
private void prepareDataResource() {
addData(new FragmentOne(), "ONE");
addData(new FragmentTwo(), "TWO");
}
private void addData(Fragment fragment, String title) {
fragmentList.add(fragment);
titleList.add(title);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.parent_fragment, container, false);
initialise();
prepareDataResource();
adapter = new TextTabsAdapter(getChildFragmentManager(), fragmentList, titleList);
// Bind Adapter to ViewPager.
viewPager.setAdapter(adapter);
// Link ViewPager and TabLayout
tabLayout.setupWithViewPager(viewPager);
BtnId = (Button) rootView.findViewById(R.id.CountryId);
BtnId.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onFeedItemClickListener.onButtonClick();
}
});
return rootView;
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
return false;
}
public void setOnFeedItemClickListener(OnFeedItemClickListener onFeedItemClickListener) {
this.onFeedItemClickListener = onFeedItemClickListener;
}
public interface OnFeedItemClickListener {
void onButtonClick();
}
}
FragmentOne.java
public class FragmentOne extends Fragment implements FeedsAdapter.OnFeedItemClickListener,ParentFragment.OnFeedItemClickListener{
private RecyclerView mrecyclerview;
private FeedsAdapter feedadapter;
View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_one, container, false);
mrecyclerview = (RecyclerView) rootView.findViewById(R.id.recyclerView);
mrecyclerview.setLayoutManager(new LinearLayoutManager(getContext()));
populateFeedsData();
ParentFragment parentFragment = new ParentFragment();
parentFragment.setOnFeedItemClickListener(this);
return rootView;
}
@Override
public void onButtonClick() {
Toast.makeText(getContext(), "ButtonClicked", Toast.LENGTH_SHORT).show();
}
}
请提前帮助我解决这个问题很多谢谢:)
答案 0 :(得分:0)
在 FragmentOne 类中,您正在创建 ParentFragment 的新实例。
ParentFragment parentFragment = new ParentFragment();
您实际需要做的是获取创建FragmentOne子片段的 ParentFragment 的实例。
要做到这一点,你可以试试这个:
ParentFragment parentFrag = ((ParentFragment)FragmentOne.this.getParentFragment());
现在你可以通过 parentFrag 调用setOnFeedItemClickListener()函数。
希望它有所帮助!