[英]Cannot resolve method 'findViewById(int)' in @Nullable fragment
I am trying to implement a button into a fragment in order to use the Float Action Button (FAB) in order to view the snackbar. 我试图将一个按钮实现为一个片段,以便使用“浮动操作按钮”(FAB)来查看小吃栏。 How do I link the fab1 to a button in a fragment?
如何将fab1链接到片段中的按钮? this is the
.java
file: 这是
.java
文件:
public class PrimaryFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.primary_layout,null);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab1);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Hai, Hallo", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
}
findViewById is defined in Activity not Fragment . findViewById是在Activity中而不是Fragment中定义的。
Try this: 尝试这个:
getActivity().findViewById(R.id.fab1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.