简体   繁体   English

方法不会覆盖其超类的方法。 Android片段

[英]method does not override method from its superclass . Android Fragment

fragment1: 片段1:

public class fragment1 extends Fragment implements View.OnClickListener {
    ImageButton but, but1, but2;
    ImageView view;
    @Override << this one
    public View OnCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View myView = inflater.inflate(R.layout.fragment1, null);
        but = (ImageButton) myView.findViewById(R.id.imageButton11);
        but.setOnClickListener(this);
        but1 = (ImageButton) myView.findViewById(R.id.imageButton1);
        but1.setOnClickListener(this);
        but2 = (ImageButton) myView.findViewById(R.id.imageButton2);
        but2.setOnClickListener(this);
        return myView;
    }

    @Override
    public void onClick(View v) {
        main xxx = (main)getActivity();
        switch (v.getId()) {
       case R.id.imageButton11:
                xxx.str="but1";
                break;
..
            }}

main: 主要:

Fragment frag1 = new fragment1();
    fr1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    fTrans = getFragmentManager().beginTransaction();
                    fTrans.replace(R.id.frameLayout2, frag1);
                    fTrans.addToBackStack(null);
                    fTrans.commit();
                }
            });

method does not override method from its superclass in fragment1(first override) but without "implemets View.OnClickListener" it works 方法不会覆盖fragment1中的超类方法(第一次覆盖)但没有“实现View.OnClickListener”它的工作原理

Spelling mistake. 拼写错误。 Name the method onCreateView instead of OnCreateView . 将方法命名为onCreateView而不是OnCreateView

I am not familiar with the android sdk but I think you need something like this 我不熟悉android sdk,但我认为你需要这样的东西

Fragment frag1 = new fragment1(){
         @Override
         public void onClick(View view) {
              fTrans = getFragmentManager().beginTransaction();
              fTrans.replace(R.id.frameLayout2, frag1);
              fTrans.addToBackStack(null);
              fTrans.commit();
         }
 };

return your inflated view 返回你的膨胀视图

public View onCreateView(.....) {
...
...

return InputFragmentView;
}

and change OnCreateView to onCreateView. 并将OnCreateView更改为onCreateView。 you can tap ctrl+o to get it ready ^^ 你可以点击ctrl + o来准备它^^

In my case I was not extending it from Fragment. 在我的情况下,我没有从Fragment扩展它。 Make sure you extend your fragment from Fragment class. 确保从Fragment类扩展片段。

public class fragment1 extends Fragment implements View.OnClickListener {
   @Override
///   public View onCreateView.......
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM