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 Build a Blog Reader Android App Using Intents to Display and Share Posts Opening a Webpage Within the App

Charles Harpke
Charles Harpke
33,986 Points

Instead of viewing this URL in the browser, change the Intent to view it using the WebViewActivity class.

I get error: Bummer! There is a compiler error. Please click on preview to view your syntax errors! ./WebViewActivity.java:16: non-static method getData() cannot be referenced from a static context mUrl = Intent.getData().toString(); ^ 1 error

Charles Harpke
Charles Harpke
33,986 Points
HERE IS MY CODE:

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

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


    }
}

1 Answer

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) {
        super.onListItemClick(l, v, position, id);

        Intent intent = new Intent(this, WebViewActivity.class);
        intent.setData(Uri.parse(mUrls[position]));
        startActivity(intent);
    }
}