从onClickListener启动Web链接(Launching a web

编程入门 行业动态 更新时间:2024-10-19 08:44:38
从onClickListener启动Web链接(Launching a web-link from onClickListener)

我正在尝试为Android开发一个应用程序,而java有点令人困惑。 我到目前为止的代码是....

protected 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); } });}}

代码当前编写方式的问题是当我尝试分配新意图(browserIntent)时,uri在我理解为该对象的“数据”部分。 Android Studio说uri无法转换为URI,但它被声明为URI只有几行以上!

我也尝试插入一串文本,也在上面初始化,看起来像......

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

编译很好,但是当我实际点击按钮时,它表示没有活动来处理意图。

所以我的问题依次是:1)如果我在代码中先前声明了URI uri变量,并且intent的第二个属性正在查找URI,那么当我简单地将变量uri提供给它时​​,为什么会出现错误? 2)在第二种情况下,当我试图解析字符串时,没有活动来处理意图,这是ACTION_VIEW的问题吗? 似乎它能够很好地找到URI,但实际上它无法通过打开浏览器来完成。 也许3)什么是最简单的java api或java教程可以解决你已经发现或知道的这个问题?

I'm trying my hand at developing an app for android, and the java is a bit confusing. The code I have so far is....

protected 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. Android Studio says that uri cannot be converted to a URI, but it's declared as a URI only a couple of lines above!

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? 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? It seems that its able to find the URI fine, however it can't actually go through with opening a browser. Perhaps 3) What is the simplest java api or java tutorial that would cover this issue that you have found or know of?

最满意答案

AS必须说“uri未初始化”,因为由于try-catch阻塞而无法保证。 因此,我们将代码更改为:

编辑:

愚蠢的我,我们需要使用Uri类而不是URI类,因为没有构造函数new Intent(String action, URI uri)但是有一个new Intent(String action, Uri uri) 。 有关URI和Uri之间的差异,请访问此处 。

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(); } } });}}

AS must be saying something like "uri is not initialized", because due to try-catch block that isn't guaranteed. 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). For differences between URI and Uri, visit here.

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(); } } });}}

更多推荐

本文发布于:2023-07-22 09:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1219145.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:链接   Web   onClickListener   Launching   web

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!