[英]Implementing Floating Action Button on a Fragment Class
Hi I want to add Floating Action Button on a Fragment Class, but when ever I add implements AppCompatActivity I get this error:嗨,我想在 Fragment 类上添加浮动操作按钮,但是当我添加工具 AppCompatActivity 时,我收到此错误:
"getLoaderManager()' in 'android.support.v4.app.Fragment' clashes with 'getLoaderManager()' in 'android.app.Activity'; attempting to use incompatible return type" “'android.support.v4.app.Fragment' 中的 getLoaderManager()' 与 'android.app.Activity' 中的 'getLoaderManager()' 冲突;试图使用不兼容的返回类型”
public class home extends Fragment implements AppCompatActivity{公共类 home 扩展 Fragment 实现 AppCompatActivity{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.home, container, false);
return rootView;
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
} }
只需从片段中删除“implements AppCompatActivity”并将“extends AppCompatActivity”添加到包含片段的活动中。
我知道这个问题很老,但对于其他任何寻找这个解决方案的人来说,只需在onCreateView 的右括号之前的最后一个写返回视图。
you return the rootview before actually using the fab.您在实际使用 fab 之前返回 rootview。
View rootView = inflater.inflate(R.layout.home, container, false);
FloatingActionButton fab = (FloatingActionButton) rootView.findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
return rootView;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.