0

出现无法访问的代码错误,不知道为什么。任何帮助都会很棒,只有初学者。在代码的第 3 行获取它 final View thumb1View = getView().findViewById(R.id.thumb_button_1);

代码

 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;

    }
4

2 回答 2

4

由于您在第三行之前调用 return 您会得到指定的错误。

return inflater.inflate(R.layout.activity_zoom, container, false);
于 2013-07-02T14:18:07.537 回答
3

编写此方法的正确方法如下:

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;

}
于 2013-07-02T14:29:38.330 回答