0

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.

8
  • 1
    Just checking, do you have permission in place <uses-permission android:name="android.permission.INTERNET" /> ? Also is WebView webView_url; declared outside onCreate() ? Commented Aug 2, 2013 at 0:37
  • i already add that in my manifest. No, it is inside onCreate, so that when this activity called it will automatically load the url.
    – Drx
    Commented Aug 2, 2013 at 0:46
  • Which line is LoadurlActivity.java:36 ? Commented Aug 2, 2013 at 0:47
  • 1
    Are you sure you have a component with id imageView1 in your layout in activity_loadurl? Looks a bit weird that a WebView has an id that is more appropriate for an ImageView. findViewById returns null if there is no view with such id. Commented Aug 2, 2013 at 0:49
  • 2
    I understood that. I meant which is line 36 in your activity named LoadUrlActivity? Also please post your XML file. Commented Aug 2, 2013 at 0:58

2 Answers 2

1

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.

0

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.

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.