0

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?

bdpolinsky
  • 341
  • 5
  • 18

1 Answers1

0

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

        } 
    });}}
Community
  • 1
  • 1
Shaishav
  • 5,282
  • 2
  • 22
  • 41
  • You have put `Button BottonClick;` somewhere, right? – Shaishav Aug 15 '16 at 04:34
  • Yeah it's on the xml page in terms of the layout. – bdpolinsky Aug 15 '16 at 11:27
  • yesss it works! Thank you very much! For your information I'm trying to build an app where a user can click on a link, and the phone pulls up A) – bdpolinsky Aug 15 '16 at 22:20
  • yesss it works! Thank you very much! For your information the thought that generated this interest is - I'm working on a list of "merchants" and "products they sell"; my goal is to allow a user to do a search on either merchants or products they sell, and the application outputs information about the business, which includes a link to their website or any digital presence. So I probably need to start focusing on how to use an app, or a java application, to query a database or a spreadsheet. I can keep you informed if you'd like. – bdpolinsky Aug 15 '16 at 22:26
  • however, after i made those changes, android studio also showed an error message in the try/catch statement because the catch statement that's there would not have been executed. Not an error really, just a FYI. – bdpolinsky Aug 15 '16 at 23:20