0

我有一个网站 ( http://wrn.erik-edgren.nu/ ),通过 HTML5 Geolocation 获取访问者的 GPS 位置。我正在尝试创建一个访问该网站的应用程序,但我只是得到一个空白页面。为什么?

package com.example.wrn;

import android.location.LocationManager;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

@SuppressLint("SetJavaScriptEnabled")
public class MainActivity extends Activity {
    final Activity activity = this;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
        WebView webview = new WebView(this);
        setContentView(R.layout.activity_main);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setGeolocationEnabled(true);
        webview.getSettings().setDomStorageEnabled(true);
        webview.setWebChromeClient(new WebChromeClient());

        webview.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                activity.setTitle("Laddar...");
                activity.setProgress(progress * 100);

                if(progress == 100)
                    activity.setTitle(R.string.app_name);
            }
        });

        webview.setWebViewClient(new WebViewClient() {
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                // Handle the error
            }

            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });

        webview.loadUrl("http://wrn.erik-edgren.nu/mobile");

        LocationManager locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
        if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
             Toast.makeText(this, "GPS-mottagaren är aktiverad i din mobil", Toast.LENGTH_SHORT).show();
        } else {
            showGPSDisabledAlertToUser();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    private void showGPSDisabledAlertToUser() {
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
            alertDialogBuilder.setMessage("GPS-mottagaren är inaktiverad i din mobil. Den måste vara aktiverad. Vill du gå till platsinställningarna och aktivera GPS-mottagaren?")
         .setCancelable(false)
         .setPositiveButton("Ja",
              new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int id) {
                      Intent callGPSSettingIntent = new Intent(
                                            android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                            startActivity(callGPSSettingIntent);
              }
         });
         alertDialogBuilder.setNegativeButton("Nej",
              new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int id) {
                   dialog.cancel();
              }
         });
    AlertDialog alert = alertDialogBuilder.create();
    alert.show();
    }
}

10-24 14:54:12.113: I/dalvikvm(2031): threadid=3: reacting to signal 3
10-24 14:54:12.213: I/dalvikvm(2031): Wrote stack traces to '/data/anr/traces.txt'
10-24 14:54:12.533: I/dalvikvm(2031): threadid=3: reacting to signal 3
10-24 14:54:12.593: I/dalvikvm(2031): Wrote stack traces to '/data/anr/traces.txt'
10-24 14:54:12.993: I/dalvikvm(2031): threadid=3: reacting to signal 3
10-24 14:54:13.117: I/dalvikvm(2031): Wrote stack traces to '/data/anr/traces.txt'
10-24 14:54:13.463: I/dalvikvm(2031): threadid=3: reacting to signal 3
10-24 14:54:13.643: I/dalvikvm(2031): Wrote stack traces to '/data/anr/traces.txt'
10-24 14:54:13.983: I/dalvikvm(2031): threadid=3: reacting to signal 3
10-24 14:54:14.023: I/dalvikvm(2031): Wrote stack traces to '/data/anr/traces.txt'
10-24 14:54:14.463: I/dalvikvm(2031): threadid=3: reacting to signal 3
10-24 14:54:14.585: I/dalvikvm(2031): Wrote stack traces to '/data/anr/traces.txt'
10-24 14:54:14.983: D/gralloc_goldfish(2031): Emulator without GPU emulation detected.
10-24 14:54:15.003: I/dalvikvm(2031): threadid=3: reacting to signal 3
10-24 14:54:15.083: I/dalvikvm(2031): Wrote stack traces to '/data/anr/traces.txt'
10-24 14:54:22.505: I/SqliteDatabaseCpp(2031): sqlite returned: error code = 14, msg = cannot open file at line 27701 of [8609a15dfa], db=/data/data/com.example.wrn/databases/webview.db
10-24 14:54:22.513: I/SqliteDatabaseCpp(2031): sqlite returned: error code = 14, msg = os_unix.c: open() at line 27701 - "" errno=2 path=/CachedGeoposition.db, db=/data/data/com.example.wrn/databases/webview.db

编辑 这是我当前的代码。我已经添加setDatabasePath()甚至onReceivedError()重置了 LogCat 以再次尝试使用模拟器。结果与以前相同 - 空白页。

package com.example.wrn;

import android.location.LocationManager;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

@SuppressLint("SetJavaScriptEnabled")
public class MainActivity extends Activity {
    final Activity activity = this;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
        WebView webview = new WebView(this);

        /****************************************************/
        // NEW:
        WebSettings webSettings = webview.getSettings();
        /****************************************************/

        setContentView(R.layout.activity_main);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setGeolocationEnabled(true);
        webview.getSettings().setDomStorageEnabled(true);
        webview.setWebChromeClient(new WebChromeClient());

        /****************************************************/
        // NEW:
        webSettings.setDatabasePath("/data/data/"+this.getPackageName()+"/databases/");
        /****************************************************/

        webview.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                activity.setTitle("Laddar...");
                activity.setProgress(progress * 100);

                if(progress == 100)
                    activity.setTitle(R.string.app_name);
            }
        });

        webview.setWebViewClient(new WebViewClient() {
            /****************************************************/
            // NEW:
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Log.i("WEB_VIEW_TEST", "error code:" + errorCode);
                super.onReceivedError(view, errorCode, description, failingUrl);
            }
            /****************************************************/

            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });

        webview.loadUrl("http://wrn.erik-edgren.nu/mobile");

        LocationManager locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
        if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
             Toast.makeText(this, "GPS-mottagaren är aktiverad i din mobil", Toast.LENGTH_SHORT).show();
        } else {
            showGPSDisabledAlertToUser();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    private void showGPSDisabledAlertToUser() {
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
            alertDialogBuilder.setMessage("GPS-mottagaren är inaktiverad i din mobil. Den måste vara aktiverad. Vill du gå till platsinställningarna och aktivera GPS-mottagaren?")
         .setCancelable(false)
         .setPositiveButton("Ja",
              new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int id) {
                      Intent callGPSSettingIntent = new Intent(
                                            android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                            startActivity(callGPSSettingIntent);
              }
         });
         alertDialogBuilder.setNegativeButton("Nej",
              new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int id) {
                   dialog.cancel();
              }
         });
    AlertDialog alert = alertDialogBuilder.create();
    alert.show();
    }
}

10-24 20:02:15.392: I/dalvikvm(2296): threadid=3: reacting to signal 3
10-24 20:02:15.502: I/dalvikvm(2296): Wrote stack traces to '/data/anr/traces.txt'
10-24 20:02:15.783: I/dalvikvm(2296): threadid=3: reacting to signal 3
10-24 20:02:15.902: I/dalvikvm(2296): Wrote stack traces to '/data/anr/traces.txt'
10-24 20:02:16.282: I/dalvikvm(2296): threadid=3: reacting to signal 3
10-24 20:02:16.402: I/dalvikvm(2296): Wrote stack traces to '/data/anr/traces.txt'
10-24 20:02:16.893: I/dalvikvm(2296): threadid=3: reacting to signal 3
10-24 20:02:17.193: I/dalvikvm(2296): Wrote stack traces to '/data/anr/traces.txt'
10-24 20:02:17.363: I/dalvikvm(2296): threadid=3: reacting to signal 3
10-24 20:02:17.463: I/dalvikvm(2296): Wrote stack traces to '/data/anr/traces.txt'
10-24 20:02:17.695: D/gralloc_goldfish(2296): Emulator without GPU emulation detected.
10-24 20:02:17.854: I/dalvikvm(2296): threadid=3: reacting to signal 3
10-24 20:02:18.052: I/dalvikvm(2296): Wrote stack traces to '/data/anr/traces.txt'
10-24 20:02:24.842: I/SqliteDatabaseCpp(2296): sqlite returned: error code = 14, msg = cannot open file at line 27701 of [8609a15dfa], db=/data/data/com.example.wrn/databases/webview.db
10-24 20:02:24.842: I/SqliteDatabaseCpp(2296): sqlite returned: error code = 14, msg = os_unix.c: open() at line 27701 - "" errno=2 path=/CachedGeoposition.db, db=/data/data/com.example.wrn/databases/webview.db

提前致谢。

4

0 回答 0