-1

my code is have a error for unreachable statement on line that have define image switcher

public class mainFragment extends Fragment implements ViewSwitcher.ViewFactory{

    private OnFragmentInteractionListener mListener;

    public mainFragment() {
        // Required empty public constructor
    }
    int cnt= 0;
    int imgs[]={
            R.drawable.mah_121,
            R.drawable.tamashagar_207,
            R.drawable.tandorosti_149

    };
    ImageSwitcher imgSwitcher;
    //final ImageView imageView;
    //ImageView imageView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_main, container, false);

        imgSwitcher = (ImageSwitcher) getView().findViewById(R.id.imageSwitcher);

        imgSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                ImageView imageView = new ImageView(getActivity());
                imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                //imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
                return imageView;
            }

        });
        imgSwitcher.setImageResource(imgs[cnt]);



    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnFragmentInteractionListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public View makeView() {
        return null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p/>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        public void onFragmentInteraction(Uri uri);
    }
}

on the line of

imgSwitcher = (ImageSwitcher) getView().findViewById(R.id.imageSwitcher);

I have "error unreachable Statement" after i use getActivity in imageview definition i have this problem

TWiStErRob
  • 44,762
  • 26
  • 170
  • 254
  • 3
    Of course you do, it is outside the method. `imgSwitcher.setImageResource(imgs[cnt])}));` With respect, if you can't just look at that and see it, you need to do some more basic Java studying before you build apps because you will have problem after problem for the simple stuff. ` – Simon Mar 08 '15 at 13:27

2 Answers2

1

That is because you are returning from that method at the start: return inflater.inflate(R.layout.fragment_main, container, false);

try: View view = inflater.inflate(R.layout.fragment_main, container, false); imgSwitcher = (ImageSwitcher) view.findViewById(R.id.imageSwitcher);

And then as the last row in that function: return view;

Marcus Hooper
  • 647
  • 7
  • 12
0

You can't have anything after a return ...; (except comments and whitespace)!

It's worth moving the code related to imgSwitcher into onViewCreated.

TWiStErRob
  • 44,762
  • 26
  • 170
  • 254