0

I have a real big problem implementing loading a intent from inside shouldOverrideUrlLoading. I believe it has to do with the way i have structured my application.

Here is my code: 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");
        }


    }


}

and my webview activity where i call 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();
}

So when i am adding this code inside shouldOverrideUrlLoading

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

i am getting this error:

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

Any help implementing this one? Thanks.

4

2 回答 2

2

As the error says, Intent doesn't have a constructor that takes WebViewClient(){} as a parameter which is what this is referring to in this situation. See Intent Constructors

What you probably want is

Intent intent = new Intent(webViewContext,profilepictureview.class);

but you will want to initialize webViewContext first.

Edit startActivity also, needs a Context. Try this

webViewContext.startActivity(intent);
于 2013-09-11T17:59:40.043 回答
0

Keeping a static reference to an activity's context is going to cause you problems. All views (including WebView) have getContext(), so use view.getContext() rather than 'this' or 'webViewContext' when creating the Intent.

Also, you're trying to start an activity named 'profilepictureview', which sounds more like a View rather than an Activity?

于 2013-09-11T18:29:06.050 回答