xiaoyan_12 2013-09-12 08:53 采纳率: 25%
浏览 1682

在shouldOverrideUrlLoading中加载intent

我想在 shouldOverrideUrlLoading 中加载一个 intent。
VariablesStorage.class

public class VariablesStorage
{
private static VariablesStorage instance;
public static  Context webViewContext;
public  WebView webView;

        public static void initInstance()
    {
        if (instance == null)
        {
            // Create the instance
            instance = new VariablesStorage();
        }
    }
    public static VariablesStorage getInstance()
    {
        // Return the instance
        return instance;
    }
    private VariablesStorage()
    {
    }
public void loadWebView() {
        webView.getSettings().setJavaScriptEnabled(true);
 if (isOnline())
        {
            webView.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {

                    if (url.startsWith("inapp://")) {
                         Intent intent = new Intent(this,profilepictureview.class);
                         intent.putExtra("img",Uri.parse(url).getHost().toString());
                         startActivity(intent);

                        }else
                        {
                            view.getContext().startActivity(
                             new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                        }                       
                    return true;
                }
                @Override
                public void onReceivedError( WebView view, int errorCode, String description, String failingUrl) 
                    {
                    }
                @Override
                public void onPageFinished(WebView view, String url) {
                    if(mProgress.isShowing()) {
                        mProgress.dismiss();
                    }
                }
            });
            webView.loadUrl(Url);
        }else
        {
            webView.loadData(customHtml, "text/html;charset=utf-8", "UTF-8");
        }
    }
}

然后在 webview activity 中调用 VariablesStorage.getInstance().loadWebView();
WebViewActivity.class

public class WebViewActivity extends Activity {

    SharedPreferences pref;
    TextView textView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webviewtab);
 VariablesStorage.getInstance().webViewContext = this;
        VariablesStorage.getInstance().webView = (WebView) findViewById(R.id.webView);
            VariablesStorage.getInstance().loadWebView();
}

当我在 shouldOverrideUrlLoading 里面添加代码时:

Intent intent = new Intent(this,profilepictureview.class);
 intent.putExtra("img",Uri.parse(url).getHost().toString());
 startActivity(intent);

出现错误:

The constructor Intent(new WebViewClient(){}, Class<profilepictureview>) is undefined

如何实现这个功能呢?

  • 写回答

1条回答 默认 最新

  • ChatGPTᴼᴾᴱᴺᴬᴵ 2023-02-10 10:57
    关注

    该回答引用ChatGPT

    在这种情况下,错误发生是因为当前上下文不是 Activity 而是一个匿名内部类(即 WebViewClient)。因此,您不能使用“this”来构造 Intent,您应该使用 webViewContext 上下文。


    更改代码:

    Intent intent = new Intent(VariablesStorage.getInstance().webViewContext, profilepictureview.class);
    intent.putExtra("img",Uri.parse(url).getHost().toString());
    VariablesStorage.getInstance().webViewContext.startActivity(intent);
    

    这样就可以在 shouldOverrideUrlLoading 中加载 Intent 了。

    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料