OnClick imageView无法运行 - Android

时间:2016-12-15 17:24:13

标签: android android-fragments

我想使用以下代码在我的片段中将imageView作为按钮:

  imageView4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // Create new fragment and transaction
            Fragment newFragment = new TermsOfServiceFragemnt();
            // consider using Java coding conventions (upper first char class names!!!)
            FragmentTransaction transaction = getFragmentManager().beginTransaction();

            // Replace whatever is in the fragment_container view with this fragment,
            // and add the transaction to the back stack
            transaction.replace(R.id.content_frame_container, newFragment);
            transaction.addToBackStack(null);

            // Commit the transaction
            transaction.commit();
        }

    });

它不起作用。什么都没发生。这是一个完整的片段代码:

    public OneFragment() {
        // Required empty public constructor
    }

    private static TextView creditWallet;

    private String[] myString;
    private static final Random rgenerator = new Random();
    private TextView firstName, textView13,textView14,textView15,textView16,textView17,textView18;
    private Animation animShake, wbijam, load1, load2, load3, load4, load5, load6, wbijam1, wbijam2, wbijam3, wbijam4, wbijam5;
    private ImageView img, imageView9,imageView4,imageView5,imageView6,imageView7,imageView8;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public static void onUpdateView(Context aiContext) {
        // TODO Auto-generated method stub
        if (aiContext != null && creditWallet != null)
            creditWallet.setText(PreferenceConnector.readInteger(aiContext, PreferenceConnector.WALLETPOINTS, 0) + "");

    }

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

        super.onViewCreated(view, savedInstanceState);
        img = (ImageView)view.findViewById(R.id.imgRandom);
        imageView9 = (ImageView)view.findViewById(R.id.imageView3);
        imageView4 = (ImageView)view.findViewById(R.id.imageView4);
        imageView5 = (ImageView)view.findViewById(R.id.imageView5);
        imageView6 = (ImageView)view.findViewById(R.id.imageView6);
        imageView7 = (ImageView)view.findViewById(R.id.imageView7);
        imageView8 = (ImageView)view.findViewById(R.id.imageView8);

        firstName = (TextView)view.findViewById(R.id.imie);


        textView13 = (TextView)view.findViewById(R.id.textView13);
        textView14 = (TextView)view.findViewById(R.id.textView15);
        textView15 = (TextView)view.findViewById(R.id.textView16);
        textView16 = (TextView)view.findViewById(R.id.textView17);
        textView17 = (TextView)view.findViewById(R.id.textView18);
        textView18 = (TextView)view.findViewById(R.id.textView19);

        final Animation wbijam = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.wbijam);
        textView13.startAnimation(wbijam);
        final Animation wbijam1 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.wbijam);
        textView14.startAnimation(wbijam1);
        final Animation wbijam2 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.wbijam);
        textView15.startAnimation(wbijam2);
        final Animation wbijam3 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.wbijam);
        textView16.startAnimation(wbijam3);
        final Animation wbijam4 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.wbijam);
        textView17.startAnimation(wbijam4);
        final Animation wbijam5 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.wbijam);
        textView18.startAnimation(wbijam5);






        final Animation animShake = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.shak);
        img.startAnimation(animShake);


        final Animation load1 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.load1);
        imageView9.startAnimation(load1);
        final Animation load2 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.load2);
        imageView4.startAnimation(load2);
        final Animation load3 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.load3);
        imageView5.startAnimation(load3);
        final Animation load4 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.load4);
        imageView6.startAnimation(load4);
        final Animation load5 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.load5);
        imageView7.startAnimation(load5);
        final Animation load6 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.load6);
        imageView8.startAnimation(load6);


        firstName.setText("Hey,"+" " +PreferenceConnector.readString(getActivity().getApplicationContext(), PreferenceConnector.FIRST_NAME, ""));

        // Inflate the layout for this fragment

        final ImageView img = (ImageView) view.findViewById(R.id.imgRandom);
//  return view;
//  ^^^^  error remove it
        Resources res = getResources();

        myString = res.getStringArray(R.array.myArray);

        String q = myString[rgenerator.nextInt(myString.length)];

        TextView tv = (TextView) view.findViewById(R.id.tv);
        tv.setText(q);

        imageView4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Create new fragment and transaction
                Fragment newFragment = new TermsOfServiceFragemnt();
                // consider using Java coding conventions (upper first char class names!!!)
                FragmentTransaction transaction = getFragmentManager().beginTransaction();

                // Replace whatever is in the fragment_container view with this fragment,
                // and add the transaction to the back stack
                transaction.replace(R.id.content_frame_container, newFragment);
                transaction.addToBackStack(null);

                // Commit the transaction
                transaction.commit();
            }

        });
            // I have 3 images named img_0 to img_2, so...
        final String str = "img_" + rnd.nextInt(9);
        img.setImageDrawable
                (
                        getResources().getDrawable(getResourceID(str, "drawable",
                                getActivity().getApplicationContext())
                ));
        return view;
        // ^^^ move it here
    }

        // I have 3 images named img_0 to img_2, so...



    protected final static int getResourceID
            (final String resName, final String resType, final Context ctx) {
        final int ResourceID =
                ctx.getResources().getIdentifier(resName, resType,
                        ctx.getApplicationInfo().packageName);
        if (ResourceID == 0) {
            throw new IllegalArgumentException
                    (
                            "No resource string found with name " + resName
                    );
        } else {
            return ResourceID;
        }

    }

}

请大家帮帮我!我和这几个小时打架了。谢谢Stack Comuunity!

修改

现在,我已经

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

    super.onViewCreated(view, savedInstanceState);


    imageView19 = (ImageView)view.findViewById(R.id.imageView3);

    imageView9.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // Create new fragment and transaction
            Fragment newFragment = new TermsOfServiceFragemnt();
            // consider using Java coding conventions (upper first char class names!!!)
            FragmentTransaction transaction = getFragmentManager().beginTransaction();

            // Replace whatever is in the fragment_container view with this fragment,
            // and add the transaction to the back stack
            transaction.replace(R.id.content_frame_container, newFragment);
            transaction.addToBackStack(null);

            // Commit the transaction
            transaction.commit();
        }

    });

并没有改变。不工作,没有错误等。

2 个答案:

答案 0 :(得分:0)

解决方案1: 如下所示,将代码添加到onActivityCreated(),

 @Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);
    // get the button view
    fashionImg = (ImageView) getView().findViewById(R.id.selectone);
    // set a onclick listener for when the button gets clicked
    fashionImg.setOnClickListener(new OnClickListener() {
        // Start new list activity
        public void onClick(View v) {
            // Toast Message 
            // New Activity..
        }
    });
}

解决方案2: 使用下面的Image View示例代码单击Listener,

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_one, container, false);

    // Here it is
    ImageButton camBt = (ImageButton)v.findViewById(R.id.button1);
    camBt.setOnClickListener(listener);


    return v;
}

答案 1 :(得分:0)

您可以尝试将onClickListener放在" onViewCreatead"方法

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
         imageView4 = (ImageView)view.findViewById(R.id.imageView4);

         imageView4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Create new fragment and transaction
                Fragment newFragment = new TermsOfServiceFragemnt();
                // consider using Java coding conventions (upper first char class names!!!)
                FragmentTransaction transaction = getFragmentManager().beginTransaction();

                // Replace whatever is in the fragment_container view with this fragment,
                // and add the transaction to the back stack
                transaction.replace(R.id.content_frame_container, newFragment);
                transaction.addToBackStack(null);

                // Commit the transaction
                transaction.commit();
            }

        });

        }