简体   繁体   English

对于类型MyWebViewClient,未定义方法startActivity(Intent)

[英]The method startActivity(Intent) is undefined for the type MyWebViewClient

I'm trying to use the Android tutorial to build an app that loads a web view to a mobile site that I built. 我正在尝试使用Android教程来构建将Web视图加载到我构建的移动网站的应用程序。 The problem is with following the tutorial the startActivity function is undefined and the Android tutorial isn't helping. 问题在于遵循本教程, startActivity函数未定义,而Android教程则无济于事。 I've done Ctrl+Shift+O to verify all the proper modules are loaded. 我已经完成Ctrl + Shift + O来验证是否已加载所有正确的模块。

package com.mysite;

import android.content.Intent;
import android.net.Uri;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (Uri.parse(url).getHost().equals("www.mysite.com")) {
            // This is my web site, so do not override; let my WebView load the page
            return false;
        }
        // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(intent);
        return true;
    }
}

Update 更新

Ok, now my code reads: 好的,现在我的代码显示为:

package com.myapp;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;

public class MyApp extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //init webview
        WebView DCWebView = (WebView) findViewById(R.id.webview);
        WebSettings webViewSettings = DCWebView.getSettings();

        //when a link is clicked, use the WebView instead of opening a new browser
        DCWebView.setWebViewClient(new MyWebViewClient() {
            @Override
            public void launchExternalBrowser(String url) {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(intent);
            }
        });

        //enable javascript
        webViewSettings.setJavaScriptEnabled(true);

    }
}

But I'm showing 2 errors: 但我显示了2个错误:

Description Resource    Path    Location    Type
The type new MyWebViewClient(){} must implement the inherited abstract method MyWebViewClient.launchExternalBrowser()   DealClippings.java  /MyApp/src/com/myapp    line 21 Java Problem

The method launchExternalBrowser(String) of type new MyWebViewClient(){} must override or implement a supertype method  MyApp.java  /DealClippings/src/com/myapp    line 23 Java Problem

There really is no startActivity method for WebViewClient . WebViewClient确实没有startActivity方法。 You can check the docs . 您可以检查文档 You'll have to signal the Context (probably your Activity ) to execute those lines of code instead. 您必须发信号给Context (可能是Activity )来执行这些代码行。 There are many possible approaches including adding listeners or simply calling an abstract method which you implement in an anonymous instance of this class when setting the WebViewClient of your WebView in your Activity. 有许多可能的方法,包括添加侦听器或简单地调用在Activity中设置WebViewWebViewClient时在此类的匿名实例中实现的抽象方法。

For example: 例如:

public abstract class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (Uri.parse(url).getHost().equals("www.mysite.com")) {
            // This is my web site, so do not override; let my WebView load the page
            return false;
        }

        launchExternalBrowser(url);            
        return true;
    }

    public abstract void launchExternalBrowser(String url);
}

And then in your activity: 然后在你的活动中:

WebViewClient client = new MyWebViewClient() {
    @Override
    public void launchExternalBrowser(String url) {
         Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
         startActivity(intent);
    }
};

Although I'm not sure why you want this behavior exactly, but it should work more or less. 尽管我不确定为什么要确切地想要这种行为,但是它应该或多或少起作用。

I don't know if there's much point answering your edit now, 18 months on, but it seems like this question gets a bit of traffic so I'll post this here for posterity. 18个月后,我不知道现在是否有很多答案可以回答您的编辑,但是似乎这个问题有点麻烦,因此我将其张贴在这里以供后代参考。

From your errors, it sounds like you haven't provided an argument to the abstract method in the abstract class definition of MyWebViewClient. 从您的错误看来,您好像没有在MyWebViewClient的抽象类定义中为abstract方法提供参数。 That is, you have this: 也就是说,您有:

public abstract void launchExternalBrowser();

when you should have this: 什么时候应该有这个:

public abstract void launchExternalBrowser(String url);

The cause of the error is that Java treats two methods with the same name but different arguments as two distinct methods. 错误的原因是Java将两个名称相同但参数不同的方法视为两个不同的方法。 So launchExternalBrowser(String) is a different method to launchExternalBrowser() . 因此, launchExternalBrowser(String)是不同于launchExternalBrowser()方法。

Hope this helps someone! 希望这有助于某人!

To answer original question before your edit.. 在编辑之前要回答原始问题。

Had the same problem, and figured out that the MyWebViewClient is meant to be an inner class inside the activity. 遇到了同样的问题,并发现MyWebViewClient应该是活动内部的一个内部类。

    package com.myapp;

    import android.app.Activity;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Bundle;
    import android.webkit.WebSettings;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;

    public class MyApp extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            //init webview
            WebView DCWebView = (WebView) findViewById(R.id.webview);
            WebSettings webViewSettings = DCWebView.getSettings();

            //when a link is clicked, use the WebView instead of opening a new browser
            DCWebView.setWebViewClient(new MyWebViewClient());

            //enable javascript
            webViewSettings.setJavaScriptEnabled(true);

        }

        private class MyWebViewClient extends WebViewClient {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (Uri.parse(url).getHost().equals("www.mysite.com")) {
                    // This is my web site, so do not override; let my WebView load the page
                    return false;
                }
                // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(intent);
                return true;
            }
        }
    }

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

相关问题 Android对于new View.OnClickListener(){}类型,未定义方法startActivity(Intent) - Android The method startActivity(Intent) is undefined for the type new View.OnClickListener(){} 无法从OnClickListener启动Activity“该类型的方法startActivity(Intent,int)未定义” - Can't start Activity from OnClickListener “method startActivity(Intent, int) is undefined for the type” 找不到适用于startActivity(intent)方法的适用方法 - no sutiable method found for startActivity(intent) method 错误:“无法从类型Activity中对非静态方法startActivity(Intent)进行静态引用” - Error: “Cannot make a static reference to the non-static method startActivity(Intent) from the type Activity” startActivity(intent)方法调用预期错误 - startActivity(intent) method call expected error Fragment类在intent的startactivity方法中显示错误 - Fragment class showing error in intent's startactivity method 找不到符号方法startActivity(android.content.Intent) - Cannot find symbol method startActivity(android.content.Intent) 如何从另一个类调用带有startActivity(Intent)的方法? - how can i call a method with a startActivity(Intent) from another class? Android上的startActivity(intent)无法正常工作 - startActivity(intent) on Android not working 意向性在startactivity时崩溃 - Intent crashes on startactivity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM