我在我的 webview 中加载 url 时遇到问题。我把我的代码放在 onCreate 中,这样当这个活动被调用时,它会直接在 webview 小部件中加载 url。我使用此代码。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loadurl);
webView_url = (WebView)findViewById(R.id.imageView1);
webView_url.getSettings().setJavaScriptEnabled(true);
webView_url.loadUrl("http://www.google.com");
}
每次我将此代码放在我的活动中时,它总是强制关闭并且我的 logcat 错误是
08-02 08:37:04.780: E/AndroidRuntime(10321): FATAL EXCEPTION: main
08-02 08:37:04.780: E/AndroidRuntime(10321): java.lang.RuntimeException: Unable to
start activity
ComponentInfo{com.example.qrreader/com.example.qrreader.LoadurlActivity}:
java.lang.NullPointerException
08-02 08:37:04.780: E/AndroidRuntime(10321): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
08-02 08:37:04.780: E/AndroidRuntime(10321): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
08-02 08:37:04.780: E/AndroidRuntime(10321): at
android.app.ActivityThread.access$600(ActivityThread.java:123)
08-02 08:37:04.780: E/AndroidRuntime(10321): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
08-02 08:37:04.780: E/AndroidRuntime(10321): at
android.os.Handler.dispatchMessage(Handler.java:99)
08-02 08:37:04.780: E/AndroidRuntime(10321): at
android.os.Looper.loop(Looper.java:137)
08-02 08:37:04.780: E/AndroidRuntime(10321): at
android.app.ActivityThread.main(ActivityThread.java:4424)
08-02 08:37:04.780: E/AndroidRuntime(10321): at
java.lang.reflect.Method.invokeNative(Native Method)
08-02 08:37:04.780: E/AndroidRuntime(10321): at
java.lang.reflect.Method.invoke(Method.java:511)
08-02 08:37:04.780: E/AndroidRuntime(10321): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-02 08:37:04.780: E/AndroidRuntime(10321): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-02 08:37:04.780: E/AndroidRuntime(10321): at
dalvik.system.NativeStart.main(Native Method)
08-02 08:37:04.780: E/AndroidRuntime(10321): Caused by: java.lang.NullPointerException
08-02 08:37:04.780: E/AndroidRuntime(10321): at
com.example.qrreader.LoadurlActivity.onCreate(LoadurlActivity.java:36)
08-02 08:37:04.780: E/AndroidRuntime(10321): at
android.app.Activity.performCreate(Activity.java:4465)
08-02 08:37:04.780: E/AndroidRuntime(10321): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
08-02 08:37:04.780: E/AndroidRuntime(10321): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
08-02 08:37:04.780: E/AndroidRuntime(10321): ... 11 more
请任何人帮助我。