我想要的WebView在Android中,但后退按钮功能不能正常工作? [英] I want to make webview in android, but back button function not work?

查看:109
本文介绍了我想要的WebView在Android中,但后退按钮功能不能正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在制作机器人的WebView,我是在begineer机器人。下面我code和我面临着以下问题。结果


  1. 后退按钮无法正常工作,当我拜访我的应用程序和preSS的不同页面后退按钮则其无法正常工作,并给予错误的应用已采空,收回了我手机的主屏幕上。


  2. 第二,我想,当任何用户打开该应用程序,然后,如果没有连接互联网或则没有工作的给予警告有些像此应用程序需要连接互联网,并在后台默认我给背景表演wihotu互联网连接。如果互联网连接,然后找到这个程序做工精细。结果


我在Android的新的。事先我对这种帮助非常感谢。并请您的answere写code为解决以下问题。在此先感谢

 包com.example.edarabia;进口android.os.Bundle;
进口android.app.Activity;
进口android.view.Menu;
进口android.webkit.WebView;
进口android.webkit.WebSettings;
进口android.webkit.WebViewClient;
进口android.annotation.Sup pressLint;
进口android.view.KeyEvent;
@燮pressLint(SetJavaScriptEnabled)公共类MainActivity延伸活动{
的WebView mywebview;
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    的WebView mywebview =(的WebView)findViewById(R.id.webview);
        mywebview.loadUrl(http://www.grafdom.com/operations/projects/ma/edarabiaapp/);
    WebSettings webSettings = mywebview.getSettings();
    webSettings.setJavaScriptEnabled(真);
    webSettings.setBuiltInZoomControls(真);
    mywebview.setWebViewClient(新WebViewClient());
}@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
    //充气菜单;如果是present这增加了项目操作栏。
    。getMenuInflater()膨胀(R.menu.activity_main,菜单);
    返回true;
}@覆盖
公共布尔的onkeydown(INT键code,KeyEvent的事件){
    //检查关键事件是后退按钮,如果有历史
    如果((键code == KeyEvent.KEY code_BACK)及和放大器; mywebview.canGoBack()){
        mywebview.goBack();
        返回true;
    }    返回super.onKeyDown(键code,事件);
}}


解决方案

您还没有initalized主的WebView web视图; 和你是指,在你的 onBack pressed()方法,这就是为什么它给你的错误。

就在您的文件,如下修改:


 公共类MainActivity延伸活动{
    的WebView mywebview; << ---而不是你在你的onCreate宣布的WebView使用mywebview。
   @覆盖
  保护无效的onCreate(捆绑savedInstanceState){
     super.onCreate(savedInstanceState);
     的setContentView(R.layout.activity_main);
     mywebview =(的WebView)findViewById(R.id.webView1);
     mywebview.loadUrl(http://www.grafdom.com/operations/projects/ma/edarabiaapp/);
     WebSettings webSettings = mywebview.getSettings();
     webSettings.setJavaScriptEnabled(真);
     webSettings.setBuiltInZoomControls(真);
     mywebview.setWebViewClient(新WebViewClient());
 }
 @覆盖
 公共布尔onCreateOptionsMenu(菜单菜单){
     //充气菜单;如果是present这增加了项目操作栏。
     。getMenuInflater()膨胀(R.menu.activity_main,菜单);
     返回true;
 }
 @覆盖
 公共无效onBack pressed(){
 如果(mywebview.canGoBack())mywebview.goBack();
    否则super.onBack pressed();
 }
 }


有关你的第二个查询:

您可以使用下面的方法检查您的网络连接:


 公共静态布尔IsNetConnected()
{
    布尔m_netConnected = FALSE;
    尝试
    {
        ConnectivityManager m_connectivity =(ConnectivityManager)AppConstants.m_context.getSystemService(Context.CONNECTIVITY_SERVICE);
        如果(m_connectivity == NULL)
        {
            Log.w(TAG,无法获得连接管理器);
            m_netConnected = FALSE;
        }
        其他
        {
            的NetworkInfo [] = m_info m_connectivity.getAllNetworkInfo();
            如果(m_info!= NULL)
            {
                的for(int i = 0; I< m_info.length;我++)
                {
                    如果(m_info [I] .getState()== NetworkInfo.State.CONNECTED)
                    {
                        m_netConnected = TRUE;
                    }
                }
            }
        }
    }
    赶上(Throwable的E)
    {
        。ErrorReporter.getInstance()handleException(E);
        如果(AppConstants.DEBUG)
        {
            Log.e(TAGIsNetConnected错误+ e.toString(),E);
            }
        m_netConnected = FALSE;
    }
    返回m_netConnected;
}


您可以用上面的方法如下:


 如果(!IsNetConnected())
    {
   的ShowDialog(MainActivity.this,该应用程序需要连接互联网);
     //设置默认的背景按您的要求。
   }


i am making webview in android, i am begineer in android. Below my code and i am facing following problems.

  1. Back button not work , when i visiting different pages of my application and press back button then its not work and give error application has been stoped and take back me on home screen of my mobile.

  2. 2nd i want when any user open this application then if internet not connected or not working then its give the alert some like "This application required internet connection" and in background by default my given background show wihotu internet connection. If internet connection found then this app work fine.

I am new in android. In advance i am very thankful for this help. and please in your answere write the code for fix the below issues. Thanks in advance

package com.example.edarabia;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.webkit.WebView;
import android.webkit.WebSettings;
import android.webkit.WebViewClient;
import android.annotation.SuppressLint;
import android.view.KeyEvent;


@SuppressLint("SetJavaScriptEnabled")

public class MainActivity extends Activity {
WebView mywebview;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    WebView mywebview = (WebView) findViewById(R.id.webview);
        mywebview.loadUrl("http://www.grafdom.com/operations/projects/ma/edarabiaapp/");        
    WebSettings webSettings = mywebview.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setBuiltInZoomControls(true);
    mywebview.setWebViewClient(new WebViewClient());
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // Check if the key event was the Back button and if there's history
    if ((keyCode == KeyEvent.KEYCODE_BACK) && mywebview.canGoBack()) {
        mywebview.goBack();
        return true;
    }

    return super.onKeyDown(keyCode, event);
}



}

解决方案

You have not initalized the main WebView webView; and you are referring that in your onBackPressed() method thats why its giving you error.

Just change as below in your file:

public class MainActivity extends Activity {
    WebView mywebview;   <<--- instead of webview use mywebview which you have declared in your onCreate.
   @Override
  protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     mywebview = (WebView) findViewById(R.id.webView1);
     mywebview.loadUrl("http://www.grafdom.com/operations/projects/ma/edarabiaapp/");        
     WebSettings webSettings = mywebview.getSettings();
     webSettings.setJavaScriptEnabled(true);
     webSettings.setBuiltInZoomControls(true);
     mywebview.setWebViewClient(new WebViewClient());
 }
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
     // Inflate the menu; this adds items to the action bar if it is present.
     getMenuInflater().inflate(R.menu.activity_main, menu);
     return true;
 }
 @Override
 public void onBackPressed (){
 if(mywebview.canGoBack()) mywebview.goBack();
    else super.onBackPressed();  
 }
 }

For your second query:

You can check your internet connection using the below method:

 public static boolean IsNetConnected()
{
    boolean m_netConnected = false;
    try
    {
        ConnectivityManager m_connectivity = (ConnectivityManager) AppConstants.m_context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (m_connectivity == null)
        {
            Log.w(TAG, "couldn't get connectivity manager");
            m_netConnected = false;
        }
        else
        {
            NetworkInfo[] m_info = m_connectivity.getAllNetworkInfo();
            if (m_info != null)
            {
                for (int i = 0; i < m_info.length; i++)
                {
                    if (m_info[i].getState() == NetworkInfo.State.CONNECTED)
                    {
                        m_netConnected = true;
                    }
                }
            }
        }
    }
    catch (Throwable e)
    {
        ErrorReporter.getInstance().handleException(e);
        if (AppConstants.DEBUG)
        {
            Log.e(TAG, "IsNetConnected Error" + e.toString(), e);
            }
        m_netConnected = false;
    }
    return m_netConnected;
}

You can use the above method as below :

  if(!IsNetConnected())
    {
   showDialog(MainActivity.this,"This application required internet connection");
     //set your default background as per your requirements.
   }

这篇关于我想要的WebView在Android中,但后退按钮功能不能正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆