简体   繁体   English

从onClickListener启动Web链接

[英]Launching a web-link from onClickListener

I'm trying my hand at developing an app for android, and the java is a bit confusing. 我正在尝试为Android开发应用程序,而Java有点令人困惑。 The code I have so far is.... 到目前为止,我的代码是...。

protected void onCreate(Bundle savedInstanceState) { 受保护的void onCreate(Bundle savedInstanceState){

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    ButtonClick = (Button) findViewById(R.id.ButtonClick);

    final String link = "htttp://www.stackoverflow.com";

    ButtonClick.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            URI uri;
            try {
                uri = new URI("http://www.stackoverflow.com");
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
            browserIntent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(browserIntent);
        }
    });}}

The issue with the way the code is currently written is when I try to assign the new intent (browserIntent), uri in what I understand to be the "data" part of that object. 当前编写代码的方式的问题是当我尝试在我理解为该对象的“数据”部分中分配新的意图(browserIntent)uri时。 Android Studio says that uri cannot be converted to a URI, but it's declared as a URI only a couple of lines above! Android Studio表示uri无法转换为URI,但仅在上面几行就声明为URI!

I also tried to insert a string of text, initialized above as well, which looks like... 我还尝试插入文本字符串,上面也进行了初始化,看起来像...

           browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(link);

This compiles fine, but when I actually click on the button it says that there is no activity to handle the intent. 这样编译就可以了,但是当我实际单击该按钮时,它说没有活动可以处理该意图。

So my questions in order are: 1) If I declare the URI uri variable earlier in the code, and the second property of the intent is looking for a URI, why is there an error when I simply feed it the variable uri? 所以我的问题依次是:1)如果我在代码的前面声明了URI uri变量,并且意图的第二个属性正在寻找URI,为什么当我简单地将其提供给变量uri时会出错? 2) In the second case, when I am trying to parse the string, where there is no activity to handle the intent, is this issue with ACTION_VIEW? 2)在第二种情况下,当我尝试解析字符串时,没有活动可处理意图,那么ACTION_VIEW是否会出现此问题? It seems that its able to find the URI fine, however it can't actually go through with opening a browser. 看起来它可以很好地找到URI,但是实际上并不能通过打开浏览器来完成。 Perhaps 3) What is the simplest java api or java tutorial that would cover this issue that you have found or know of? 也许3)最简单的Java api或Java教程可以解决您已经发现或知道的此问题?

AS must be saying something like "uri is not initialized", because due to try-catch block that isn't guaranteed. AS必须说出类似“未初始化uri”之类的字眼,因为由于try-catch块无法保证。 So, we change the code as following: 因此,我们将代码更改如下:

EDIT: 编辑:

Silly me, we need to use the Uri class instead of URI class as there is no constructor new Intent(String action, URI uri) but there is one for new Intent(String action, Uri uri) . 愚蠢的我,我们需要使用Uri类而不是URI类,因为没有构造函数new Intent(String action, URI uri)但是有一个构造函数用于new Intent(String action, Uri uri) For differences between URI and Uri , visit here . 有关URIUri之间的差异,请访问此处

ButtonClick.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            Uri uri;
            try { 
                uri = Uri.parse("http://www.stackoverflow.com");
                browserIntent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(browserIntent);
            } catch (URISyntaxException e) {
                e.printStackTrace();
            } 

        } 
    });}}

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

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