Javascript (TypeError):无法调用方法';setBadgeText';未定义的

Javascript (TypeError):无法调用方法';setBadgeText';未定义的,javascript,gwt,google-chrome-extension,Javascript,Gwt,Google Chrome Extension,我正在使用GWT实现chrome扩展。我在manifest.json中创建了映射并编写了外部java脚本文件。在background.js脚本中,我在该集合中编写了函数,即chrome.browserAction.setBadgeText(count);。我使用gwt+jsni方法从gwt类传递字符串值 public static native void passValue(String updateCount) /*-{ $wnd.realTimeUpdateCoun

我正在使用GWT实现chrome扩展。我在manifest.json中创建了映射并编写了外部java脚本文件。在background.js脚本中,我在该集合中编写了函数,即chrome.browserAction.setBadgeText(count);。我使用gwt+jsni方法从gwt类传递字符串值

     public static native void passValue(String updateCount) /*-{
        $wnd.realTimeUpdateCount(updateCount);
    }-*/;    
The Methods in the java script :  
        function realTimeUpdateCount(count) {

      localStorage.unreadCount =  count;
      updateCount();
        }

       function updateCount() {
         if (!localStorage.hasOwnProperty('unreadCount')) {
          chrome.browserAction.setIcon({path:"images/favIcon.ico"});
          chrome.browserAction.setBadgeBackgroundColor({color:[190, 190, 190, 230]});
          chrome.browserAction.setBadgeText({text:"0"});
      } else {
           chrome.browserAction.setBadgeText(localStorage.unreadCount);
      }
       }
这是我的manifest.json文件:-

{
  "name": "A browser action with no icon that makes the page red",
  "version": "1.0",
  "background": { "scripts": ["background.js"] },
  "permissions": [
     "tabs",
    "webNavigation", 
    "http://127.0.0.1:8888/Engile.html?gwt.codesvr=127.0.0.1"
  ],
  "web_accessible_resources": [
    "4D1B5509A17D34BAE8DB33C353725838.cache.html"
  ],  
  "browser_action": {
    "name": "Make this page red",
    "default_icon": "images/favIcon.ico"

  },
  "manifest_version": 2
}
以下是我的background.js:-

function realTimeUpdateCount(count) {


    localStorage.unreadCount =  count;
    updateIcon();

}

if (chrome.runtime && chrome.runtime.onStartup) {
      chrome.runtime.onStartup.addListener(function() {
        console.log('Starting browser... updating icon.');
        updateIcon();
      });
    } else {

      chrome.windows.onCreated.addListener(function() {
        console.log('Window created... updating icon.');
        updateIcon();
      });
    }
function updateIcon() {



    if (!localStorage.hasOwnProperty('unreadCount')) {
        chrome.browserAction.setIcon({path:"images/favIcon.ico"});
        chrome.browserAction.setBadgeBackgroundColor({color:[190, 190, 190, 230]});
        chrome.browserAction.setBadgeText({text:"?"});
      } else {

        chrome.browserAction.setBadgeText({
          text: localStorage.unreadCount
        });
      }

    }



function goToApp() {
    chrome.browserAction.setBadgeBackgroundColor({color:[0, 200, 0, 100]});
      console.log('Going to app...');
      chrome.tabs.getAllInWindow(undefined, function(tabs) {

        console.log('Opening the application ...');
        chrome.tabs.create({url: 'http://server.ensarm.com:8082/EngileLive/Engile.html'});
      });
    }
chrome.browserAction.onClicked.addListener(goToApp);
加载浏览器时,默认的0值在图标上正确显示。实时值在updateCount()方法中正确显示。但是在这个chrome.browserAction.setBadgeText(count)上进行控制;第行,它引发以下异常:

com.google.gwt.core.client.JavaScriptException: (TypeError): Cannot call method 'setBadgeText' of undefined
    at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:248)
    at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:561)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:269)
    at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)
    at com.google.gwt.core.client.impl.Impl.apply(Impl.java)
    at com.google.gwt.core.client.impl.Impl.entry0(Impl.java:213)
    at sun.reflect.GeneratedMethodAccessor30.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
    at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
    at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
    at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessages(BrowserChannelServer.java:292)
    at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:546)
    at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:363)
    at java.lang.Thread.run(Thread.java:662) 
有人能帮忙吗?代码中有什么问题。

使用

else {
           chrome.browserAction.setBadgeText({text:localStorage.unreadCount});
      }
而不是

else {
           chrome.browserAction.setBadgeText(localStorage.unreadCount);
      }
browserAction.setBadgeText
将json对象作为输入

工具书类

您只能从后台脚本文件调用chrome.browserAction调用(我也遇到了同样的问题)

见:


谢谢你,Sudarshan……我已经使用了这两个语句,但还是出现了相同的异常。还有其他解决方案吗?嗨,Sudarshan,我在上面添加了两个文件manifest.json和background.js。在第二个文件中,即background.js函数realTimeUpdateCount(count)是从GWT代码(上述问题中提到的代码)调用的使用json。字符串值被传递给此函数。请对此进行指导。@dhananjay:如果您通过编辑问题发布所有代码,这将非常好,这也将对其他人有所帮助。@dhananjay:我尝试过您的代码,它对我来说工作正常,这是你正在使用的代码吗?@dhananjay:这段代码
chrome.browserAction.setBadgeText(count)在哪里它不在
background.js
中,它在哪里?我只在background.html上调用chrome.browserAction,我以同样的方式得到了错误。。。