Java onclick侦听器不';我不能断断续续地工作

Java onclick侦听器不';我不能断断续续地工作,java,android-studio,onclicklistener,android-button,Java,Android Studio,Onclicklistener,Android Button,我已经在一个按钮上实现了onclicklistener,当我在应用程序中单击按钮时,什么都不会发生 代码 它也不会显示Toast消息,而不是返回充气机。充气(R.layout.fragment\u statusfragment,container,false) do返回视图代替返回充气机。充气(R.layout.fragment\u status fragment,container,false) do返回视图您已经在初始化充气机。充气(R.layout.fragment\u statusfra

我已经在一个按钮上实现了onclicklistener,当我在应用程序中单击按钮时,什么都不会发生

代码


它也不会显示Toast消息,而不是返回充气机。充气(R.layout.fragment\u statusfragment,container,false)
do
返回视图

代替
返回充气机。充气(R.layout.fragment\u status fragment,container,false)

do
返回视图

您已经在初始化充气机。充气(R.layout.fragment\u statusfragment,container,false)开始时,将其分配给
视图
,因此只需
返回视图结束。

您已经在初始化充气机。充气(R.layout.fragment\u statusfragment,container,false)开始时,将其分配给
视图
,因此只需
返回视图结束。

首先尝试在
片段的
方法的
onViewCreated()
中完成所有UI工作

在这里,您不会返回上面最后一段中初始化的
视图
,而是使用
按钮
变量来实现click listener

@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);
    button.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 view;
}

首先尝试在
Fragment
onViewCreated()
方法中完成所有UI工作

在这里,您不会返回上面最后一段中初始化的
视图
,而是使用
按钮
变量来实现click listener

@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);
    button.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 view;
}