0

ズームとスクロールをプログラムする必要がないように、AndroidのWebViewを使用しています。アセットフォルダに必要なWebページがあり、これが私のコードです。エミュレータで実行するたびにエラーが発生しますが、そのようなファイルやディレクトリはありません。私の質問は、これが機能するために何を変更する必要があるかということです。

.javaをこれに更新したところ、button1を押すと強制終了が発生し、XMLは以下のようになります。

OK、最後の部分を修正しました。アプリを開くと、空白の画面が表示されるだけです。

package com.DS;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;

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


final Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener(){
    public void onClick(View v){

            WebView webView = (WebView) findViewById(R.id.webView);
            webView.getSettings().setJavaScriptEnabled(true);
            webView.getSettings().setBuiltInZoomControls(true);
            webView.getSettings().setSupportZoom(true);
            webView.loadUrl("file:///android_asset/stuffyoumustknowcoldforapcalc.htm");

    } });  
} 

}    

これが私のXMLです。追加されたWebViewの前に、2つのボタンと1つのテキストビューを持つホームアクティビティがありました。WebViewを追加すると、グラフィックレイアウトに表示されるのは、中央にWebViewと表示された灰色の画面だけです。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:gravity="center_horizontal"
android:orientation="vertical"
android:background="#4876ff" >
<Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_marginTop="90dp"
    android:textColor="#0000ff"
    android:text="AP Calculus" />
 <Button
    android:id="@+id/button2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/button1"
    android:layout_marginTop="38dp"
    android:text="More Coming Soon!" />
<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/button2"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="87dp"
    android:textColor="#FFFAFA"
    android:text="If you have any questions or want to report a bug, feel free to email                        me at fgoyer3856@gmail.com
                    Thank you!" />
<TextView
    android:id="@+id/titleView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="37dp"
    android:gravity="center_vertical"
    android:text="Welcome to Math Cheat Sheet!"
    android:textAppearance="?android:attr/textAppearanceLarge" />
<WebView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />
</RelativeLayout>
4

2 に答える 2

2

インテントを使用すると(上記で投稿したように)、独自のを使用する代わりに、新しい外部ブラウザインスタンスが表示され、Uriセットが読み込まれます。あなたが求めていることを達成するために、あなたはあなたの活動レイアウトの内部への参照を得て、それからあなたのページをロードしなければなりません。さらに、フォルダからページをロードする場合は、代わりにその方法を使用する必要があります。メソッドは次のようになります。IntentWebViewWebViewassets/AssetManagerWebView.loadData()onCreate()

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  // get a reference to the webview
  final WebView wV = (WebView) findViewById(R.id.webview);

  // get page content from assets/your_page.html as a String
  InputStream is = getAssets().open("your_page.html");
  BufferedReader br = new BufferedReader(new InputStreamReader(is));
  StringBuffer sb = new StringBuffer();
  String line;
  while((line=br.readLine())!=null){
    sb.append(line);
    sb.append("\n");
  }
  is.close();
  final String html = sb.toString();

  // get a reference to the button
  Button button1 = (Button) findViewById(R.id.button1);
  button1.setOnClickListener(new View.OnClickListener(){
    public void onClick(View v){

      // load html string into your WebView
      wV.loadData( html, "text/html", "utf-8" );
    }
  });

}
于 2012-04-22T20:31:29.860 に答える
1

レイアウトにWebビューを配置します。アクティビティで、このようにhtmlアセットをロードします。

webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setSupportZoom(true);
webView.loadUrl("file:///android_asset/stuffyoumustknowcoldforapcalc.htm");

stuffyoumustknowcoldforapcalc.htmそして、という名前のフォルダにファイルを置くことを忘れないでくださいassets

package com.DS;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.webkit.WebView;

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


    final Button button1 = (Button) findViewById(R.id.button1);
    button1.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){

                webView = (WebView) findViewById(R.id.webView1);
                webView.getSettings().setJavaScriptEnabled(true);
                webView.getSettings().setBuiltInZoomControls(true);
                webView.getSettings().setSupportZoom(true);
                webView.loadUrl("file:///android_asset/stuffyoumustknowcoldforapcalc.htm");

        } });  
    } 


}  
于 2012-04-22T19:59:08.637 に答える