Javascript not working on android webview consistently. Sometimes it works,most of the time it doesn't

StackOverflow https://stackoverflow.com/questions/17641780

Question

I'm trying to achieve syntax highlighting of a C program from a html page.

"index.html","prettify.css","prettify.js" are stored in the assets folder

This is the code of my webview :

package com.example.javascriptexample;

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 wv=(WebView) findViewById(R.id.webView);
        wv.getSettings().setJavaScriptEnabled(true);
        wv.getSettings().setDomStorageEnabled(true);
        wv.loadUrl("file:///android_asset/index.html");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

The corresponding layout file(XML) is :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <WebView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:id="@+id/webView" />

</RelativeLayout>

index.html is as follows:

<HTML>
<body>

<pre>
<code class="prettyprint">
/*---------- PROGRAM USING IF STATEMENT --------*/
#include < stdio.h >
#include < conio.h >
void main()
   {
       int count, i;
       float weight, height;
       count = 0;
       printf("Enter weight and height for 10 boys\n");

       for (i =1; i <= 10; i++)
       {
      scanf("%f %f", &weight, &height);
      if (weight < 50 && height > 170)
         count = count + 1;
       }
       printf("Number of boys with weight < 50 kg\n");
       printf("and height > 170 cm = %d\n", count);
       getch();
   }
</code>
</pre>

<script src="https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js"></script>

</body>
</HTML>

I tried searching on the internet but was unable to solve this problem. The text is getting displayed but not with syntax highlighting(i.e maybe there's something wrong with the way I have implemented javascript inside the webview. THe script however works without a hitch on my computer so maybe I'm making some mistake as I am a beginner to both javascript and android. btw,the prettify css and js files are not written by me,I downloaded them from the internet(code.google.com)

I went through other similar questions. One of the solutions offered was "You have to force the app to wait (try it, place a breakpoint at your webview, and then let it continue, you'll see the webview will load with javascript this time, reliably. Whereas if you just let the app load without the breakpoint, it frequently "skips" the javascript). Some sort of forced threaded wait method is needed."

-How to implement this ?? i.e,how to place a breakpoint and then let it continue ?

Était-ce utile?

La solution

You wrote that you have stored "prettify.css","prettify.js" in the assets folder, but you didn't use them. Instead you load the script via network, which could cause timing issues.

<script src="https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js"></script>

To use your local resources add the followling link to the head section

<link href="prettify.css" type="text/css" rel="stylesheet" />

and replace the network script tag with the following

<script type="text/javascript" src="prettify.js"></script>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top