Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

May Nyein
May Nyein
4,561 Points

Code Challenge: Opening a Webpage in the Browser -Step 2

I've got "Bummer Try again! " in step 2. I think I'm right. Please show me the way !

    import android.os.Bundle;
    import android.widget.ArrayAdapter;
    import android.view.View;
    import android.view.ListView;
    import android.content.Intent;
    import android.net.Uri;

    public class CustomListActivity extends ListActivity {

    protected String[] mUrls = { "http://www.teamtreehouse.com", 
    "http://developer.android.com", "http://www.github.com" };
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_custom_list);
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
    android.R.layout.simple_list_item_1, mUrls);
    setListAdapter(adapter);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
     // Add code here
    super.onListItemClick(l,v,position,id);
     Integer TutPosition=Integer.parseInt(mUrls[position]);
    String TutUrl=mUrls[TutPosition];
     Intent intent=new Intent(Intent.ACTION_VIEW);
    intent.setData(Uri.parse(TutUrl));
    startActivity(intent);
    }
    }

Bummer! Try agin

1 Answer

May Nyein
May Nyein
4,561 Points

I finish this step after I try "CodeChallenge:Opening a Webpage Within the App". It's solved. But I want to know why my previous (above code) one is not working.

Ben Jakuben
Ben Jakuben
Treehouse Teacher

This line of code (which is not needed) is the one causing you problems:

Integer TutPosition=Integer.parseInt(mUrls[position]);

Let's start by working backwards. mUrls[position] returns a String, which in this case is one of the URLs in the list. Trying to parse an int out of that will not work. :)

Also, Integer.parseInt() returns a simple int, not a full-fledged Integer object. For reference, you'd want to use this format instead:

int tutPosition = Integer.parseInt("3");

May Nyein
May Nyein
4,561 Points

Oops what a silly mistake. Thank you for your help sir