Null Pointer Exception in Android html 5 app

I am trying to create my first android+html 5 application.. My Activity code is :

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


public class MainActivity extends Activity {

    WebView webView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        webView = new WebView(this);        
        String summary = "<html><body>HTML 5 Test</body></html>";
        webView = (WebView)findViewById(R.id.webView);
        webView.loadData( summary, "text/html; character=UTF-8", null);        
        setContentView(webView);
    }
}

When I run above code, It gives Null Pointer Exception...

09-08 09:27:21.393: D/AndroidRuntime(562): Shutting down VM
09-08 09:27:21.393: W/dalvikvm(562): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
09-08 09:27:21.530: E/AndroidRuntime(562): FATAL EXCEPTION: main
09-08 09:27:21.530: E/AndroidRuntime(562): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rightquery.test/com.rightquery.test.MainActivity}: java.lang.NullPointerException
09-08 09:27:21.530: E/AndroidRuntime(562):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
09-08 09:27:21.530: E/AndroidRuntime(562):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
09-08 09:27:21.530: E/AndroidRuntime(562):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
09-08 09:27:21.530: E/AndroidRuntime(562):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
09-08 09:27:21.530: E/AndroidRuntime(562):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-08 09:27:21.530: E/AndroidRuntime(562):  at android.os.Looper.loop(Looper.java:137)
09-08 09:27:21.530: E/AndroidRuntime(562):  at android.app.ActivityThread.main(ActivityThread.java:4424)
09-08 09:27:21.530: E/AndroidRuntime(562):  at java.lang.reflect.Method.invokeNative(Native Method)
09-08 09:27:21.530: E/AndroidRuntime(562):  at java.lang.reflect.Method.invoke(Method.java:511)
09-08 09:27:21.530: E/AndroidRuntime(562):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-08 09:27:21.530: E/AndroidRuntime(562):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-08 09:27:21.530: E/AndroidRuntime(562):  at dalvik.system.NativeStart.main(Native Method)
09-08 09:27:21.530: E/AndroidRuntime(562): Caused by: java.lang.NullPointerException
09-08 09:27:21.530: E/AndroidRuntime(562):  at com.rightquery.test.MainActivity.onCreate(MainActivity.java:19)
09-08 09:27:21.530: E/AndroidRuntime(562):  at android.app.Activity.performCreate(Activity.java:4465)
09-08 09:27:21.530: E/AndroidRuntime(562):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-08 09:27:21.530: E/AndroidRuntime(562):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
09-08 09:27:21.530: E/AndroidRuntime(562):  ... 11 more

EDIT: Layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"    
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <WebView
        android:id="@+id/webView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="2dp"
        android:layout_marginLeft="2dp" />

</RelativeLayout>

What is wrong ?

The Solution:

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


public class MainActivity extends Activity {

    WebView webView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        webView = new WebView(this); 
        setContentView(R.layout.main);
        String summary = "<html><body>HTML 5 Test</body></html>";

        webView = new WebView(this);
        webView.loadData( summary, "text/html; character=UTF-8", null);        
        setContentView(webView);
    }
}

Answers


You need to do setContentView(R.layout.youView); before calling

webView = (WebView)findViewById(R.id.webView);

Need Your Help

Rspec tests not running because of initializer Yaml files

ruby rspec ruby-on-rails-3.2 yaml

I'm integrating tests with Rspec into quite a large / developed app. When I run the test I'm getting very odd errors from some of my initializers.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.