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

Stuck on Opening a webpage within the app, objective 4

import android.os.Bundle;
import android.content.Intent;
import android.net.Uri;
import android.webkit.WebView;

public class WebViewActivity extends Activity {

    protected String mUrl;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);

        WebView webView = (WebView) findViewById(R.id.webView1);
       Intent intent = getIntent();
      Uri blogUri = intent.getData();
      mUrl.loadUrl(blogUri.toString());

10 Answers

Ok i found it out now but now I am stuck on the 4th :-(

import android.os.Bundle;
import android.content.Intent;
import android.net.Uri;
import android.webkit.WebView;

public class WebViewActivity extends Activity {

    protected String mUrl;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);

        WebView webView = (WebView) findViewById(R.id.webView1);
        Intent intent = getIntent();
        Uri blogUri = intent.getData();
        mUrl.loadUrl(blogUri.toString());
Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Your last line is trying to call the 'loadUrl' method from the mUrl variable, which is not correct. The mUrl variable should just be holding the blogUri as a String object (i.e. mUrl = <code to convert blogUri to a String>;).

Then, with that in place, you need to load the URL in the webView (hint: that 'loadUrl' method you are trying to call is actually from the WebView class). :)

I am sorry I still get an error;

import android.os.Bundle;
import android.content.Intent;
import android.net.Uri;
import android.webkit.WebView;

public class WebViewActivity extends Activity {

    protected String mUrl;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);

        WebView webView = (WebView) findViewById(R.id.webView1);
                 Intent intent = getIntent();
                 mUrl= intent.getData().toString(); }
        webView.loadUrl(blogUri.toString());
Ben Jakuben
Ben Jakuben
Treehouse Teacher

This is close! You still want the line you had earlier:

Uri blogUri = intent.getData();

Then set mUrl to the String version of the blogUri variable (like you're trying to do for loadUrl).

Then use your webView.loadUrl() method, but pass in the mUrl variable instead of what you have there.

This code looks like it might work except you need to move that last line before the closing curly brace on the mUrl line above it.

I am really stuck I give up :-( ;

import android.os.Bundle;
import android.content.Intent;
import android.net.Uri;
import android.webkit.WebView;

public class WebViewActivity extends Activity {

    protected String mUrl;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);

        WebView webView = (WebView) findViewById(R.id.webView1);
                 Intent intent = getIntent();
                 mUrl= intent.getData().toString(); }
         WebView.loadUrl("http://www.teamtreehouse.com", "http://developer.android.com", "http://www.github.com");
}
import android.os.Bundle;
import android.content.Intent;
import android.net.Uri;
import android.webkit.WebView;

public class WebViewActivity extends Activity {

    protected String mUrl;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);

        WebView webView = (WebView) findViewById(R.id.webView1);

     Intent intent = getIntent();
     Uri blogUri = intent.getData();
      WebView.loadUrl(mUrl.toString());
          WebView.loadUrl('mUrl');


    }
}
Ben Jakuben
Ben Jakuben
Treehouse Teacher

So with this one, you're good up through setting blogUri. Your next step should be to set mUrl using blogUri. So on the line after that you would have mUrl = blogUri.toString();

Now, mUrl is a String variable holding the correct String representation of the URL we want. So all you need is to use the loadUrl method on the webView variable (note the lowercase "w") and pass in mUrl as a parameter. The loadUrl() method expects a String parameter.

import android.os.Bundle; import android.content.Intent; import android.net.Uri; import android.webkit.WebView;

public class WebViewActivity extends Activity {

protected String mUrl;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_web_view);

    WebView webView = (WebView) findViewById(R.id.webView1);

 Intent intent = getIntent();
 Uri blogUri = intent.getData();
  WebView.loadUrl(mUrl.toString());
      WebView.loadUrl("mUrl");


}

}

Still get an error, i just dont get it Ps: Below theres another curly brace but somehow it wont copy

public class WebViewActivity extends Activity {

protected String mUrl;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_web_view);

    WebView webView = (WebView) findViewById(R.id.webView1);

    Intent intent = getIntent();
    Uri blogUri = intent.getData();
  webView.loadUrl(blogUri.toString());
    mUrl.setblogUri();
   WebView.loadUrl("mUrl");   
}

}

import android.content.Intent; import android.net.Uri; import android.webkit.WebView;

public class WebViewActivity extends Activity {

protected String mUrl;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_web_view);

    WebView webView = (WebView) findViewById(R.id.webView1);

Intent intent = getIntent();
    Uri blogUri = intent.getData();
  mUrl = (blogUri.toString());

webView.loadUrl("Url");   
}

}

I did It yeahhhhhh finally, I forgot the m, thank you Ben :-)

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Hooray!! I'm so glad you got past it. I never want to just give you the code for these, so hopefully working through it helps your learning in some way! :)