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

Scott Graves
Scott Graves
2,255 Points

BlogWebViewActivity webView crashing

I am having trouble with webView crashing in the BlogReader. In BlogWebViewActivity.java

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

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

//the last line crashes the app, comment it out and it runs ok.

*/

I have tried this with API 17 and 19 in a VDM and live phone running 4.3.

Any suggestions.

8 Answers

Aaron Arkie
Aaron Arkie
5,345 Points

Did you check if you put it in the blogWebViewActivity or in the fragment? when you create the blank activity file it creates two files one fragment and the other the regular one. This was happening to me also! Ill give you the code you need so you can copy and paste. Make sure you are using the blogWebViewActivity java file and not the fragment. :)

You can compare my code to see whats diff.

BlogWebViewActivity.java:

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
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());
    }

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



}
Aaron Arkie
Aaron Arkie
5,345 Points

You only need this method for the mainActivity file

MainListActivity.java

@Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        try {
            JSONArray jsonPosts = mBlogData.getJSONArray("posts");
            JSONObject jsonPost = jsonPosts.getJSONObject(position);
            String blogUrl = jsonPost.getString("url");
            Intent intent = new Intent(this,BlogWebViewActivity.class);
            intent.setData(Uri.parse(blogUrl));
            startActivity(intent);
        } catch (JSONException e) {
            logException(e);

        }
    }
Scott Graves
Scott Graves
2,255 Points

Your first response was exactly what it was. I had placed the webView1 in the fragment_blog_web_view and not the activity_blog_web_view and once I moved it to the proper layout, the app ran properly. I missed that during the instruction video. Thanks for your response.

Aaron Arkie
Aaron Arkie
5,345 Points

No problem! Yeah it happened to me to it takes a little investigating im glad you got it working too. Hey by any chance in your res folder and inside the menu sub folder. inside does it say activity_blog_web_view.xml or blog_web_view.xml i couldn't do the last two exercises where you add the share button/drop down menu icon in the app because something was interfering with the res folder and pretty much made my app inoperable until i restored the files to a later version i worked on. But i could never get the share Intent to work.

Scott Graves
Scott Graves
2,255 Points

it's blog_web_view.xml. I had issues with the Android Menu layout not showing the generated menu item and nothing when I did 'add' even though the generated menu item was showing up in the xml code. I ended up closing up eclipse, killing the adb.exe process in task manager and then reloading eclipse and it started behaving. I just left the blog_web_view.xml named as it was since the call for it matched. I did get the share intent to work but currently it's showing as a submenu item and not an icon and I am gonna work on that later. Make sure you are update to date in eclipse (help-->update) and the SDK manager. eclipse is a little buggy (loosely putting it) so you have to fiddle sometimes to get things working. Thanks again for your help.

Hello Scott,

I have the same issue where the share intent is showing as a submenu item and not an icon and I can't seem to get it to work no matter how many times I restart the layout from scratch. Were you able to get this to work by any chance? I also am having the same issue where the webview crashes, however, it only happens in the emulator and actually works fine on my phone so I'm sort of ok with looking past it in that case because of the amount of hours being spent trying to figure it out.

Scott Graves
Scott Graves
2,255 Points

no I wasn't able to get the icon to show up properly as it did in the video. I suspect it is a api version difference as I was compiling in the current api (19/4.4). I did resolve the webView crash by placing the layout in the fragment_blog_web_view and not the activity_blog_web_view. The webView attempts to call the fragment_blog_web and it crashes if it's 'empty'. The whole fragment thing is new to the current eclispe and the android studio is going that way too. The problem is that the videos use the non-fragmented method and it's difficult to unfragment cleanly in some cases (at least for me). It gets a little confusing and I haven't done it enough to be sure I have it down. On another project, I ended up just downloading the project files from the provided link that has the non-fragmented classes already to go so it matched the video. It's supposed to be a 'best practice' method and I could see it being that way on larger projects but it's a little confusing for me as a beginner. It would have been nice if eclipse had to ability to turn off fragmenting during the class build. Good luck and if all else fails, download the project files listed at the bottom right.

Yeah I'm starting to realize the Android updates make it a bit harder to follow along with when you're new to this stuff. Thanks for getting back.

Joseph Marcus
Joseph Marcus
11,069 Points

@Aaron Arkie and others who had the Res folder issue. I am glad to say I had the same issue and I got the answers here (http://stackoverflow.com/questions/885009/r-cannot-be-resolved-android-error). This is the answer in a nut shell:

Note: Eclipse sometimes likes to add an "import android.R" statement at the top of your files that use resources, especially when you ask Eclipse to sort or otherwise manage imports. This will cause your make to break. Look out for these erroneous import statements and delete them.

Clean project and rerun on your emulator.

Read the attached link above for more details.

Hope this helped?