0

我正在尝试创建我的第一个 android+html 5 应用程序。我的活动代码是:

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

当我运行上面的代码时,它给出了空指针异常......

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

编辑:布局

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

怎么了 ?

解决方案:

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

3 回答 3

3

你需要 setContentView(R.layout.youView);在打电话之前做

webView = (WebView)findViewById(R.id.webView);
于 2012-09-08T04:17:18.173 回答
1

你这样打电话

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.XMLname);
       .....
  }
于 2012-09-08T04:29:03.793 回答
0

在你的 onCreate 做改变

setContentView(R.layout.main);
webView = (WebView)findViewById(R.id.webView);
于 2012-09-08T05:10:32.110 回答