Fragment for Image Viewer (Android)

后端 未结 2 972
眼角桃花
眼角桃花 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<MainActivity> activity;
    
    public ExampleFragment(MainActivity mainActivity) {
       activity = new WeakReference<MainActivity>(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);
    }
    
    0 讨论(0)
  • 2021-01-24 07:49

    I added the following modifications to your code ;

    1) Import Fragment from support library(important if running on early versions of android);

    2) Insted of Inflater I used the inflater received as method param;

    3) I created a method findViewById that can be used in this fragment;

    4) I moved some of the initialisation code in the method onActivityCreated ; Here

    you cand find more about that method ;

    package com.example.gctest;
    import android.app.Activity;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup; 
    import android.view.View.OnClickListener;
    import android.webkit.WebView.FindListener;
    import android.widget.Button;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuInflater;
    
    import android.widget.ImageView;
    
    public class ExampleFragment extends Fragment {
    
    Button button;
    ImageView image;
    
    
    public ExampleFragment() {
    
    }
    
    public static ExampleFragment newInstance() {
        return new ExampleFragment();
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    
            //ImageView imageview = (ImageView)findViewById(R.id.imageView1); this will raise NullPointerException because the parent view has not been created. Is is created with this method;
            return inflater.inflate(R.layout.iamgeview_main, container, false); //just return the  view ;
        }
    
    @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            //here you can initialise your variables,listeners,e.t.c ;
            super.onActivityCreated(savedInstanceState);
    
            ImageView imageview = (ImageView)findViewById(R.id.imageView1);
            addListenerOnButton();
        }
    
    /**
     * You can use this method in order to access the child views of the fragment parent view;
     * @param id
     * @return
     */
    protected View findViewById(int id)
    {
        return getView().findViewById(id);
    }
    
    public void addListenerOnButton() {
    
    
        button = (Button) findViewById(R.id.btnChangeImage);
        button.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View arg0) {
                image.setImageResource(R.drawable.eagle);
            }
    
        });
    
    }
    
    0 讨论(0)
提交回复
热议问题