开发者

HelloWebViewClient cannot be resolved to a type

开发者 https://www.devze.com 2023-02-01 17:28 出处:网络
i followed a guide on http://developer.android.com/resources/tutorials/views/hello-webview.html and then created my own EditText field and a button. the code should explain it all, my problem is i kee

i followed a guide on http://developer.android.com/resources/tutorials/views/hello-webview.html and then created my own EditText field and a button. the code should explain it all, my problem is i keep getting a "HelloWebViewClient cannot be resolved to a type" Error, any suggestions? thanks in advance!

    package com.text.text;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;

public class test extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.alt);
        final EditText edittext = (EditText) findViewById(R.id.edittext);
        final Button button = (Button) findViewById(R.id.okay);
        开发者_高级运维button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Editable text = edittext.getText();
                String Tekst = text.toString();
                setContentView(R.layout.main);
                WebView mWebView;
                mWebView = (WebView) findViewById(R.id.webview);
                mWebView.getSettings().setJavaScriptEnabled(true);
                mWebView.loadUrl(Tekst);
                mWebView.setWebViewClient(new HelloWebViewClient());
                class HelloWebViewClient extends WebViewClient {
                    @Override
                    public boolean shouldOverrideUrlLoading(WebView view, String url) {
                        view.loadUrl(url);
                        return true;

                    }
        };
    }
});
}}


What if you put that class outside?

public class test extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.alt);
        final EditText edittext = (EditText) findViewById(R.id.edittext);
        final Button button = (Button) findViewById(R.id.okay);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Editable text = edittext.getText();
                String Tekst = text.toString();
                setContentView(R.layout.main);
                WebView mWebView;
                mWebView = (WebView) findViewById(R.id.webview);
                mWebView.getSettings().setJavaScriptEnabled(true);
                mWebView.loadUrl(Tekst);
                mWebView.setWebViewClient(new HelloWebViewClient());
             }
        });
     }

    private class HelloWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }

}


Change

mWebView = (WebView) findViewById(R.id.webview);

to

mWebView = (WebView) test.this.findViewById(R.id.webview);

Also, in Java class names should start with a capital letter.


The problem is that you declare your class AFTER you use it. Move line mWebView.setWebViewClient(new HelloWebViewClient()); AFTER class declaration.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号