简体   繁体   中英

getActivity().getPackageManager() showing null?

I have tried removing the if condition but didn't work, also it's inside the fragment so no context issue.

button = view.findViewById(R.id.button);
url = view.findViewById(R.id.Url);
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        String s= url.getText().toString();
        Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(s));

        if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
            startActivity(intent);
        }
        else{
            url.setText("Error");
        }
    }
});

It's not getActivity().getPackageManager() that is null .
It's intent.resolveActivity that returns null
And this simply means that there doesn't exist an activity that can handle the Uri that you provided. Be sure to write your uri correct

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM