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

Opening a Webpage Within a App STRANGE Code!

I'm following along on this tutorial doing very well but there is slight problem Ben Jakuben created a new activity and got a new java activity with only 25 or so lines when I created mine it had almost twice as much and it also made a new fragment_blog_web_view.xml but I thought it was an update eclispe did so I just followed along now when I start my app everything is fine but when I click a Blog Title it crashes SOMEONE HELP! Here is my code for

BlogWebViewActivity.java

package com.jwelchapps.blogreader;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;

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

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }

    @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;
    }


    /**
     * 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;
        }
    }

}

Here is the fragment_blog_web_view.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.jwelchapps.blogreader.BlogWebViewActivity$PlaceholderFragment" >

    <WebView
        android:id="@+id/webView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

</RelativeLayout>

6 Answers

I haven't learned fragments yet but I had similar trouble and found this:

In his tutorial Eclipse doesn't include fragments, which with newer versions auto-generates when you create new classes . You can delete the one created once you copy the code in fragment_blog_web_view.xml into activity_blog_web_view.xml

Also change tools:context=".BlogWebViewActivity" > from the fragment, I also think there was a center gravity alignment that through things off for me which you can test/delete if needed.

And delete all the the fragment code in the java file which is:

if (savedInstanceState == null) {
    getSupportFragmentManager().beginTransaction()
            .add(R.id.container, new PlaceholderFragment()).commit();
}

and at the bottom under the comment.

That just gives me more errors. Ben Jakuben Please help!

Delete this below AND delete the xml file referencing the fragment.. If you deleted the xml file for the fragment, you have a method that is trying to reference a file that no longer exists (fragment_blog_XXX).You have to make sure you are following Java convention with the start and close braces when you do this, as the copy and paste here seems kind of wonky- it leaves out the declaration of the PlaceHolder class in my answer to you, and this has to be deleted too..

If you're using Eclipse, run "clean" a few times and/or restart the IDE. You'd be surprised how many weird errors just doing that solves. This is particularly true of XML changes.

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

}

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Sorry for the trouble! I'll refresh this shortly after Google I/O later this month. Let us know if you're still stuck with this!

Im still stuck at this...

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Hashsham, can you post a new question in the Forum with your code? Tag me with "@Ben Jakuben" so I see it.

I am still having the same problem I dont want to start deleting files and stuff cus I dont know where all the references to that file are and I might end up making a monumental mess. Ben Jakuben

Here's a suggestion..Download the course's files and replace your problem files with the downloaded files, or at least side by side compare them to see where the problem is. Sometimes one simple misstep can cause big problems.

I worked it out the problem I was having anyway. When you create the class it makes two new files a fragment and a normal xml. Which you find the normal on in the layout directory!