我有此函数尝试使用以下firebase addchildevent侦听器调用android活动中的方法,但出现“ NullPointer”错误。我需要实现此代码的帮助,以便可以在其他kotlin类中访问该方法,并在适当的上下文中调用它。
// ==UserScript==
// @name USTVGO, sort "Recent Posts"
// @match *://ustvgo.tv/*
// @noframes
// @grant none
// ==/UserScript==
/* global jQuery, $ */
/* eslint-disable no-multi-spaces, curly */
'use strict';
var $ = jQuery; // The page loads jQuery (and we are in grant none mode), but doesn't set `$`.
var recentPostsLst = $("#recent-posts-3 > ul");
var itemsToSort = recentPostsLst.find ("li");
itemsToSort.sort (sortByLinkTextAscending).appendTo (recentPostsLst);
function sortByLinkTextAscending (nodeA, nodeB) {
var valA_Text = $(nodeA).find ("a").text ().trim ();
var valB_Text = $(nodeB).find ("a").text ().trim ();
return valA_Text.localeCompare (valB_Text, 'en', {sensitivity: 'base'} );
}
这是我尝试从kotlin类调用的android活动方法:
companion object {
private fun checkAdmin(userId: String) {
isAdmin = false
val ref: DatabaseReference = firebaseDatabase!!.reference
.child("administrators")
.child(userId)
childEventListener = ref.addChildEventListener(object : ChildEventListener {
override fun onCancelled(p0: DatabaseError) {
Log.d("Cancelled", "FirebaseUtil cancel")
}
override fun onChildMoved(p0: DataSnapshot, p1: String?) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onChildChanged(p0: DataSnapshot, p1: String?) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onChildAdded(p0: DataSnapshot, p1: String?) {
isAdmin = true
val caller = ListActivity()
caller.showMenu()
}
override fun onChildRemoved(p0: DataSnapshot) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
})
}
fun detachListener(callerActivity: Activity?) {
firebaseAuth!!.removeAuthStateListener(object : AuthStateListener {
override fun onAuthStateChanged(p0: FirebaseAuth) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
})
}
}
错误:
fun showMenu() {
this@ListActivity.invalidateOptionsMenu()
}
答案 0 :(得分:0)
我能够使用以下方法解决该问题:
步骤:
修改checkAdmin
函数以接受两个参数,一个像以前一样为string
,第二个为ListActivity
类型的活动:
private fun checkAdmin(userId: String, activity: ListActivity) {
isAdmin = false
val ref: DatabaseReference = firebaseDatabase!!.reference
.child("administrators")
.child(userId)
childEventListener = ref.addChildEventListener(object : ChildEventListener {
override fun onCancelled(p0: DatabaseError) {
Log.d("Cancelled", "FirebaseUtil cancel")
}
override fun onChildMoved(p0: DataSnapshot, p1: String?) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onChildChanged(p0: DataSnapshot, p1: String?) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onChildAdded(p0: DataSnapshot, p1: String?) {
isAdmin = true
activity.showMenu(activity)
}
override fun onChildRemoved(p0: DataSnapshot) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
})
}
调用此函数以传入两个参数,一个为字符串,另一个为活动,并将第二个强制转换为ListActivity类型:
checkAdmin(userId, callerActivity as ListActivity)
在ListActivity类中,如下设置showMenu函数:
fun showMenu(activity: ListActivity) {
activity.invalidateOptionsMenu()
}
通过上述设置,我可以访问ListActivity的showManu
函数。