java - onclick 侦听器在片段中不起作用
问题描述
当我单击应用程序中的按钮时,我在按钮上实现了一个 onclicklistener 以打开画廊,没有任何反应
代码
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_statusfragment,container,false);
imageView = view.findViewById(R.id.imageView);
button = (Button)view.findViewById(R.id.button2);
textView = view.findViewById(R.id.textView);
view.findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(),"Upload screenshot",Toast.LENGTH_LONG).show();
if(ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){
requestPermissions(
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
2000);
}else {
Toast.makeText(getActivity(), "Choose Screenshot", Toast.LENGTH_LONG).show();
imageView.setVisibility(View.VISIBLE);
button.setVisibility(View.VISIBLE);
textView.setVisibility(View.VISIBLE);
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivityForResult(intent,1000);
}
}
}
});
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_statusfragment, container, false);
}
它也不显示 Toast 消息
解决方案
而不是return inflater.inflate(R.layout.fragment_statusfragment, container, false);
做return view;
推荐阅读
- react-native - React 本机 iOS 无法在纱线工作区中自动链接
- spring-boot - 如何修复 - 无法加载驱动程序类:com.vertica.jdbc.Driver
- arrays - 如何将 YAML 数据解析为自定义 Bash 数据数组/哈希结构?
- ios - SWIFT 如何操作不同于用户当前显示的导航堆栈
- c# - 如何在 ASP.Net Core - CKEDITOR 5 上使用自定义上传适配器?
- python - 如何排除错误并将结果重定向到新文件
- amazon-web-services - AWS Fargate 网络 - 端口 80:连接被拒绝
- reactjs - 如何使用现有域上的子目录主页在服务器上提供反应应用程序?
- excel - 创建具有两个数组的动态 SumProduct 函数
- r - 根据与另一个表的关系填充缺失值