1

I have a webview and i want it to load a url as soon as the app starts.

If I call loadURL() on the press of a button it works perfectly but when I try to load the url inside onCreate() the app crashes.

WebView myWebView;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }


}

public void loadURL(View view) {
    myWebView = (WebView) findViewById(R.id.WebView);
    myWebView.setWebViewClient(new WebViewClient());
    myWebView.loadUrl("http://www.google.com");
}

The app crashes when I use this:

WebView myWebView;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }

    myWebView = (WebView) findViewById(R.id.WebView);
    myWebView.setWebViewClient(new WebViewClient());
    myWebView.loadUrl("http://www.google.com");

}

EDIT** This is what i am getting in my console.

[2014-06-14 19:33:58 - ddms] Can't bind to local 8700 for debugger
[2014-06-14 19:34:11 - ddms] null
java.nio.BufferOverflowException
    at java.nio.HeapByteBuffer.put(HeapByteBuffer.java:200)
    at com.android.ddmlib.JdwpPacket.movePacket(JdwpPacket.java:235)
    at com.android.ddmlib.Debugger.sendAndConsume(Debugger.java:347)
    at com.android.ddmlib.Client.forwardPacketToDebugger(Client.java:665)
    at com.android.ddmlib.MonitorThread.processClientActivity(MonitorThread.java:344)
    at com.android.ddmlib.MonitorThread.run(MonitorThread.java:263)

[2014-06-14 19:34:12 - ddms] null
java.nio.BufferOverflowException
    at java.nio.HeapByteBuffer.put(HeapByteBuffer.java:200)
    at com.android.ddmlib.JdwpPacket.movePacket(JdwpPacket.java:235)
    at com.android.ddmlib.Debugger.sendAndConsume(Debugger.java:347)
    at com.android.ddmlib.Client.forwardPacketToDebugger(Client.java:665)
    at com.android.ddmlib.MonitorThread.processClientActivity(MonitorThread.java:344)
    at com.android.ddmlib.MonitorThread.run(MonitorThread.java:263)

EDIT** Here is the logcat output

06-14 19:53:22.677: D/AndroidRuntime(8075): Shutting down VM
06-14 19:53:22.677: W/dalvikvm(8075): threadid=1: thread exiting with uncaught exception (group=0x41c11c80)
06-14 19:53:22.677: E/AndroidRuntime(8075): FATAL EXCEPTION: main
06-14 19:53:22.677: E/AndroidRuntime(8075): Process: com.myapp.myapp, PID: 8075
06-14 19:53:22.677: E/AndroidRuntime(8075): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp.myapp/com.myapp.myapp.MainActivity}: java.lang.NullPointerException
06-14 19:53:22.677: E/AndroidRuntime(8075):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2227)
06-14 19:53:22.677: E/AndroidRuntime(8075):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2277)
06-14 19:53:22.677: E/AndroidRuntime(8075):     at android.app.ActivityThread.access$800(ActivityThread.java:145)
06-14 19:53:22.677: E/AndroidRuntime(8075):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
06-14 19:53:22.677: E/AndroidRuntime(8075):     at android.os.Handler.dispatchMessage(Handler.java:102)
06-14 19:53:22.677: E/AndroidRuntime(8075):     at android.os.Looper.loop(Looper.java:136)
06-14 19:53:22.677: E/AndroidRuntime(8075):     at android.app.ActivityThread.main(ActivityThread.java:5088)
06-14 19:53:22.677: E/AndroidRuntime(8075):     at java.lang.reflect.Method.invokeNative(Native Method)
06-14 19:53:22.677: E/AndroidRuntime(8075):     at java.lang.reflect.Method.invoke(Method.java:515)
06-14 19:53:22.677: E/AndroidRuntime(8075):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
06-14 19:53:22.677: E/AndroidRuntime(8075):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-14 19:53:22.677: E/AndroidRuntime(8075):     at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:133)
06-14 19:53:22.677: E/AndroidRuntime(8075):     at dalvik.system.NativeStart.main(Native Method)
06-14 19:53:22.677: E/AndroidRuntime(8075): Caused by: java.lang.NullPointerException
06-14 19:53:22.677: E/AndroidRuntime(8075):     at com.myapp.myapp.MainActivity.onCreate(MainActivity.java:27)
06-14 19:53:22.677: E/AndroidRuntime(8075):     at android.app.Activity.performCreate(Activity.java:5310)
06-14 19:53:22.677: E/AndroidRuntime(8075):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-14 19:53:22.677: E/AndroidRuntime(8075):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2181)
06-14 19:53:22.677: E/AndroidRuntime(8075):     ... 12 more

Just for any use here is the complete MainActivity.java

package com.myapp.myapp;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {
    WebView myWebView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }

        myWebView = (WebView) findViewById(R.id.WebView);
        myWebView.setWebViewClient(new WebViewClient());
        myWebView.loadUrl("http://www.google.com");
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
            return rootView;
        }
    }

}
Karan Jit Singh
  • 595
  • 7
  • 25

2 Answers2

1

Solved it!

I finally came across this.

@Override
protected void onStart()
{
    super.onStart();
    myWebView = (WebView) findViewById(R.id.WebView);
    myWebView.setWebViewClient(new WebViewClient());
    myWebView.loadUrl("http://www.google.com");
}

Apparently the fragment wasn't loaded immediately

getFragmentManager().beginTransaction()
            .add(R.id.container, new PlaceholderFragment()).commit(); 

This takes some time to process so onStart which is executed after this did the work perfectly.

Karan Jit Singh
  • 595
  • 7
  • 25
0

this works on mine

For Local File

  WebView webView =(WebView) findViewById(R.id.webView1);
  webView.loadUrl("file:///android_asset/programs.htm");

make sure you have the file in this case the file programs.htm is stored on the Assets folder on android

For Website

WebView webView =(WebView) findViewById(R.id.webView1);
webView.loadUrl("http://www.facebook.com");

it will direct you whatever destination you desire in this case Facebok.com

hope this helps you...