문제

Getting Unreachable code error, not sure why. Any help would be awesome, only beginner. Getting it on 3rd line of code final View thumb1View = getView().findViewById(R.id.thumb_button_1);

Code

 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_zoom, container, false);


        final View thumb1View = getView().findViewById(R.id.thumb_button_1);
        thumb1View.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                zoomImageFromThumb(thumb1View, R.drawable.image1);
            }
        });

        final View thumb2View = getView().findViewById(R.id.thumb_button_2);
        thumb2View.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                zoomImageFromThumb(thumb2View, R.drawable.image2);
            }


        });

        // Retrieve and cache the system's default "short" animation time.
        mShortAnimationDuration = getResources().getInteger(android.R.integer.config_shortAnimTime);

        return thumb2View;

    }
도움이 되었습니까?

해결책 2

The proper way to write this method is like so:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.activity_zoom, container, false);


    final View thumb1View = view.findViewById(R.id.thumb_button_1);
    thumb1View.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            zoomImageFromThumb(thumb1View, R.drawable.image1);
        }
    });

    final View thumb2View = view.findViewById(R.id.thumb_button_2);
    thumb2View.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            zoomImageFromThumb(thumb2View, R.drawable.image2);
        }


    });

    // Retrieve and cache the system's default "short" animation time.
    mShortAnimationDuration = getResources().getInteger(android.R.integer.config_shortAnimTime);

    return view;

}

다른 팁

Since you are calling a return before the third line you get the error specified.

return inflater.inflate(R.layout.activity_zoom, container, false);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top