javascriptinterface的startActivity

时间:2014-04-19 16:34:18

标签: android

活动无法启动...

MainActivity.java:

   public class MainActivity extends Activity {
            WebView web;

            public void onCreate(Bundle savedInstanceState) {

                super.onCreate(savedInstanceState);
                requestWindowFeature(Window.FEATURE_NO_TITLE);
                getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                        WindowManager.LayoutParams.FLAG_FULLSCREEN);

                web = new WebView(this);
                setContentView(web);
                WebSettings settings = web.getSettings();
                settings.setDefaultTextEncodingName("utf-8");
                web.addJavascriptInterface(new JavaScriptInterface(this), "Android");
                settings.setJavaScriptEnabled(true);
                web.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
                web.loadUrl("file:///android_asset/www/simple.html");

            }

            public class JavaScriptInterface {
                Context mContext;

                /** Instantiate the interface and set the context */
                JavaScriptInterface(Context c) {
                    mContext = c;
                }

                public void order() {
                    Log.d("JS", "StartActivity");
                    mContext.startActivity(new Intent(MainActivity.this, Order.class));

                }
            }
        }

javascript.js:

function order() {
   Android.order();
}

我的simple.html:

   <html>
       <head>
         <meta charset="utf-8">
         <title>Simple</title>
         <link rel="stylesheet" type="text/css" href="style.css">
         <script type="text/javascript" src="javascript.js" charset="utf-8"></script>
       </head>

    <body>
       <div class="product">
          <img src="images/p1.png" width="130" height="200">
          <input class="submit-button" onclick="order()" type="button" value="Submit" />
       </div>

    </body>
    </html>

startActivity:

   public class Order extends Activity{
         @Override
         protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         requestWindowFeature(Window.FEATURE_NO_TITLE);
         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                        WindowManager.LayoutParams.FLAG_FULLSCREEN);


          setContentView(R.layout.main);
          }

      }

当我点击按钮时,logcat显示:

04-19 21:46:42.790: V/WebViewInputDispatcher(24323): blockWebkitDraw
04-19 21:46:42.790: V/WebViewInputDispatcher(24323): blockWebkitDraw lockedtrue
04-19 21:46:42.790: V/webview(24323):  singleCursorHandlerTouchEvent -  getEditableSupport  FASLE 
04-19 21:46:43.280: D/webview(24323): blockWebkitViewMessage= false

我阅读了很多关于这个主题的信息,但由于某些原因它不起作用。请帮忙解决这个问题。

1 个答案:

答案 0 :(得分:1)

要创建一个javascript界面​​,您必须使用@JavascriptInterface在javascript中注释要使用的方法,如下所示:

public class JavaScriptInterface {
    Context mContext;

    /** Instantiate the interface and set the context */
    JavaScriptInterface(Context c) {
        mContext = c;
    }

    @JavascriptInterface
    public void order() {
        Log.d("JS", "StartActivity");
        mContext.startActivity(new Intent(MainActivity.this, Order.class));
    }
}

您还应该在logcat中查找任何错误,例如找不到订单功能的javascript错误也应该显示在那里。