简体   繁体   English

嗨,我有一个 findViewById 错误

[英]HI.I have an error with findViewById

public class HomeFragment extends BasePageFragment {
    private Unbinder unbinder;


//    @BindView(R.id.fab)
//    FloatingActionButton mFab;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_homepage, container, false);


    ImageButton ImageButton = (ImageButton) FindViewById (R.id.imageButton);
    ImageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Uri uri = Uri.parse("https://play.google.com/store/apps/details?id=com.mowmo.minimale");
            startActivity(new Intent(Intent.ACTION_VIEW, uri));
        }
    });

}

Your problem starts here:你的问题从这里开始:

return inflater.inflate(R.layout.fragment_homepage, container, false);

None of the code following this statement will ever execute since you've already returned the inflated View.由于您已经返回了膨胀的视图,因此此语句之后的任何代码都不会执行。 Change it to this:改成这样:

View rootview = inflater.inflate(R.layout.fragment_homepage, container, false);


ImageButton ImageButton = (ImageButton) rootview.findViewById (R.id.imageButton);
ImageButton.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
          Uri uri = Uri.parse("https://play.google.com/store/apps/details?id=com.mowmo.minimale");
          startActivity(new Intent(Intent.ACTION_VIEW, uri));
        }
    });

return rootview;

This inflates the view and allows you to access the child views on the inflated view (using the findViewById method) before returning the view.这会膨胀视图并允许您在返回视图之前访问膨胀视图上的子视图(使用 findViewById 方法)。

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

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