Android 特定页面的Webview功能不正常
我试图在我的应用程序中实现webview,但我面临两个问题 -进度条(加载未显示) -Webview不适用于此特定url()适用于其他url其工作正常当我在chrome或其他浏览器中打开此url()时,此url()工作正常 这是我的MainActivity.javaAndroid 特定页面的Webview功能不正常,android,Android,我试图在我的应用程序中实现webview,但我面临两个问题 -进度条(加载未显示) -Webview不适用于此特定url()适用于其他url其工作正常当我在chrome或其他浏览器中打开此url()时,此url()工作正常 这是我的MainActivity.java import android.content.DialogInterface; import android.graphics.Bitmap; import android.support.v7.app.AlertDialog; i
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new myWebClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("https://www.bastibazar.com/");
getSupportActionBar().hide();
webView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView webView, int errorCode, String description, String failingUrl) {
try {
webView.stopLoading();
} catch (Exception e) {
}
if (webView.canGoBack()) {
webView.goBack();
}
webView.loadUrl("about:blank");
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("No Internet Connection");
alertDialog.setMessage("Check your internet connection and try again.");
alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Try Again", new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
startActivity(getIntent());
}
});
alertDialog.show();
super.onReceivedError(webView, errorCode, description, failingUrl);
}
});
}
public class myWebClient extends WebViewClient {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
@Override
// This method is used to detect back button
public void onBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
} else {
// Let the system handle the back button
super.onBackPressed();
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
>
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
在manifest.xml中,我添加了
<uses-permission android:name="android.permission.INTERNET" />
有谁能建议我做些什么更改,以便进度栏和网络视图的功能都能在我的应用程序中显示出来,提前谢谢 XML:
<?xml version="1.0" encoding="utf-8"?>
<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">
<ProgressBar
android:id="@+id/progressBar"
android:max="3"
android:progress="100"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerInParent="true" />
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/progressBar"
android:layout_centerHorizontal="true" />
</RelativeLayout>
androidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="app.com.sample">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
试试这个也许对你有帮助
自行更改URL
谢谢!快乐编码 XML:
<?xml version="1.0" encoding="utf-8"?>
<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">
<ProgressBar
android:id="@+id/progressBar"
android:max="3"
android:progress="100"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerInParent="true" />
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/progressBar"
android:layout_centerHorizontal="true" />
</RelativeLayout>
androidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="app.com.sample">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
试试这个也许对你有帮助
自行更改URL
谢谢!快乐编码 一开始它对我也不起作用 读这个
webView.getSettings().setDomStorageEnabled(true) 一开始它对我也不起作用 读这个
webView.getSettings().setDomStorageEnabled(true) 谢谢,但我已经看到了开发者页面,但没有得到任何解决方案OK!让我为您编写代码,您希望webview使用progressBar,对吗?是的,当然,也许我遗漏了一些东西,进度条起作用了,但正如前面提到的,对于这个特定的url,代码仍然不起作用,我以前的代码在其他url(如google)上运行良好,但对于这个url()使用旧代码更改webView代码并保留与现在相同的progressBar代码是不起作用的。谢谢,但我已经看到了开发人员页面,但没有得到任何解决方案OK!让我为您编写代码,您希望webview使用progressBar,对吗?是的,当然,也许我遗漏了一些东西,进度条起作用了,但正如前面提到的,对于这个特定的url,代码仍然不起作用,我以前的代码在其他url(如google)上运行良好,但对于这个url()用旧代码更改webView代码并保留与现在相同的progressBar代码是不起作用的。