0

Stackoverflowがあります。

WebViewを使用してJavaScriptウィジェットを表示するアプリケーションを作成しようとしていますが、ウィジェットをまったく表示できない壁にぶつかったようです。私はかなりの量の研究をしました、そして私がやろうとしていることは可能だと思います。

これが私の主な活動です

public class WebViewAppTestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //grab the webview
    WebView webview = (WebView) findViewById(R.id.webview);

  //grab the webview settings
    WebSettings websettings = webview.getSettings();

    //enable javascript
    websettings.setJavaScriptEnabled(true);

    //add a js interface to display the widgets
    webview.addJavascriptInterface(new JavaScriptInterface(this), "Android");

    //load a webpage
    webview.loadUrl("file:///android_asset/javascript/index.html");


}

}

次のようなファイルindex.htmlをロードします

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width; user-scalable=0;" />
<title>WebView test!</title>
</head>
<body>
<h1>MyHTML</h1>

<div id="panel">
<section class="episode"></section>
</div>

<script src="https://sourceurl1.js" />
<script src="https://sourceurl2.js" />
<script src="https://sourceurl3.js" />
<script src="https://sourceurl4.js" />

<script src="./widget.js" />
<script src="./top-episodes.js" />
<script>
  var TopEpisodes = new Company.TopEpisodes({
  $element: $('#panel section.episodes')
  });
  TopEpisodes.init({
         mode      : 'realtime',
         range     : { from: 1341911040, to: 1341907200 },
         path      : ["4FB159EB613033AA710006DF"],
         isEpisode : true
  });
</script>
</html>

</body>
</html>

アプリケーション(Nexus S 4G ICS 4.0.4)をテストしたときに表示されるのは、空白の白い画面のMyHTMLヘッダーだけです。xmlでインターネットアクセスが有効になっています。

WebView設定で有効にする必要があるものは他にありますか、それともHTMLファイルに何か問題がありますか?addJavaScriptInterfaceメソッド/JSインターフェイス内で何かをする必要がありますか?

とにかく私は非常に立ち往生していて、どんな提案にもオープンです!お時間をいただきありがとうございます!

編集この質問に対する回答は数日間ありません。この質問またはその他の役立つ詳細を明確にするために私にできることがあればお知らせください。またはそれが可能であれば。ありがとうございました!

4

2 に答える 2

2

HTMLが間違っ<script src="<location of js>" /> ています、場所を正しく指定していません、

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width; user-scalable=0;" />
<title>WebView test!</title>
</head>
<body>
<h1>MyHTML</h1>

<div id="panel">
<section class="episode"></section>
</div>
<!-- Below is the code if the js in the same location of your index.html file. -->
<script src="sourceurl1.js" />
<script src="sourceurl2.js" />
<script src="sourceurl3.js" />
<script src="sourceurl4.js" />

<!-- Below is the code if the js is outside the location of your index.html file. -->  
<script src="../widget.js" />

<!-- Below is the code if the js is outside the location of your index.html file., inside folder js-->  
<script src="../js/top-episodes.js" />
<script>
  var TopEpisodes = new Company.TopEpisodes({
  $element: $('#panel section.episodes')
  });
  TopEpisodes.init({
         mode      : 'realtime',
         range     : { from: 1341911040, to: 1341907200 },
         path      : ["4FB159EB613033AA710006DF"],
         isEpisode : true
  });
</script>
</html>

さらに役立つように、ファイルの場所を指定してください

于 2012-07-13T16:31:58.227 に答える
0

そのため、HTMLとJavascriptの両方の経験が不足しているため、アプリケーションを別の方向に進めることにしました。

代わりに、次のチャートライブラリを使用してJavaでネイティブにウィジェットを開発します:http://achartengine.org/次に、JavascriptのWebView内にデータを作成するのではなく、HttpClientを使用してデータを更新します。 Androidアプリケーションの場合はより簡単かつ迅速に。

いつもありがとうございました!

于 2012-07-13T22:32:35.557 に答える