简体   繁体   English

我想开发浏览器应用程序,但出现错误

[英]I want to develop browser app but I am getting errors

I am new to Android I want to develop a browser app now I am trying to develop it, in this I am getting errors.我是 Android 新手我想开发一个浏览器应用程序,现在我正在尝试开发它,在这方面我遇到了错误。 I tried in another method also on that it's asking on which browser you have to open like that but for me I want to open in my app only.我还尝试了另一种方法,它询问您必须像那样打开哪个浏览器,但对我来说,我只想在我的应用程序中打开。

WebviewActivity.java WebviewActivity.java

package com.example.admin.fristapp;

import android.app.Activity;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class WebviewActivity extends Activity {
    WebView webview1;
   // String url = "http://www.google.com";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);
        webview1 = (WebView) findViewById(R.id.webview1);
      //  webview1.loadUrl(url);

        //MyBrowser my ;

        new Background().execute();


    }

    public class MyBrowser extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }




    public class Background extends AsyncTask<String,String,String>
    {
        @Override
        protected void onPostExecute(String s)
        {
            super.onPostExecute(s);
        }
        @Override
        protected void onPreExecute()
        {
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String... strings)
        {
            search();
            return null;
        }
    }
    public void search()
    {
        webview1.setWebViewClient(new WebviewActivity.MyBrowser());
        String url = "http://www.google.com";
        webview1.getSettings().setJavaScriptEnabled(true);
        webview1.loadUrl(url);
    }
}

logcat日志猫

java.lang.Throwable: A WebView method was called on thread 'AsyncTask #4'. All WebView methods must be called on the same thread. (Expected Looper Looper (main, tid 1) {180a245b} called on null, FYI main Looper is Looper (main, tid 1) {180a245b})
                                                                            at android.webkit.WebView.checkThread(WebView.java:2290)

Afteredit后编辑

import android.app.Activity;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class WebviewActivity extends Activity {
    WebView webview;
    String url = "http://www.google.com";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);

        webview =(WebView )findViewById(R.id.webview1);
                //WebView(WebviewActivity.this);
        webview.loadUrl(url);

        webview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return false;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);

                // Do your code when page finished
            }
        });

Your are loading a url in WebView in doInBackground() method.您正在使用doInBackground()方法在WebView中加载 url。 You should do on the UI thread.你应该在 UI 线程上做。

loadUrl() is already doing everything asynchronously. loadUrl()已经在异步执行所有操作。 So no need to of AsyncTask .所以不需要AsyncTask Move your code in onCreate() method.onCreate()方法中移动您的代码。

If you want to display a loading indicator, you need to use as below :如果你想显示一个加载指示器,你需要使用如下:

WebView webview = new WebView(MainActivity.this);

webview.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {    
        view.loadUrl(url);    
         return false;
    }

    @Override
    public void onPageFinished(WebView view, String url) {
           super.onPageFinished(view, url);

           // Do your code when page finished
     }
});

webview.loadUrl(url);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ProGuard,出现错误 - ProGuard, I am getting errors 我正在使用 flutter 编写 Firebase 应用程序,但我收到有关 google-services.json 和初始化的错误 - I am coding a Firebase app with flutter but i am getting errors about google-services.json and inititilization 我正在尝试运行我的 flutter 应用程序,但出现这些错误? - I am trying to run my flutter app and I am getting these errors? 当我在浏览器中并单击链接时,我想在操作列表中查看我的应用程序,然后使用我的应用程序转到该链接 - When I am in a browser and I click on a link I want see my app in an action list and I go to the link using my app 编译时出现这些错误 - I am getting these errors while compiling 为什么会出现这些语法错误? - Why am I getting these syntax errors? 我的计时器应用程序中出现很多错误。 他们中的大多数似乎是因为资源是私有的 - I am getting a lot of Errors in my Timer App. Most of them seem to be because the resource is private 我想使用印度字体在Android中开发一个应用,也想在对话框中显示印度字体 - I want to develop an app in android using indic font, also want to show indic font in dialog box 我想测试&#39;Crittercism&#39;工具,我怎么能为android开发一个崩溃的应用程序? - I want to test 'Crittercism' tool, how can i develop a crashing app for android? 我正在制作一个应用程序,想知道何时卸载该应用程序 - I am making an app and want to know when the app is uninstalling
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM