Fragment for Image Viewer (Android)

后端 未结 2 1009
眼角桃花
眼角桃花 2021-01-24 06:44

I\'m trying to create a simple fragment that merely displays an image when clicked. I\'m getting numerous errors inlcuding \"the method findViewbyId(int) is undefined for the ty

2条回答
  •  忘掉有多难
    2021-01-24 07:26

    findViewById is a method in the class Activity. To avoid passing an instance of the entire activity use a WeakRefence (see http://developer.android.com/reference/java/lang/ref/WeakReference.html) for example

    private WeakReference activity;
    
    public ExampleFragment(MainActivity mainActivity) {
       activity = new WeakReference(mainActivity);
    } 
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        ImageView imageview = (ImageView)activity.findViewById(R.id.imageView1);
        return Inflater.inflate(R.layout.iamgeview_main, container, false);
    }
    

提交回复
热议问题