Java 嗨,我的findViewById有个错误
你的问题从这里开始:Java 嗨,我的findViewById有个错误,java,findviewbyid,Java,Findviewbyid,你的问题从这里开始: public class HomeFragment extends BasePageFragment { private Unbinder unbinder; // @BindView(R.id.fab) // FloatingActionButton mFab; @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bu
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));
}
});
}
此语句后面的任何代码都不会执行,因为您已经返回了膨胀视图。将其更改为:
return inflater.inflate(R.layout.fragment_homepage, container, false);
这将使视图膨胀,并允许您在返回视图之前访问膨胀视图上的子视图(使用findViewById方法)。欢迎使用堆栈溢出!为了给你一个很好的答案,如果你还没有看一眼,它可能会帮助我们。如果你能提供一个答案,它可能也很有用。你的错误是什么?这是编译器错误还是运行时错误?这里有很多错误。阅读编译错误以了解它们。(返回后的语句,
FindViewById
未找到,不确定编译器是否会看到ImageButton
和ImageButton
之间的差异)
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;