Search code examples
androidwebviewonloadonload-eventwebviewclient

Error in identifying url by clicking on it


I have 3 pages, my webview takes me to page 1, in page 1 you can go to page 2 or 3, I'm trying to identify if you go to page 2 or 3 from page 1. I'm trying to perform an action type, Toast or Intent in my WebView by clicking on a link, but in method to make the Toast onLoadResource send me the error "The method Maketext (Context, CharSequence, int) in the type Toast is not applicable for the arguments (Class , String, int)" I can do about it to fix this?

public class WebViewTest extends Activity{
WebView site;
String webUrl;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.coursetest);

    site = (WebView) findViewById(R.id.wvcurse);
    site.loadUrl("http://wstest.comuf.com/test/webviewtest.html");

    site.getSettings().setJavaScriptEnabled(true);
    site.getSettings().setLoadWithOverviewMode(true);
    site.getSettings().setUseWideViewPort(false);
    site.setWebViewClient(new ViewClient());}}

my ViewClient.java class is;

    public class ViewClient extends WebViewClient {
    @Override
public boolean shouldOverrideUrlLoading(WebView v, String url){
    super.shouldOverrideUrlLoading(v, url);
    v.loadUrl(url); 

    return true;    
}
    @Override
    public void onLoadResource(WebView  v, String  url){
        super.onLoadResource(v, url);
        if( url.equals("http://www.wstest.comuf.com/test/page_two.html") ){

            Toast t=Toast.makeText(WebViewTest.class,"passed", Toast.LENGTH_LONG);
            t.show();

           // Intent i = new Intent("com.mariposatraining.courses.lay_main");
          //startActivity(i); here too i have the error "The method startActivity(Intent) is undefined for the type ViewClient"
        }
        if( url.equals("http://www.wstest.comuf.com/test/page_three.html") ){

            Toast t=Toast.makeText(WebViewTest.class,"failed", Toast.LENGTH_LONG);
            t.show();
        }                       
}}

How should I make toast or other activity work here? Really would appreciate your help.


Solution

  • The method signature of Toast.makeText that you're trying to use is:

    public static Toast makeText (Context context, CharSequence text, int duration)

    WebViewTest.class is not a Context.