【发布时间】:2018-04-30 00:14:39
【问题描述】:
我有一个巨大的 htm 文件文件夹和一个带有指向每个 htm 文件的 href 链接的 html 文件。我想创建一个供个人使用的 android 应用程序,以便在平板电脑上更轻松地浏览这些文件。
我设法将主 html 文件加载到 Android Studio 中的 WebView,但单击任何 URL 会导致崩溃,因此我无法通过我的 html 目录加载任何 htm 文件。
我在android编程方面没有经验,从html和一些css就足够的日子开始,我只有一些过时的网站创建经验,所以我尝试使用我的直觉。我可能遗漏了一些非常明显的东西,但我没有设法解决我的问题。
也许你能帮助我?是否可以通过 WebView 中的 href 在 html 文件之间进行重定向?或者也许有一种简单的方法可以绕过它?不幸的是,从头开始编码目录是不可能的 - 有成千上万的 htm 文件。欢迎提出任何建议。
编辑: 我的代码如下:
MainActivity.java
package a.my.app4;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import java.nio.channels.WritableByteChannel;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webz=(WebView)findViewById(R.id.web1);
webz.loadUrl("file:///android_asset/index.html");
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<WebView
android:id="@+id/web1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="0dp" />
</RelativeLayout>
index.html 只是正文部分中的一个巨大链接列表。它已正确加载到 WebView,即使来自 css 文件的简单格式也是正确的,但单击链接会导致崩溃。有成千上万行这样的:
<a href="view-1.htm" title="file description">file title</a>
我也尝试过以这种方式更改它(这会导致数千行出现问题......)但它也不起作用:
<a href="file:///android_asset/view-1.htm" title="file description">file title</a>
所有文件都在 assets 文件夹中。
【问题讨论】:
-
您可以在尝试加载 html 的位置添加代码吗?这将是让 SO 上的任何人获得更清晰的图片并最终帮助您获得想法的起点
-
@Sagar 谢谢你的建议,代码就在那里。
-
能否添加崩溃日志?
标签: android html android-studio webview