我需要在FaceBookFragment.java中更改在BaseFragment.java中夸大的布局( fragment_base )的ImageView的资源(图像)。
在 FaceBookFragment 上,执行此操作时出现空对象引用错误:
ImageView fragIcon = view.findViewById(R.id.fragIcon);
fragIcon.setImageResource(R.drawable.newsIcon);
这是因为在 FaceBookFragment 上,我夸大了布局 app_bar_main_tabs 而不是 fragment_base ,这是因为我需要在其他位置使用ViewPager实现其他未来功能。我知道它相当广泛,但任何帮助都将不胜感激。
如何从* FaceBookFragment更改此ImageView的资源(图像)?这是代码:
fragment_base.xml 这是我需要更改的ImageView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:padding="16dp"
tools:showIn="@layout/app_bar_main_tabs">
<ImageView
android:id="@+id/frag_icon"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_above="@+id/frag_title"
android:layout_centerHorizontal="true"
android:contentDescription="Tab Icon"
android:tint="@color/colorPrimary" />
<TextView
android:id="@+id/frag_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="center"
android:text="HOME"
android:textSize="35sp"
android:textStyle="bold" />
<TextView
android:id="@+id/frag_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/frag_title"
android:gravity="center"
android:text="Home description"
android:textSize="20sp"
android:textStyle="italic" />
</RelativeLayout>
app_bar_main_tabs.xml :这是在 FaceBookFragment
处夸大的布局<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:theme="@style/AppTheme.PopupOverlay" />
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager.widget.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
android:theme="@style/AppTheme.MyNavigationDrawer"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
FaceBookFragment.java 在这里,我需要更改布局 fragment_base
中ImageView的资源(图像)public class FaceBookFragment extends Fragment {
private String[] tabs = {"NOTICIAS", "SOLICITUDES", "SOCIAL"};
private String[] descriptions = {"Acá encontrarás todas las noticias de tu ciudad.",
"Acá encontrarás todas las solicitudes de amistad.",
"Acá encontrarás todas las interecciones de tu red social."};
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.app_bar_main_tabs, container, false);
}
@Override
public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ViewPager viewPager = view.findViewById(R.id.view_pager);
viewPager.setAdapter(new FacebookViewPagerAdapter(getActivity().getSupportFragmentManager()));
final TabLayout tabLayout = view.findViewById(R.id.tab_layout);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setTabMode(TabLayout.MODE_FIXED);
// ImageView fragIcon = view.findViewById(R.id.frag_icon); ERROR HERE!! NULL OBJECT REFERENCE
// fragIcon.setImageResource(R.drawable.ic_tab_fb_news); ERROR HERE!!!
for (int i = 0; i < tabLayout.getTabCount(); i++) {
TabLayout.Tab tab = tabLayout.getTabAt(i);
Drawable iconTab = null;
switch (i) {
case 0:
iconTab = getContext().getResources().getDrawable(R.drawable.ic_tab_fb_news);
break;
case 1:
iconTab = getContext().getResources().getDrawable(R.drawable.ic_tab_fb_solicitudes);
break;
case 2:
iconTab = getContext().getResources().getDrawable(R.drawable.ic_tab_fb_socialfeed);
break;
}
if (tab != null) {
tab.setIcon(iconTab);
tab.setText(null);
}
}
}
private class FacebookViewPagerAdapter extends BaseViewPagerAdapter {
public FacebookViewPagerAdapter(@NonNull FragmentManager manager) {
super(manager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT, tabs, descriptions);
}
}
}
BaseViewPagerAdapter.java 在这里,我获得 BaseFragment
的实例public class BaseViewPagerAdapter extends FragmentStatePagerAdapter {
private String[] tabs;
private String[] descriptions;
public BaseViewPagerAdapter(FragmentManager manager, int behaviorResumeOnlyCurrentFragment, String[] tabs, String[] descriptions) {
super(manager, behaviorResumeOnlyCurrentFragment);
this.tabs = tabs;
this.descriptions = descriptions;
}
@NonNull
@Override
public Fragment getItem(int position) {
return BaseFragment.getInstance(tabs[position], descriptions[position]);
}
@Override
public int getCount() {
return tabs.length;
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return tabs[position];
}
}
BaseFragment.java 这是我在其中布局 fragment_base
的地方public class BaseFragment extends Fragment {
private String title;
private String description;
private static final String ARG_TITLE = "title";
private static final String ARG_DESCRIPTION = "description";
public static BaseFragment getInstance(String title, String description) {
BaseFragment fragment = new BaseFragment();
Bundle args = new Bundle();
args.putString(ARG_TITLE, title);
args.putString(ARG_DESCRIPTION, description);
fragment.setArguments(args);
return fragment;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle args = getArguments();
if (args != null) {
title = args.getString(ARG_TITLE);
description = args.getString(ARG_DESCRIPTION);
}
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_base, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
((TextView) view.findViewById(R.id.frag_title)).setText(this.title);
((TextView) view.findViewById(R.id.frag_description)).setText(this.description);
}
}
我将不胜感激!谢谢!