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

Kevin Lavi
Kevin Lavi
3,311 Points

NullPointerException in BlogReader app

After finishing the video on Opening a Webpage within the app my app crashes after clicking on the title. Logcat says I am getting a NullPointerException on line 26 of BlogWebViewActivity (below). I'm not exactly sure what logcat is talking about. There is nothing set to null there. There was an if statement automatically created by eclipse that was set to null that I removed, but I can't see the problem now. Can someone help me out?

I'm not sure how it will load once I make this post but line 26 is : webView.loadUrl(blogUri.toString());

public class BlogWebViewActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_blog_web_view);

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

    WebView webView = (WebView) (findViewById(R.id.webView1));
    webView.loadUrl(blogUri.toString());


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.blog_web_view, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_blog_web_view,
                container, false);
        return rootView;
    }
}

}

2 Answers

Kevin Lavi
Kevin Lavi
3,311 Points

So I realized a few things. There shouldn't be () around findViewById, and that I was doing everything in the fragment layout and not the Blog Web View Layout. I Deleted everything starting from this video and started over and still couldn't get it to work. So I deleted everything again starting from this video, up to the end of opening a blog post on the browser. And guess what? now this doesn't work either. LogCat isn't giving me a definitive answer as to where the problem is coming from, I'm at a complete loss here.

william parrish
william parrish
13,774 Points

I have this -

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

What you will notice is the creation of a seperate member variable for the Url to be loaded in webView.loadUrl, which gets the conversion of the Uri to string out of the way, so that the passed in mUrl member variable for webView immediately meets the criteria.

That is my best guess on why your's might not be working. I could be totally wrong.