Android Webview无法从按钮监听器加载URL。

huangapple go评论82阅读模式
英文:

Android Webview doesn't load the Url from button listener

问题

    WebView wbvCheckURL;
    Button btnSearch;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        wbvCheckURL = findViewById(R.id.wbvCheckURL);
        btnSearch = findViewById(R.id.btnSearch);

        wbvCheckURL.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                ...
            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                ...
            }
        });
        wbvCheckURL.getSettings().setJavaScriptEnabled(true);
        wbvCheckURL.setWebViewClient(new WebViewClient());

        // When i do it like this it works:
        // wbvCheckURL.loadUrl("http://www.google.de");

        btnSearch.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                wbvCheckURL.loadUrl("http://www.google.de");

                hideKeyboard(v);
                LoadCode();
            }
        });
    }
英文:

It loads the site but only when it's not in the Action Listener.
Even if I load the url way down in the onCreate method it works.
I already checked if the Webview is invisible, the button listener works also.

The problem began after a few hours of coding other things

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

WebView wbvCheckURL;
Button btnSearch;

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        wbvCheckURL = findViewById(R.id.wbvCheckURL);
        btnSearch = findViewById(R.id.btnSearch);
        
        wbvCheckURL.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                ...
            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                ...
            }
        });
        wbvCheckURL.getSettings().setJavaScriptEnabled(true);
        wbvCheckURL.setWebViewClient(new WebViewClient());
        
        // When i do it like this it works:
        // wbvCheckURL.loadUrl(&quot;http://www.google.de&quot;);

        btnSearch.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                wbvCheckURL.loadUrl(&quot;http://www.google.de&quot;);

                hideKeyboard(v);
                LoadCode();
            }
        });
    }

<!-- end snippet -->

答案1

得分: 0

解决方案是在线程中加载它

> 创建实例并启动线程:

LoadURL t = new LoadURL();
t.start();

> 线程类:

class LoadURL extends Thread {
public void run() {
    Classname.wbvCheckURL.post(new Runnable() {
           public void run() {
               Classname.wbvCheckURL.loadUrl("http://www.google.de");
           }
    });
}
英文:

The Solution is to load it in a Thread

> Create instance and start the thread:

LoadURL  t = new LoadURL();
t.start();

> Thread class:

class LoadURL extends Thread {
public void run() {
    Classname.wbvCheckURL.post(new Runnable() {
           public void run() {
               Classname.wbvCheckURL.loadUrl(&quot;http://www.google.de&quot;);
           }
    });
}

答案2

得分: 0

你可以尝试使用我的代码。我的代码中包含另一种带有抽屉布局(Drawer Layout)的 Web 视图(WebView)。希望你能理解如何插入 Web 视图。

package com.example.appname;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageButton;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

public class Provokinc extends AppCompatActivity {

    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_provokinc);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        webView = findViewById(R.id.webView);
        webView.setWebViewClient(new WebViewClient());
        webView.loadUrl("https://stackoverflow.com/questions/63438455/android-webview-doesnt-load-the-url-from-button-listener");

        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        ImageButton ImageButton = findViewById(R.id.imageButtonA);
        ImageButton ImageButton1 = findViewById(R.id.imageButtonB);
        ImageButton ImageButton2 = findViewById(R.id.imageButtonC);
        ImageButton ImageButton3 = findViewById(R.id.imageButtonD);
        ImageButton ImageButton4 = findViewById(R.id.imageButtonE);

        ImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent int1 = new Intent(Provokinc.this, Main3Activity.class);
                startActivity(int1);
            }
        });

        ImageButton1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent int2 = new Intent(Provokinc.this, Main2Activity.class);
                startActivity(int2);
            }
        });

        ImageButton2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent int3 = new Intent(Provokinc.this, Provokinc.class);
                startActivity(int3);
            }
        });

        ImageButton3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent int4 = new Intent(Provokinc.this, ActivityItemDetails.class);
                startActivity(int4);
            }
        });

        ImageButton4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent int5 = new Intent(Provokinc.this, MainActivity.class);
                startActivity(int5);
            }
        });
    }

    @Override
    public void onBackPressed(){
        if(webView.canGoBack()){
            webView.goBack();
        } else{
            super.onBackPressed();
        }
    }
}
英文:

you can try mine. Mine is another kind of webview with Drawer Layout. Hope you get the idea on how to insert the webview.

package com.example.appname;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageButton;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.ShareActionProvider;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.MenuItemCompat;
public class Provokinc extends AppCompatActivity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_provokinc);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
webView = findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl(&quot;https://stackoverflow.com/questions/63438455/android-webview-doesnt-load-the-url-from-button-listener&quot;);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
ImageButton ImageButton = (ImageButton) findViewById(R.id.imageButtonA);
ImageButton ImageButton1 = (ImageButton) findViewById(R.id.imageButtonB);
ImageButton ImageButton2 = (ImageButton) findViewById(R.id.imageButtonC);
ImageButton ImageButton3 = (ImageButton) findViewById(R.id.imageButtonD);
ImageButton ImageButton4 = (ImageButton) findViewById(R.id.imageButtonE);
ImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent int1 = new Intent(Provokinc.this,Main3Activity.class);
startActivity(int1);
}
});
ImageButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent int2 = new Intent(Provokinc.this, Main2Activity.class);
startActivity(int2);
}
});
ImageButton2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent int3 = new Intent(Provokinc.this, Provokinc.class);
startActivity(int3);
}
});
ImageButton3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent int4 = new Intent(Provokinc.this, ActivityItemDetails.class);
startActivity(int4);
}
});
ImageButton4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent int5 = new Intent(Provokinc.this, MainActivity.class);
startActivity(int5);
}
});
}
@Override
public void onBackPressed(){
if(webView.canGoBack()){
webView.goBack();
}                 else{
super.onBackPressed();
}
}
}

huangapple
  • 本文由 发表于 2020年8月16日 23:09:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63438455.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定