Skip to content

Commit

Permalink
修复空指针问题
Browse files Browse the repository at this point in the history
  • Loading branch information
hjhrq1991 committed Mar 19, 2024
1 parent 860629e commit 8a1954b
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 14 deletions.
81 changes: 69 additions & 12 deletions example/src/main/assets/testJavascriptBridge.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
<meta name="viewport" content="initial-scale=1, user-scalable=0, minimal-ui">
<title></title>
</head>
<body onload="JsHandler()">
<body onload="finishLoadAndCall()">
<script type="text/javascript" src="/res/common/js/jquery.js"></script>
<p>
<input type="button" id="enter" value="我是按钮1" onclick="jsClick1();"
<input type="button" id="enter" value="js按钮1" onclick="jsClick1();"
/>
<input type="button" id="enter" value="我是按钮2" onclick="jsClick2();"
<input type="button" id="enter" value="js按钮2" onclick="jsClick2();"
/>
</p><br/>
<p>
Expand Down Expand Up @@ -64,8 +64,6 @@
//注册桥方法1
bridge.registerHandler("appClick1", function(data, responseCallback) {
responseCallback("receive click1");
/*弹窗*/
dialog();
});

//注册桥方法2
Expand All @@ -79,11 +77,74 @@
});
})








//桥2测试
//桥方法
var jsBridge2 = function (callback) {
try {
if (window.AppBridge) {
callback(AppBridge);
} else {
document.addEventListener("AppBridgeReady", function () {
callback(AppBridge);
}, false);
}
} catch (ex) { }
};

/*
* JS 调用app native方法
*
* @param nativeCallName 请求nativeCallName
* @param data 数据实体
*/
var appBridge = {
callApp: function (name, data) {
if (!data) {
data = default_data;
}
jsBridge2(function (appBridge) {
if (typeof appBridge === "undefined") {
console.log("JsBridge", "该桥未初始化,不执行发送消息到 undefined了" );
return;
}
appBridge.callHandler(name, JSON.stringify(data));
});
},
};

/* app native调用本页面方法 */
jsBridge2(function(appBridge) {
//初始化
appBridge.init(function(message, responseCallback) {

});

//注册桥方法1
appBridge.registerHandler("AppBridgeClick1", function(data, responseCallback) {
console.log("JsBridge", "该桥未初始化,不执行发送消息到 桥2也收到消息了" );
});
})







//JS点击方法
function jsClick1() {
var data = new Data().click1;
try {
bridge.callApp("jsClick1", data);

appBridge.callApp("jsBridge2Click", data);
} catch (ex) { }
}

Expand All @@ -96,13 +157,8 @@
}


/*弹窗*/
function dialog(){
jsClick2();
}

<!-- 测试方法 -->
function JsHandler(){
<!-- 页面加载完就调用方法 -->
function finishLoadAndCall(){
var objData = new Data();
var handlerData = objData.handlerData;
bridge.callApp("jsCall1", handlerData);
Expand All @@ -125,6 +181,7 @@
status : "ok"
}
}

</script>

</body>
Expand Down
2 changes: 1 addition & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

def group = 'com.github.hjhrq1991'
def libraryVersion = '1.1.3'
def libraryVersion = '1.1.4'

// Because the components are created only during the afterEvaluate phase, you must
// configure your publications using the afterEvaluate() lifecycle method.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public void onCallBack(String data) {
if (!TextUtils.isEmpty(responseId)) {
CallBackFunction function = responseCallbacks.get(responseId);
String responseData = m.getResponseData();
function.onCallBack(responseData);
if (function != null) function.onCallBack(responseData);
responseCallbacks.remove(responseId);

if (isDebug) Log.i(TAG, bridgeName + " console flushMessageQueue:responseId不为空");
Expand Down

0 comments on commit 8a1954b

Please sign in to comment.