1

I am new to eclipse/Java. I searched for answer but nothing solved my problem. I hope someone can guide me. I am attempting to launch a webpage withing an android application however I am getting a blank page in the Android Virtual Emulation.

Here is my code

package com.example.t4;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.webkit.WebView;

public class MainActivity extends Activity {

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

    WebView myWebView = (WebView) findViewById(R.id.webview);
    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;
  }
}

Here is the AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.t4"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET"></uses-permission>

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.t4.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
   </application>

   </manifest>

and here is the activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<WebView 
    android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
/>

</LinearLayout>

I am not sure what I am doing wrong. The code runs without an error. I just get a blank screen. Any help is much appreciated. Thanks in advance.

ejohansson
  • 2,832
  • 1
  • 23
  • 30
  • Can you check if your AVD is on-line? Just go to the browser and try to go to google.com or some other random page. – DrkStr Mar 14 '14 at 14:30
  • Thanks for the suggestion. I opened the AVD and just have a blank page. None of the buttons are doing anything. I am really new to this so bear with me. Can you guide me more? – user3420186 Mar 14 '14 at 15:52
  • I clicked the 'home' button it did nothing. or any other button for that matter, – user3420186 Mar 14 '14 at 15:52
  • 1
    Your AVD has not loaded yet. That is you problem. depending on your computer it can take upto 15 mins. – DrkStr Mar 14 '14 at 16:04
  • Thanks DrkStr. You are right it took a while for it to load. I can see the AVD. I got a red border around the screen and now I am waiting to see if the app loads. so far nothing. I will step out and check back in 1 hour. – user3420186 Mar 14 '14 at 21:27
  • Hi, I dont know what emulator you are using at the moment(in eclipse), but depending on the computer you have you can use this to get a mroe faster and more responsive AVD. https://www.youtube.com/watch?v=xea3Ng-cwwg – DrkStr Mar 15 '14 at 04:46
  • Thanks. Successfully running the app now on my android device. I am skipping the ADV. I just need to figure out how to make pdfs open in pdf capable app or inside my app. – user3420186 Mar 15 '14 at 13:10
  • Here this might be of some help. http://stackoverflow.com/questions/17453105/android-open-pdf-file – DrkStr Mar 16 '14 at 11:07

3 Answers3

1

try to put myWebView.getSettings().setJavaScriptEnabled(true); // enable javascript

myWebView.loadUrl("http://www.google.com");
Lucian Novac
  • 1,255
  • 12
  • 18
  • Thanks. Did that. Run it. Still a blank screen. Any other ideas? – user3420186 Mar 14 '14 at 14:54
  • yes also put this code myWebview.setWebViewClient(new WebViewClient() { public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { // do something here } }); – Lucian Novac Mar 14 '14 at 14:56
  • Thanks Lucian. I am not sure how to use the code that you wrote. I copied and pasted and got errors. I was able to fix one error by changing myWebview.setWebViewClient to myWebView.setWebViewClient (changed the v to Captial V in myWebView). The errors are many errors and I am not sure where to start. Do you I need to import anything else? – user3420186 Mar 14 '14 at 15:46
  • Your AVD has not loaded yet. That is you problem. depending on your computer it can take upto 15 mins. – DrkStr Mar 14 '14 at 16:04
  • The ADV machine is really slow. Ended up running it on my Android device. Worked but asked me to select which application to open in and I opened the URL in browser. partial success. – user3420186 Mar 15 '14 at 06:30
  • Got it working using WebView theWebPage = new WebView(this); setContentView(theWebPage); theWebPage.loadUrl("http://www.example.com"); – user3420186 Mar 15 '14 at 06:34
0

Check out this working basic webview. I tested with "www.google.com" like yours and came up with a blank view. I used "http://www.google.com" and it ran fine. Try that out on yours? But feel free to steal the sample code too.

KDEx
  • 3,505
  • 4
  • 31
  • 39
0

Restart your app (I mean click Run again) and make sure you have enabled javaScript as mentioned above.

Eenvincible
  • 5,641
  • 2
  • 27
  • 46