将 "this" 替换为 "getActivity ()" 后无法访问的语句
unreachable statement after replacing "this" to "getActivity ()"
我正在尝试使用 viewpager
在 fragment
中创建幻灯片图像。
我遇到了一些问题,我无法获得 getActivity()
函数,
之前我用过
adapter = new Home_imageadapter (this, imageResource);
因为我的很多错误将其更改为
adapter = new Home_imageadapter (getActivity (), imageResource);
现在只出现一处错误。
为什么以及如何解决这个问题?
public class OtherFragment extends Fragment {
private ViewPager viewPager;
private Beranda_imageadapter adapter;
private LinearLayout sliderdotspanel;
private int dotscount;
private ImageView[] dots;
private int[] imageResource = {
R.drawable.slide3,
R.drawable.slide1,
R.drawable.slide2
};
public OtherFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_other, container, false);
viewPager = (ViewPager) rootView.findViewById(R.id.viewpager);
sliderdotspanel = (LinearLayout) rootView.findViewById(R.id.viewPagerCountDots);
return rootView;
adapter = new Beranda_imageadapter(getActivity(), imageResource);
viewPager.setAdapter(adapter);
dotscount = adapter.getCount();
dots = new ImageView[dotscount];for (int i = 0; i < dotscount; i++) {
dots[i] = new ImageView(getActivity());
dots[i].setImageDrawable(ContextCompat.getDrawable(getActivity().getApplicationContext(), R.drawable.nonactive_dot));
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(8, 0, 8, 0);
sliderdotspanel.addView(dots[i], params);
dots[0].setImageDrawable(ContextCompat.getDrawable(getActivity().getApplicationContext(), R.drawable.active_dots));
}
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
for (int i = 0; i < dotscount; i++) {
dots[i].setImageDrawable(ContextCompat.getDrawable(getActivity().getApplicationContext(), R.drawable.nonactive_dot));
}
dots[position].setImageDrawable(ContextCompat.getDrawable(getActivity().getApplicationContext(), R.drawable.active_dots));
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
}
您的代码中间有 return rootView;
。将其移至 onCreateView()
方法的最后一行。
必须在设置布局属性后返回视图 [在您的情况下是 rootView],否则将无法到达之后添加的更改:
像这样更改您的 onCretae 方法:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_other, container, false);
viewPager = (ViewPager) rootView.findViewById(R.id.viewpager);
sliderdotspanel = (LinearLayout) rootView.findViewById(R.id.viewPagerCountDots);
adapter = new Beranda_imageadapter(getActivity(), imageResource);
viewPager.setAdapter(adapter);
dotscount = adapter.getCount();
dots = new ImageView[dotscount];for (int i = 0; i < dotscount; i++) {
dots[i] = new ImageView(getActivity());
dots[i].setImageDrawable(ContextCompat.getDrawable(getActivity().getApplicationContext(), R.drawable.nonactive_dot));
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(8, 0, 8, 0);
sliderdotspanel.addView(dots[i], params);
dots[0].setImageDrawable(ContextCompat.getDrawable(getActivity().getApplicationContext(), R.drawable.active_dots));
return rootView;
}
您可以使用 rootView.getContex()
或 rootView.getContex().getActivity()
而不是 getActivity()
,您还应该 return 在 onCreateView() 方法末尾使用 rootView。
我正在尝试使用 viewpager
在 fragment
中创建幻灯片图像。
我遇到了一些问题,我无法获得 getActivity()
函数,
之前我用过
adapter = new Home_imageadapter (this, imageResource);
因为我的很多错误将其更改为
adapter = new Home_imageadapter (getActivity (), imageResource);
现在只出现一处错误。
为什么以及如何解决这个问题?
public class OtherFragment extends Fragment {
private ViewPager viewPager;
private Beranda_imageadapter adapter;
private LinearLayout sliderdotspanel;
private int dotscount;
private ImageView[] dots;
private int[] imageResource = {
R.drawable.slide3,
R.drawable.slide1,
R.drawable.slide2
};
public OtherFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_other, container, false);
viewPager = (ViewPager) rootView.findViewById(R.id.viewpager);
sliderdotspanel = (LinearLayout) rootView.findViewById(R.id.viewPagerCountDots);
return rootView;
adapter = new Beranda_imageadapter(getActivity(), imageResource);
viewPager.setAdapter(adapter);
dotscount = adapter.getCount();
dots = new ImageView[dotscount];for (int i = 0; i < dotscount; i++) {
dots[i] = new ImageView(getActivity());
dots[i].setImageDrawable(ContextCompat.getDrawable(getActivity().getApplicationContext(), R.drawable.nonactive_dot));
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(8, 0, 8, 0);
sliderdotspanel.addView(dots[i], params);
dots[0].setImageDrawable(ContextCompat.getDrawable(getActivity().getApplicationContext(), R.drawable.active_dots));
}
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
for (int i = 0; i < dotscount; i++) {
dots[i].setImageDrawable(ContextCompat.getDrawable(getActivity().getApplicationContext(), R.drawable.nonactive_dot));
}
dots[position].setImageDrawable(ContextCompat.getDrawable(getActivity().getApplicationContext(), R.drawable.active_dots));
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
}
您的代码中间有 return rootView;
。将其移至 onCreateView()
方法的最后一行。
必须在设置布局属性后返回视图 [在您的情况下是 rootView],否则将无法到达之后添加的更改:
像这样更改您的 onCretae 方法:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_other, container, false);
viewPager = (ViewPager) rootView.findViewById(R.id.viewpager);
sliderdotspanel = (LinearLayout) rootView.findViewById(R.id.viewPagerCountDots);
adapter = new Beranda_imageadapter(getActivity(), imageResource);
viewPager.setAdapter(adapter);
dotscount = adapter.getCount();
dots = new ImageView[dotscount];for (int i = 0; i < dotscount; i++) {
dots[i] = new ImageView(getActivity());
dots[i].setImageDrawable(ContextCompat.getDrawable(getActivity().getApplicationContext(), R.drawable.nonactive_dot));
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(8, 0, 8, 0);
sliderdotspanel.addView(dots[i], params);
dots[0].setImageDrawable(ContextCompat.getDrawable(getActivity().getApplicationContext(), R.drawable.active_dots));
return rootView;
}
您可以使用 rootView.getContex()
或 rootView.getContex().getActivity()
而不是 getActivity()
,您还应该 return 在 onCreateView() 方法末尾使用 rootView。