如何从外部访问onCreate中声明的Webview?

时间:2016-03-25 21:03:40

标签: java android android-studio android-webview

我正在尝试开发一些基于WebView的简单应用,并且可以在Android应用和远程页面之间交换数据。

问题描述:

loadUrl在onCreate中工作,但不在onCreate外部工作。请参阅下面的代码并解释我做错了什么,为什么它在WebAppInterface类中不起作用:

public class MainActivity extends Activity {
    public WebView view1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String url = "htxx://mydomain.com/";
        final WebView view1 =(WebView) this.findViewById(R.id.webView);
        view1.addJavascriptInterface(new WebAppInterface(this), "Android"); //exchange between web-js and webview
        WebSettings webSettings = view1.getSettings();
        webSettings.setJavaScriptEnabled(true);
        view1.loadUrl(url);
        view1.setWebViewClient(new com.example.project.MyAppWebViewClient() {
            @Override
            public void onPageFinished(WebView view1, String url) {
                findViewById(R.id.progressBar1).setVisibility(View.GONE);
                findViewById(R.id.webView).setVisibility(View.VISIBLE);
            }
        });
        view1.setInitialScale(100);

        ///// ---> That's works:
        view1.setWebViewClient(new WebViewClient() {
            public void onPageFinished(WebView view, String url) {
                view1.loadUrl("javascript:somescript('Page is loaded')");
            }
        });

        ///// ---> That's works too:
        Button btn = (Button) findViewById(R.id.button1);
        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
                String device_id = tm.getDeviceId();
                view1.loadUrl("javascript:somescript('"+device_id+"')");
            }
        });
    }

    public class WebAppInterface {
        Context mContext;
        WebAppInterface(Context c) {
            mContext = c;
        }

        @JavascriptInterface
        public void showToast(String toast) {
            //show toast from web-app (js to android)
            Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
        }

        @JavascriptInterface
        public void printFromWeb() {
            //show toast from web-app (js to android)

            ///// ---> That's NOT working :(
            view1.loadUrl("javascript:somescript('Checking loadurl from outside of onCreate')");
        }
    }
}

有趣的是,Android studio中没有任何错误。

1 个答案:

答案 0 :(得分:2)

您在public WebView view1;内有view1字段和完全不同的最终onCreate(),因此在您的public void printFromWeb()方法中它实际上为空。

这是安全的路线:

要从外部访问“WebView”,请添加WebView类的私有字段:

private WebView webView;

onCreate()方法中,实例化它:

webView = (WebView) findViewById(R.id.webView);

添加getter方法来访问实例:

public WebView getWebView() {
    return webView;
}

请记住只在UI线程上触摸它。

更新:如何从not-ui线程安全地访问视图:

MainActivity.this.runOnUiThread(new Runnable() {
    @Override
    public void run(){
        view1.loadUrl(...)
    }
})