简体   繁体   English

getActivity().getPackageManager() 显示 null?

[英]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.我试过删除 if 条件但没有用,它也在片段内部,所以没有上下文问题。

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 .不是getActivity().getPackageManager()null
It's intent.resolveActivity that returns null返回intent.resolveActivity的是null
And this simply means that there doesn't exist an activity that can handle the Uri that you provided.这仅仅意味着不存在可以处理您提供的Uri的活动。 Be sure to write your uri correct一定要写正确的uri

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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