获取错误childbrowser.java [英] Getting error in childbrowser.java

查看:155
本文介绍了获取错误childbrowser.java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到错误childbrowser.javaCTX解决不了。我跟着从视频链接指令 http://www.youtube.com/watch?v=84jmuXS8GJI 获得使用PhoneGap的在我的Andr​​oid应用程序的childbrowser,但仍然得到这个错误。任何人都请解决这个问题。


  / *这是我childBrowser.java code * /包com.phonegap.plugins.childBrowser;进口java.io.IOException异常;公共类ChildBrowser扩展Plugin`
    受保护的静态最后弦乐LOG_TAG =ChildBrowser;
    私有静态诠释CLOSE_EVENT = 0;
    私有静态诠释LOCATION_CHANGED_EVENT = 1;    私人字符串browserCallbackId = NULL;    私人对话的对话;
    私人的WebView的WebView;
    私人的EditText的EditText;
    私人布尔showLocationBar = TRUE;    / **
     *执行该请求并返回PluginResult。
     *
     * @参数动作的动作来执行。
     *为插件参数@param ARGS JSONArry。
     * @参数callbackId回调成JavaScript时使用的回调ID。
     * @返回一个PluginResult用状态和消息对象。
     * /
    公共PluginResult执行(串动,JSONArray ARGS,弦乐callbackId){
        PluginResult.Status状态= PluginResult.Status.OK;
        字符串结果=;        尝试{
            如果(action.equals(showWebPage)){
                this.browserCallbackId = callbackId;                //如果ChildBrowser已经打开,然后抛出一个错误
                如果(对话= NULL&放大器;!&安培; dialog.isShowing()){
                    返回新PluginResult(PluginResult.Status.ERRORChildBrowser已打开);
                }                结果= this.showWebPage(args.getString(0),args.optJSONObject(1));                如果(result.length()大于0){
                    状态= PluginResult.Status.ERROR;
                    返回新PluginResult(状态,结果);
                }其他{
                    PluginResult pluginResult =新PluginResult(状态,结果);
                    pluginResult.setKeepCallback(真);
                    返回pluginResult;
                }
            }
            否则,如果(action.equals(亲密)){
                closeDialog();                JSONObject的OBJ =新的JSONObject();
                obj.put(类型,CLOSE_EVENT);                PluginResult pluginResult =新PluginResult(状态,OBJ);
                pluginResult.setKeepCallback(假);
                返回pluginResult;
            }
            否则如果(action.equals(openExternal)){
                结果= this.openExternal(args.getString(0),args.optBoolean(1));
                如果(result.length()大于0){
                    状态= PluginResult.Status.ERROR;
                }
            }
            其他{
                状态= PluginResult.Status.INVALID_ACTION;
            }
            返回新PluginResult(状态,结果);
        }赶上(JSONException E){
            返回新PluginResult(PluginResult.Status.JSON_EXCEPTION);
        }
    }    / **
     *显示与指定的URL新的浏览器。
     *
     * @参数的URL要加载的URL。
     在PhoneGap的web视图*参数usePhoneGap负载网址
     * @返回如果正常,或错误消息。
     * /
    公共字符串openExternal(字符串URL,布尔usePhoneGap){
        尝试{
            意向意图= NULL;
            如果(usePhoneGap){
                意图=新意图()setClass(this.ctx.getContext(),org.apache.cordova.DroidGap.class)。
                intent.setData(Uri.parse(URL)); //此行会在将来被移除。
                intent.putExtra(URL,URL);                //超时参数:60秒最大 - 可能更少,如果HTTP设备超时较少。
                intent.putExtra(loadUrlTimeoutValue,60000);                //这些参数可以,如果你想显示加载对话框进行配置
                intent.putExtra(loadingDialog,稍等,正在载入网页...); //显示加载对话框
                intent.putExtra(hideLoadingDialogOnPageLoad,真正的); //隐藏它一旦页面完全加载
            }
            其他{
                意图=新意图(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(URL));
            }
            this.ctx.startActivity(意向);
            返回;
        }赶上(android.content.ActivityNotFoundException E){
            Log.d(LOG_TAG,ChildBrowser:错误加载网址+ URL +:+ e.toString());
            返回e.toString();
        }
    }    / **
     *关闭对话框
     * /
    私人无效closeDialog(){
        如果(对话!= NULL){
            dialog.dismiss();
        }
    }    / **
     *检查,看看是否有可能返回一个页面的历史,那么这样做。
     * /
    私人无效GoBack的(){
        如果(this.webview.canGoBack()){
            this.webview.goBack();
        }
    }    / **
     *检查,看看是否有可能前进一页历史,那么这样做。
     * /
    私人无效goForward(){
        如果(this.webview.canGoForward()){
            this.webview.goForward();
        }
    }    / **
     *导航到新页面
     *
     * @参数的URL加载
     * /
    私人无效导航(字符串URL){
        InputMethodManager IMM =(InputMethodManager)this.ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(edittext.getWindowToken(),0);        如果(!url.startsWith(HTTP)及与放大器; url.startsWith(文件:)){
            this.webview.loadUrl(HTTP://+网址);
        }其他{
            this.webview.loadUrl(URL);
        }
        this.webview.requestFocus();
    }
    / **
     *我们应该显示地址栏?
     *
     * @返回布尔
     * /
    私人布尔getShowLocationBar(){
        返回this.showLocationBar;
    }    / **
     *显示与指定的URL新的浏览器。
     *
     * @参数的URL要加载的URL。
     * @参数的JSONObject
     * /
    公共字符串showWebPage(最终字符串URL,JSONObject的选项){
        //确定我们是否应该隐藏地址栏。
        如果(选项!= NULL){
            showLocationBar = options.optBoolean(showLocationBar,真正的);
        }        //创建新的线程对话
        可运行可运行=新的Runnable(){
            / **
             *把我们的DIP单位像素
             *
             * @返回INT
             * /
            私人诠释dpToPixels(INT dipValue){
                int值=(int)的TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                                                            (浮点)dipValue,
                                                            ctx.getContext()。getResources()。getDisplayMetrics()
                );                返回值;
            }            公共无效的run(){
                //让我们创建主对话框
                对话框=新的对话框(ctx.getContext(),android.R.style.Theme_NoTitleBar);
                dialog.getWindow()的getAttributes()windowAnimations = android.R.style.Animation_Dialog。;
                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog.setCancelable(真);
                dialog.setOnDismissListener(新DialogInterface.OnDismissListener(){
                        公共无效onDismiss(DialogInterface对话){
                            尝试{
                                JSONObject的OBJ =新的JSONObject();
                                obj.put(类型,CLOSE_EVENT);                                sendUpdate(OBJ,FALSE);
                            }赶上(JSONException E){
                                Log.d(LOG_TAG,不应该发生);
                            }
                        }
                });                //主容器布局
                主要的LinearLayout =新的LinearLayout(ctx.getContext());
                main.setOrientation(LinearLayout.VERTICAL);                //工具栏布局
                RelativeLayout的工具栏=新的RelativeLayout(ctx.getContext());
                toolbar.setLayoutParams(新RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,this.dpToPixels(44)));
                toolbar.setPadding(this.dpToPixels(2),this.dpToPixels(2),this.dpToPixels(2),this.dpToPixels(2));
                toolbar.setHorizo​​ntalGravity(Gravity.LEFT);
                toolbar.setVerticalGravity(Gravity.TOP);                //操作按钮容器布局
                RelativeLayout的actionButtonContainer =新的RelativeLayout(ctx.getContext());
                actionButtonContainer.setLayoutParams(新RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
                actionButtonContainer.setHorizo​​ntalGravity(Gravity.LEFT);
                actionButtonContainer.setVerticalGravity(Gravity.CENTER_VERTICAL);
                actionButtonContainer.setId(1);                //返回按钮
                后面的ImageButton =新的ImageButton(ctx.getContext());
                RelativeLayout.LayoutParams backLayoutParams =新RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.FILL_PARENT);
                backLayoutParams.addRule(RelativeLayout.ALIGN_LEFT);
                back.setLayoutParams(backLayoutParams);
                back.setContentDescription(后退按钮);
                back.setId(2);
                尝试{
                    back.setImageBitmap(loadDrawable(WWW / childbrowser / icon_arrow_left.png));
                }赶上(IOException异常五){
                    Log.e(LOG_TAG,e.getMessage(),E);
                }
                back.setOnClickListener(新View.OnClickListener(){
                    公共无效的onClick(视图v){
                        回去();
                    }
                });                //前进按钮
                前进的ImageButton =新的ImageButton(ctx.getContext());
                RelativeLayout.LayoutParams forwardLayoutParams =新RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.FILL_PARENT);
                forwardLayoutParams.addRule(RelativeLayout.RIGHT_OF,2);
                forward.setLayoutParams(forwardLayoutParams);
                forward.setContentDescription(前进按钮);
                forward.setId(3);
                尝试{
                    forward.setImageBitmap(loadDrawable(WWW / childbrowser / icon_arrow_right.png));
                }赶上(IOException异常五){
                    Log.e(LOG_TAG,e.getMessage(),E);
                }
                forward.setOnClickListener(新View.OnClickListener(){
                    公共无效的onClick(视图v){
                        前进();
                    }
                });                //编辑文本框
                的EditText =新的EditText(ctx.getContext());
                RelativeLayout.LayoutParams textLayoutParams =新RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
                textLayoutParams.addRule(RelativeLayout.RIGHT_OF,1);
                textLayoutParams.addRule(RelativeLayout.LEFT_OF,5);
                edittext.setLayoutParams(textLayoutParams);
                edittext.setId(4);
                edittext.setSingleLine(真);
                edittext.setText(URL);
                edittext.setInputType(InputType.TYPE_TEXT_VARIATION_URI);
                edittext.setImeOptions(EditorInfo.IME_ACTION_GO);
                edittext.setInputType(InputType.TYPE_NULL); //将不除输入...使文本不可编辑
                edittext.setOnKeyListener(新View.OnKeyListener(){
                    公共布尔安其(视图V,INT键code,KeyEvent的事件){
                        //如果事件上的Enter键一键按下事件
                        如果((event.getAction()== KeyEvent.ACTION_DOWN)及及(键code == KeyEvent.KEY code_ENTER)){
                          导航(edittext.getText()的toString());
                          返回true;
                        }
                        返回false;
                    }
                });                //关闭按钮
                的ImageButton接近=新的ImageButton(ctx.getContext());
                RelativeLayout.LayoutParams closeLayoutParams =新RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.FILL_PARENT);
                closeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                close.setLayoutParams(closeLayoutParams);
                forward.setContentDescription(关闭);
                close.setId(5);
                尝试{
                    close.setImageBitmap(loadDrawable(WWW / childbrowser / icon_close.png));
                }赶上(IOException异常五){
                    Log.e(LOG_TAG,e.getMessage(),E);
                }
                close.setOnClickListener(新View.OnClickListener(){
                    公共无效的onClick(视图v){
                        closeDialog();
                    }
                });                //的WebView
                网页流量=新的WebView(ctx.getContext());
                webview.setLayoutParams(新LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
                webview.setWebChromeClient(新WebChromeClient());
                WebViewClient客户端=新ChildBrowserClient(EditText上);
                webview.setWebViewClient(客户端);
                WebSettings设置= webview.getSettings();
                settings.setJavaScriptEnabled(真);
                settings.setJavaScriptCanOpenWindowsAutomatically(真);
                settings.setBuiltInZoomControls(真);
                settings.setPluginsEnabled(真);
                settings.setDomStorageEnabled(真);
                webview.loadUrl(URL);
                webview.setId(6);
                。webview.getSettings()setLoadWithOverviewMode(真);
                。webview.getSettings()setUseWideViewPort(真);
                webview.requestFocus();
                webview.requestFocusFromTouch();                //添加后退和前进按钮,我们的行动按钮容器布局
                actionButtonContainer.addView(回);
                actionButtonContainer.addView(向前);                //意见添加到工具栏我们
                toolbar.addView(actionButtonContainer);
                toolbar.addView(EditText上);
                toolbar.addView(接近);                //如果被禁用不要添加工具栏
                如果(getShowLocationBar()){
                    //我们的工具栏添加到我们的主视图/布局
                    main.addView(工具栏);
                }                //我们的WebView添加到我们的主视图/布局
                main.addView(web视图);                WindowManager.LayoutParams LP =新WindowManager.LayoutParams();
                lp.copyFrom(dialog.getWindow()的getAttributes());
                lp.width = WindowManager.LayoutParams.FILL_PARENT;
                lp.height = WindowManager.LayoutParams.FILL_PARENT;                dialog.setContentView(主);
                dialog.show();
                。dialog.getWindow()setAttributes(LP);
            }          私人位图loadDrawable(字符串文件名)产生java.io.IOException {
              输入的InputStream = ctx.getAssets()打开(文件名)。
              返回BitmapFactory.de codeStream(输入);
          }
        };
        this.ctx.runOnUiThread(可运行);
        返回;
    }    / **
     *创建一个新的插件结果,并把它发送回的JavaScript
     *
     * @参数OBJ一个JSONObject包含事件负载信息
     * /
    私人无效sendUpdate(OBJ的JSONObject,布尔keepCallback){
        如果(this.browserCallbackId!= NULL){
            PluginResult结果=新PluginResult(PluginResult.Status.OK,OBJ);
            result.setKeepCallback(keepCallback);
            this.success(结果,this.browserCallbackId);
        }
    }    / **
     *本网页流量的客户端接收有关APPVIEW通知
     * /
    公共类ChildBrowserClient扩展WebViewClient {
        EditText上的EditText;        / **
         *构造函数。
         *
         * @参数mContext
         * @参数的EditText
         * /
        公共ChildBrowserClient(EditText上mEditText){
            this.edittext = mEditText;
        }        / **
         *通知页面已经开始加载主机应用程序。
         *
         * @参数视图的WebView开始回调。
         * @参数的URL网页的网址。
         * /
        @覆盖
        公共无效onPageStarted(的WebView视图,字符串URL,位图图标){
            super.onPageStarted(查看,网址,图标);
            串newloc;
            如果(url.startsWith(HTTP)|| url.startsWith(https:开头)|| url.startsWith(文件)){
                newloc =网址;
            }其他{
                newloc =HTTP://+网址;
            }            如果(!newloc.equals(edittext.getText()。的toString())){
                edittext.setText(newloc);
            }            尝试{
                JSONObject的OBJ =新的JSONObject();
                obj.put(类型,LOCATION_CHANGED_EVENT);
                obj.put(位置,URL);                sendUpdate(OBJ,真);
            }赶上(JSONException E){
                Log.d(ChildBrowser,这绝不应该发生);
            }
        }
    }
}


这是我在我的ChildBrowser.java页面获取错误:

 描述资源路径位置类型
CTX解决不了ChildBrowser.java / childBrowser / src目录/ COM / PhoneGap的/插件/ childBrowser线228 Java问题
CTX解决不了ChildBrowser.java / childBrowser / src目录/ COM / PhoneGap的/插件/ childBrowser线236 Java问题
CTX解决不了ChildBrowser.java / childBrowser / src目录/ COM / PhoneGap的/插件/ childBrowser线254 Java问题
...


解决方案

您正在寻找一个错误的地方....为了获取更新和工作childbrowser副本
点击这里<一个href=\"https://github.com/phonegap/phonegap-plugins/tree/65be38cf02466748a3b3e93c2d18cb2c198feb28/Android/ChildBrowser\" rel=\"nofollow\">https://github.com/phonegap/phonegap-plugins/tree/65be38cf02466748a3b3e93c2d18cb2c198feb28/Android/ChildBrowser

更新: -

转寄此的http://simonmacdonald.blogspot.fi/2012/07/phonegap-android-plugins-sometimes-we.html

您需要修改一些东西像这样


  1. 与cordova.getContext取代ctx.getContext()()


  2. ctx.startActivity()与cordova.getActivity()取代。startActivity()


  3. ctx.getSystemService()与cordova.getActivity()取代。getSystemService()


  4. ctx.getAssets()与cordova.getActivity()取代。getAssets()


  5. 与cordova.getActivity()代替ctx.runOnUiThread()。runOnUiThread()


  6. ctx.getApplicationContext()与cordova.getActivity()取代。getApplicationContext()


  7. ctx.getPackageManager()与cordova.getActivity()取代。getPackageManager()


  8. 与cordova.getActivity()。getShared preferences取代
  9. ctx.getShared preferences()()


  10. ctx.unregisterActivity()与cordova.getActivity()取代。unregisterActivity()


  11. ctx.getResources()与cordova.getActivity()取代。getResources()


  12. 进口com.phonegap.api。*替换为进口org.apache.cordova.api。*


I am getting the error "ctx cannot be resolved" in childbrowser.java. I followed the instruction from the video link http://www.youtube.com/watch?v=84jmuXS8GJI to get the childbrowser in my Android app using Phonegap, but still got this error. Anybody please resolve this.


/*This is my childBrowser.java code*/

package com.phonegap.plugins.childBrowser;

import java.io.IOException;

public class ChildBrowser extends Plugin`  
    protected static final String LOG_TAG = "ChildBrowser";
    private static int CLOSE_EVENT = 0;
    private static int LOCATION_CHANGED_EVENT = 1;

    private String browserCallbackId = null;

    private Dialog dialog;
    private WebView webview;
    private EditText edittext; 
    private boolean showLocationBar = true;

    /**
     * Executes the request and returns PluginResult.
     *
     * @param action        The action to execute.
     * @param args          JSONArry of arguments for the plugin.
     * @param callbackId    The callback id used when calling back into JavaScript.
     * @return              A PluginResult object with a status and message.
     */
    public PluginResult execute(String action, JSONArray args, String callbackId) {
        PluginResult.Status status = PluginResult.Status.OK;
        String result = "";

        try {
            if (action.equals("showWebPage")) {
                this.browserCallbackId = callbackId;

                // If the ChildBrowser is already open then throw an error
                if (dialog != null && dialog.isShowing()) {
                    return new PluginResult(PluginResult.Status.ERROR, "ChildBrowser is already open");
                }

                result = this.showWebPage(args.getString(0), args.optJSONObject(1));

                if (result.length() > 0) {
                    status = PluginResult.Status.ERROR;
                    return new PluginResult(status, result);
                } else {
                    PluginResult pluginResult = new PluginResult(status, result);
                    pluginResult.setKeepCallback(true);
                    return pluginResult;
                }
            }
            else if (action.equals("close")) {
                closeDialog();

                JSONObject obj = new JSONObject();
                obj.put("type", CLOSE_EVENT);

                PluginResult pluginResult = new PluginResult(status, obj);
                pluginResult.setKeepCallback(false);
                return pluginResult;
            }
            else if (action.equals("openExternal")) {
                result = this.openExternal(args.getString(0), args.optBoolean(1));
                if (result.length() > 0) {
                    status = PluginResult.Status.ERROR;
                }
            }
            else {
                status = PluginResult.Status.INVALID_ACTION;
            }
            return new PluginResult(status, result);
        } catch (JSONException e) {
            return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
        }
    }

    /**
     * Display a new browser with the specified URL.
     *
     * @param url           The url to load.
     * @param usePhoneGap   Load url in PhoneGap webview
     * @return              "" if ok, or error message.
     */
    public String openExternal(String url, boolean usePhoneGap) {
        try {
            Intent intent = null;
            if (usePhoneGap) {
                intent = new Intent().setClass(this.ctx.getContext(), org.apache.cordova.DroidGap.class);
                intent.setData(Uri.parse(url)); // This line will be removed in future.
                intent.putExtra("url", url);

                // Timeout parameter: 60 sec max - May be less if http device timeout is less.
                intent.putExtra("loadUrlTimeoutValue", 60000);

                // These parameters can be configured if you want to show the loading dialog
                intent.putExtra("loadingDialog", "Wait,Loading web page...");   // show loading dialog
                intent.putExtra("hideLoadingDialogOnPageLoad", true);           // hide it once page has completely loaded
            }
            else {
                intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(url));
            }
            this.ctx.startActivity(intent);
            return "";
        } catch (android.content.ActivityNotFoundException e) {
            Log.d(LOG_TAG, "ChildBrowser: Error loading url "+url+":"+ e.toString());
            return e.toString();
        }
    }

    /**
     * Closes the dialog
     */
    private void closeDialog() {
        if (dialog != null) {
            dialog.dismiss();
        }
    }

    /**
     * Checks to see if it is possible to go back one page in history, then does so.
     */
    private void goBack() {
        if (this.webview.canGoBack()) {
            this.webview.goBack();
        }
    }

    /**
     * Checks to see if it is possible to go forward one page in history, then does so.
     */
    private void goForward() {
        if (this.webview.canGoForward()) {
            this.webview.goForward();
        }
    }

    /**
     * Navigate to the new page
     * 
     * @param url to load
     */
    private void navigate(String url) {        
        InputMethodManager imm = (InputMethodManager)this.ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(edittext.getWindowToken(), 0);

        if (!url.startsWith("http") && !url.startsWith("file:")) {
            this.webview.loadUrl("http://" + url);
        } else {
            this.webview.loadUrl(url);
        }
        this.webview.requestFocus();
    }


    /**
     * Should we show the location bar?
     * 
     * @return boolean
     */
    private boolean getShowLocationBar() {
        return this.showLocationBar;
    }

    /**
     * Display a new browser with the specified URL.
     *
     * @param url           The url to load.
     * @param jsonObject 
     */
    public String showWebPage(final String url, JSONObject options) {
        // Determine if we should hide the location bar.
        if (options != null) {
            showLocationBar = options.optBoolean("showLocationBar", true);
        }

        // Create dialog in new thread 
        Runnable runnable = new Runnable() {
            /**
             * Convert our DIP units to Pixels
             * 
             * @return int
             */
            private int dpToPixels(int dipValue) {
                int value = (int) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP,
                                                            (float) dipValue,
                                                            ctx.getContext().getResources().getDisplayMetrics()
                );

                return value;
            }

            public void run() {
                // Let's create the main dialog
                dialog = new Dialog(ctx.getContext(), android.R.style.Theme_NoTitleBar);
                dialog.getWindow().getAttributes().windowAnimations = android.R.style.Animation_Dialog;
                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog.setCancelable(true);
                dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                        public void onDismiss(DialogInterface dialog) {
                            try {
                                JSONObject obj = new JSONObject();
                                obj.put("type", CLOSE_EVENT);

                                sendUpdate(obj, false);
                            } catch (JSONException e) {
                                Log.d(LOG_TAG, "Should never happen");
                            }
                        }
                });

                // Main container layout
                LinearLayout main = new LinearLayout(ctx.getContext());
                main.setOrientation(LinearLayout.VERTICAL);

                // Toolbar layout
                RelativeLayout toolbar = new RelativeLayout(ctx.getContext());
                toolbar.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, this.dpToPixels(44)));
                toolbar.setPadding(this.dpToPixels(2), this.dpToPixels(2), this.dpToPixels(2), this.dpToPixels(2));
                toolbar.setHorizontalGravity(Gravity.LEFT);
                toolbar.setVerticalGravity(Gravity.TOP);

                // Action Button Container layout
                RelativeLayout actionButtonContainer = new RelativeLayout(ctx.getContext());
                actionButtonContainer.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                actionButtonContainer.setHorizontalGravity(Gravity.LEFT);
                actionButtonContainer.setVerticalGravity(Gravity.CENTER_VERTICAL);
                actionButtonContainer.setId(1);

                // Back button
                ImageButton back = new ImageButton(ctx.getContext());
                RelativeLayout.LayoutParams backLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
                backLayoutParams.addRule(RelativeLayout.ALIGN_LEFT);
                back.setLayoutParams(backLayoutParams);
                back.setContentDescription("Back Button");
                back.setId(2);
                try {
                    back.setImageBitmap(loadDrawable("www/childbrowser/icon_arrow_left.png"));
                } catch (IOException e) {
                    Log.e(LOG_TAG, e.getMessage(), e);
                }
                back.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        goBack();
                    }
                });

                // Forward button
                ImageButton forward = new ImageButton(ctx.getContext());
                RelativeLayout.LayoutParams forwardLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
                forwardLayoutParams.addRule(RelativeLayout.RIGHT_OF, 2);
                forward.setLayoutParams(forwardLayoutParams);
                forward.setContentDescription("Forward Button");
                forward.setId(3);
                try {
                    forward.setImageBitmap(loadDrawable("www/childbrowser/icon_arrow_right.png"));
                } catch (IOException e) {
                    Log.e(LOG_TAG, e.getMessage(), e);
                }
                forward.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        goForward();
                    }
                });

                // Edit Text Box
                edittext = new EditText(ctx.getContext());
                RelativeLayout.LayoutParams textLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
                textLayoutParams.addRule(RelativeLayout.RIGHT_OF, 1);
                textLayoutParams.addRule(RelativeLayout.LEFT_OF, 5);
                edittext.setLayoutParams(textLayoutParams);
                edittext.setId(4);
                edittext.setSingleLine(true);
                edittext.setText(url);
                edittext.setInputType(InputType.TYPE_TEXT_VARIATION_URI);
                edittext.setImeOptions(EditorInfo.IME_ACTION_GO);
                edittext.setInputType(InputType.TYPE_NULL); // Will not except input... Makes the text NON-EDITABLE
                edittext.setOnKeyListener(new View.OnKeyListener() {
                    public boolean onKey(View v, int keyCode, KeyEvent event) {
                        // If the event is a key-down event on the "enter" button
                        if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                          navigate(edittext.getText().toString());
                          return true;
                        }
                        return false;
                    }
                });

                // Close button
                ImageButton close = new ImageButton(ctx.getContext());
                RelativeLayout.LayoutParams closeLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
                closeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                close.setLayoutParams(closeLayoutParams);
                forward.setContentDescription("Close Button");
                close.setId(5);
                try {
                    close.setImageBitmap(loadDrawable("www/childbrowser/icon_close.png"));
                } catch (IOException e) {
                    Log.e(LOG_TAG, e.getMessage(), e);
                }   
                close.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        closeDialog();
                    }
                });

                // WebView
                webview = new WebView(ctx.getContext());
                webview.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
                webview.setWebChromeClient(new WebChromeClient());
                WebViewClient client = new ChildBrowserClient(edittext);
                webview.setWebViewClient(client);
                WebSettings settings = webview.getSettings();
                settings.setJavaScriptEnabled(true);
                settings.setJavaScriptCanOpenWindowsAutomatically(true);
                settings.setBuiltInZoomControls(true);
                settings.setPluginsEnabled(true);
                settings.setDomStorageEnabled(true);
                webview.loadUrl(url);
                webview.setId(6);
                webview.getSettings().setLoadWithOverviewMode(true);
                webview.getSettings().setUseWideViewPort(true);
                webview.requestFocus();
                webview.requestFocusFromTouch();   

                // Add the back and forward buttons to our action button container layout
                actionButtonContainer.addView(back);
                actionButtonContainer.addView(forward);

                // Add the views to our toolbar
                toolbar.addView(actionButtonContainer);
                toolbar.addView(edittext);
                toolbar.addView(close);

                // Don't add the toolbar if its been disabled
                if (getShowLocationBar()) {
                    // Add our toolbar to our main view/layout
                    main.addView(toolbar);
                }

                // Add our webview to our main view/layout
                main.addView(webview);

                WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
                lp.copyFrom(dialog.getWindow().getAttributes());
                lp.width = WindowManager.LayoutParams.FILL_PARENT;
                lp.height = WindowManager.LayoutParams.FILL_PARENT;

                dialog.setContentView(main);
                dialog.show();
                dialog.getWindow().setAttributes(lp);
            }

          private Bitmap loadDrawable(String filename) throws java.io.IOException {
              InputStream input = ctx.getAssets().open(filename);    
              return BitmapFactory.decodeStream(input);
          }
        };
        this.ctx.runOnUiThread(runnable);
        return "";
    }

    /**
     * Create a new plugin result and send it back to JavaScript
     * 
     * @param obj a JSONObject contain event payload information
     */
    private void sendUpdate(JSONObject obj, boolean keepCallback) {
        if (this.browserCallbackId != null) {
            PluginResult result = new PluginResult(PluginResult.Status.OK, obj);
            result.setKeepCallback(keepCallback);
            this.success(result, this.browserCallbackId);
        }
    }

    /**
     * The webview client receives notifications about appView
     */
    public class ChildBrowserClient extends WebViewClient {
        EditText edittext;

        /**
         * Constructor.
         * 
         * @param mContext
         * @param edittext 
         */
        public ChildBrowserClient(EditText mEditText) {
            this.edittext = mEditText;
        }       

        /**
         * Notify the host application that a page has started loading.
         * 
         * @param view          The webview initiating the callback.
         * @param url           The url of the page.
         */
        @Override
        public void onPageStarted(WebView view, String url,  Bitmap favicon) {
            super.onPageStarted(view, url, favicon);            
            String newloc;
            if (url.startsWith("http:") || url.startsWith("https:") || url.startsWith("file:")) {
                newloc = url;
            } else {
                newloc = "http://" + url;
            }

            if (!newloc.equals(edittext.getText().toString())) {           
                edittext.setText(newloc);
            }

            try {
                JSONObject obj = new JSONObject();
                obj.put("type", LOCATION_CHANGED_EVENT);
                obj.put("location", url);

                sendUpdate(obj, true);
            } catch (JSONException e) {
                Log.d("ChildBrowser", "This should never happen");
            }
        }
    }
}


These are the errors I am getting in my ChildBrowser.java page:

Description Resource    Path    Location    Type
ctx cannot be resolved  ChildBrowser.java   /childBrowser/src/com/phonegap/plugins/childBrowser line 228    Java Problem
ctx cannot be resolved  ChildBrowser.java   /childBrowser/src/com/phonegap/plugins/childBrowser line 236    Java Problem
ctx cannot be resolved  ChildBrowser.java   /childBrowser/src/com/phonegap/plugins/childBrowser line 254    Java Problem
...

解决方案

You are looking at a wrong place.... To get the updated and working copy of childbrowser check here https://github.com/phonegap/phonegap-plugins/tree/65be38cf02466748a3b3e93c2d18cb2c198feb28/Android/ChildBrowser

UPDATE:-

Refer this http://simonmacdonald.blogspot.fi/2012/07/phonegap-android-plugins-sometimes-we.html

You need to modify few things like this

  1. ctx.getContext() replaced with cordova.getContext()

  2. ctx.startActivity() replaced with cordova.getActivity().startActivity()

  3. ctx.getSystemService() replaced with cordova.getActivity().getSystemService()

  4. ctx.getAssets() replaced with cordova.getActivity().getAssets()

  5. ctx.runOnUiThread() replaced with cordova.getActivity().runOnUiThread()

  6. ctx.getApplicationContext() replaced with cordova.getActivity().getApplicationContext()

  7. ctx.getPackageManager() replaced with cordova.getActivity().getPackageManager()

  8. ctx.getSharedPreferences() replaced with cordova.getActivity().getSharedPreferences()

  9. ctx.unregisterActivity() replaced with cordova.getActivity().unregisterActivity()

  10. ctx.getResources() replaced with cordova.getActivity().getResources()

  11. import com.phonegap.api.* replaced with import org.apache.cordova.api.*

这篇关于获取错误childbrowser.java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆