Drx
Drx

Reputation: 167

Displaying websites in my webview

i have a problem in loading url in my webview. i put my code in onCreate so that when this activity called it would directly load the url in webview widget. I use this code.

@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");

}

Everytime i put this code in my activity it always force close and my logcat error is

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

Please anyone help me.

Upvotes: 0

Views: 88

Answers (2)

meborda
meborda

Reputation: 391

Please check if your webview's id is really imageView1 and is really on your R.layout.activity_loadurl .

PS: please don't use confusing ids, like imageView1 on WebView. Use descriptive ones.

Upvotes: 1

uncannyj
uncannyj

Reputation: 306

Did you check that your LoadurlActivity activity is added in your manifest file? Something like this for instance,

<activity 
 android:name=".LoadurlActivity" 
 android:label="@string/app_name">
</activity>

You can check the developer guide about manifest for more.

Upvotes: 0

Related Questions