我从Java到kotlin都没有看这个表盘示例,但是突然发现我无法访问三个函数。 (我只提到一个,因为错误是相同的“未解决的引用”)
我不明白为什么我不能访问它们,我已经更改了访问修饰符,将一个类扩展到另一个类,但是我无法访问那些功能
感谢您的宝贵意见
ComplicationConfigActivity类:WearableActivity(),View.OnClickListener {
private val TAG = "Configuration Activity"
internal val COMPLICATION_CONFIG_REQUEST_CODE = 1001 // internal val COMPLICATION_CONFIG_REQUEST_CODE = 1001
enum class ComplicationLocation {
LEFT,
RIGHT,
TOP,
BOTTOM
}//---
private var mLeftComplicationId: Int = 0
private var mRightComplicationId: Int = 0
//---
private var mTopComplicationId: Int = 0
private var mBottomComplicationId: Int = 0
//---
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_complication_config)
setContentView(R.layout.activity_complication_config)
mDefaultAddComplicationDrawable = getDrawable(R.drawable.add_complication)
mSelectedComplicationId = -1
mLeftComplicationId = MyWatchFace.getComplicationId(ComplicationLocation.LEFT) // unresolved reference
mRightComplicationId = MyWatchFace.getComplicationId(ComplicationLocation.RIGHT) // getComplicationId
mTopComplicationId = MyWatchFace.getComplicationId(ComplicationLocation.TOP)
mBottomComplicationId = MyWatchFace.getComplicationId(ComplicationLocation.BOTTOM)
MyWatchFace类:CanvasWatchFaceService(){
private val TAG = "ComplicationConfigActivity"
// TODO: Step 2, intro 1
private val LEFT_COMPLICATION_ID = 0
private val RIGHT_COMPLICATION_ID = 1
private val TOP_COMPLICATION_ID = 2
private val BOTTOM_COMPLICATION_ID = 3
private val COMPLICATION_IDS =
intArrayOf(LEFT_COMPLICATION_ID, RIGHT_COMPLICATION_ID, TOP_COMPLICATION_ID, BOTTOM_COMPLICATION_ID)
// Left and right dial supported types.
private val COMPLICATION_SUPPORTED_TYPES = arrayOf(
intArrayOf(
ComplicationData.TYPE_RANGED_VALUE, ComplicationData.TYPE_ICON, ComplicationData.TYPE_SHORT_TEXT,
//ComplicationData.IMAGE_STYLE_PHOTO,//
ComplicationData.TYPE_SMALL_IMAGE
), intArrayOf(
ComplicationData.TYPE_RANGED_VALUE, ComplicationData.TYPE_ICON, ComplicationData.TYPE_SHORT_TEXT,
//ComplicationData.IMAGE_STYLE_PHOTO,//
ComplicationData.TYPE_SMALL_IMAGE
), intArrayOf(
ComplicationData.TYPE_RANGED_VALUE, ComplicationData.TYPE_ICON, ComplicationData.TYPE_SHORT_TEXT,
//ComplicationData.IMAGE_STYLE_PHOTO,//
ComplicationData.TYPE_SMALL_IMAGE
), intArrayOf(
ComplicationData.TYPE_RANGED_VALUE, ComplicationData.TYPE_ICON, ComplicationData.TYPE_SHORT_TEXT,
//ComplicationData.IMAGE_STYLE_PHOTO, //
ComplicationData.TYPE_SMALL_IMAGE
)
)
// Used by {@link ComplicationConfigActivity} to retrieve id for complication locations and
// to check if complication location is supported.
// TODO: Step 3, expose complication information, part 1
//---
internal fun getComplicationId( // CAN´T ACCESS THIS FUNCTION
complicationLocation: ComplicationLocation
): Int {
when (complicationLocation) {
LEFT -> return LEFT_COMPLICATION_ID
RIGHT -> return RIGHT_COMPLICATION_ID
TOP -> return TOP_COMPLICATION_ID
BOTTOM -> return BOTTOM_COMPLICATION_ID
else -> return -1
}
}
//----
// Used by {@link ComplicationConfigActivity} to retrieve all complication ids.
// TODO: Step 3, expose complication information, part 2
//---
internal fun getComplicationIds(): IntArray { // CAN´T ACCESS THIS FUNCTION
return COMPLICATION_IDS
}
//---
// Used by {@link ComplicationConfigActivity} to retrieve complication types supported by
// location.
// TODO: Step 3, expose complication information, part 3
//---
internal fun getSupportedComplicationTypes( // CAN´T ACCESS THIS FUNCTION
complicationLocation: ComplicationLocation
): IntArray {
when (complicationLocation) {
LEFT -> return COMPLICATION_SUPPORTED_TYPES[0]
RIGHT -> return COMPLICATION_SUPPORTED_TYPES[1]
TOP -> return COMPLICATION_SUPPORTED_TYPES[2]
BOTTOM -> return COMPLICATION_SUPPORTED_TYPES[3]
else -> return intArrayOf()
}
}
//---
/*
* Update rate in milliseconds for interactive mode. We update once a second to advance the
* second hand.
*/
private val INTERACTIVE_UPDATE_RATE_MS = TimeUnit.SECONDS.toMillis(1)
override fun onCreateEngine(): Engine {
return Engine()
}
private class EngineHandler(reference: Engine) : Handler() { //
private val mWeakReference: WeakReference<Engine> = WeakReference(reference)
override fun handleMessage(msg: Message) {
val engine = mWeakReference.get()
if (engine != null) {
when (msg.what) {
MSG_UPDATE_TIME -> engine.handleUpdateTimeMessage()
}
}
}
}
inner class Engine : CanvasWatchFaceService.Engine() {}
答案 0 :(得分:3)
您像 static 方法那样调用getComplicationId
,但是它是 Instance 方法。
要解决此问题,请将该方法(及其使用的变量)放在Companion Object中:
class MyWatchFace : CanvasWatchFaceService() {
companion object{
private val LEFT_COMPLICATION_ID = 0
private val RIGHT_COMPLICATION_ID = 1
private val TOP_COMPLICATION_ID = 2
private val BOTTOM_COMPLICATION_ID = 3
internal fun getComplicationId(complicationLocation: ComplicationLocation): Int {
when (complicationLocation) {
ComplicationLocation.LEFT -> return LEFT_COMPLICATION_ID
ComplicationLocation.RIGHT -> return RIGHT_COMPLICATION_ID
ComplicationLocation.TOP -> return TOP_COMPLICATION_ID
ComplicationLocation.BOTTOM -> return BOTTOM_COMPLICATION_ID
else -> return -1
}
}
}
}