1

I am trying to create a simple webview and I am getting FC on each start.

Here is my MainActivity.java file:

package com.jerdog.apps;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        WebView myWebView = (WebView) findViewById(R.layout.main);
        myWebView.loadUrl("http://www.google.com");
    }
}

and here is my res/layout/main.xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
/>
</LinearLayout>

I am including my logcat from the moment I press Run in Eclipse at pastebin

http://pastebin.com/5trRw9Bd

1 Answer 1

2

This line:

WebView myWebView = (WebView) findViewById(R.layout.main);

is incorrect, and myWebView is null. Thus the following line throws the NullPointerException

Change the line to:

WebView myWebView = (WebView) findViewById(R.id.webview);

findViewById takes the id of the View you wish to find as an input, however you were passing in your layout. Changing this to the id of the WebView element from your main.xml file should fix the issue. There is a tutorial if you need a reference implementation.

6
  • Well now I get it to load in the emulator, but nothing shows up except for the title bar. I copied everything from the tutorial
    – Jeremy
    Jan 23, 2011 at 23:36
  • Did you include step 4 where you add the android.permission.INTERNET to your manifest?
    – dave.c
    Jan 24, 2011 at 1:17
  • Yes - the mainActivity.java file - pastebin.com/z2fabdA3 and the main.xml file - pastebin.com/DdJ8ER4x and the manifest file - pastebin.com/knNUcwQW
    – Jeremy
    Jan 24, 2011 at 1:21
  • Does your emulator have working internet access? Can you load google in the normal web browser?
    – dave.c
    Jan 24, 2011 at 2:00
  • yes - that was one of the first things I checked.... I was able to browse using the browser.
    – Jeremy
    Jan 24, 2011 at 3:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.