片段onClick无法正常工作

时间:2016-12-18 13:44:07

标签: android android-fragments

我有:

imageView9.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Fragment fragment = new MyRewardsListFragment();
        FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.content_frame_container, fragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }
});

修改

片段的完整代码:

public class OneFragment extends Fragment {
    final Random rnd = new Random();


    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);



        imageView9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Fragment fragment = new MyRewardsListFragment();
                FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.content_frame_container, fragment);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
            }
        });

        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);


            // 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;
        }

    }

}

它不起作用。没有错误,只是没有。我点击时只看到我的应用的“加载圈子”。这有什么不对?

编辑:我添加了我的片段的完整代码,我想从中打开一个新代码。

0 个答案:

没有答案