بروز خطا
[message]
اشتراک در سوال
رای ها
[dataList]
مشکل با برگشت در webview
سلام میخوام با استفاده از دکمه back روی گوشی در webview به عقب برگردم و در نهایت که به صفحه اول برگشت با زدن یه بار دگ دکمه back از webview خارج بشه
کدش رو نوشتم فقط یه مشکل دارم وقتی اتصال به اینترنت ندارم و دکمه بک رو میزنم برنامه کرش میکنه!!!! واقعا نمیدونم دلیلش چیه :(((
package r.ali.webview;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class WebviewActivity extends Activity {
private WebView webview;
private static final String TAG = "Main";
private ProgressDialog progressBar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
if (isNetworkAvailable(getApplicationContext()))
{
loadingUrl();
}
else {
Toast.makeText(getApplicationContext(), "connection lost", 3000).show();
}
}
/*----------------------------------------------------------------------------------------------------------*/
private void loadingUrl() {
this.webview = (WebView) findViewById(R.id.webview);
WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
progressBar = ProgressDialog.show(WebviewActivity.this, "WebView Example", "Loading...");
webview.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i(TAG, "Processing webview url click...");
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
Log.i(TAG, "Finished loading URL: " + url);
if (progressBar.isShowing()) {
progressBar.dismiss();
}
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.e(TAG, "Error: " + description);
Toast.makeText(WebviewActivity.this, "Oh no! " + description, Toast.LENGTH_SHORT).show();
alertDialog.setTitle("Error");
alertDialog.setMessage(description);
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
return;
}
});
alertDialog.show();
}
});
webview.loadUrl("http://www.google.com");
}
/*----------------------------------------------------------------------------------------------------------*/
@Override
public void onBackPressed()
{
if (webview.isFocused() && webview.canGoBack())
{
webview.goBack();
}
else
{
super.onBackPressed();
finish();
}
}
/*----------------------------------------------------------------------------------------------------------*/
public boolean isNetworkAvailable(Context context) {
if (context == null) {
return false;
}
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
// if no network is available networkInfo will be null, otherwise check if we are connected
try {
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) {
return true;
}
}
catch (Exception e) {
Log.i(TAG, "isNetworkAvailable()");
}
return false;
}
/*----------------------------------------------------------------------------------------------------------*/
}
اینم کد xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
0
0
بخش های اضافی کد را حذف کنید و خطا های مربوطه را درج کنید (10 سال پیش)
برای این سوال پاسخی وجود ندارد.
پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .