androidandroidimageslider

unreachable statement after replacing "this" to "getActivity ()"


I am trying to create a slide image in the fragment using viewpager. I am getting some problems, I can not get the getActivity() function,

previously I used

adapter = new Home_imageadapter (this, imageResource);

because many of my errors change it to

adapter = new Home_imageadapter (getActivity (), imageResource);

and now only one error occurs.

Why and how to fix this?

    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) {

            }
        });
    }
 }

Solution

  • You have return rootView; in the middle of your code. Move it to the last line in your onCreateView() method.