Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从片段调用startActionMode_Android_Kotlin_Android Fragments_Contextmenu - Fatal编程技术网

Android 从片段调用startActionMode

Android 从片段调用startActionMode,android,kotlin,android-fragments,contextmenu,Android,Kotlin,Android Fragments,Contextmenu,我试图在点击一个按钮时从一个片段启动上下文动作模式菜单。我不知道如何从片段中的onClick方法调用“startActionMode()” 我的碎片 class MyFragment : Fragment() { ... private var mActionMode: ActionMode? = null // when this button is clicked it should launch the Contextual Action Ba

我试图在点击一个按钮时从一个片段启动上下文动作模式菜单。我不知道如何从片段中的onClick方法调用“startActionMode()”

我的碎片

class MyFragment : Fragment() {
    ...
    
    private var mActionMode: ActionMode? = null 
    
    // when this button is clicked it should launch the Contextual Action Bar (CAB) 
    fun myCAB() {
        // Check to see CAB is currenty active. 
        if(mActionMode != null) return        
        mActionMode = mActivity.startActionMode(actionModeCallback)
    }

    // ActionMode.Callback interface, handles actions for contextual CAB
    private val actionModeCallback: ActionMode.Callback = object : ActionMode.Callback {
         ... 
    }
}
我试过使用

  • 反应性
  • 从onAttach引用活动(活动:活动)
两个选项都返回类型未匹配。 必需:androidx.appcompt.view.ActionMode?
找到:android.view.ActionMode?

您可以强制执行该活动,然后调用startSupportAction模式

mActionMode = (activity as MainActivity?)!!.startSupportActionMode(actionModeCallback)
下面是代码中的内容

class MyFragment : Fragment() {
    ...

    private var mAction: ActionMode? = null

    fun myCAB() {
        if(mActionMode!= null) return
      
        // as casts the type if is successful or returns null
        mAction = (activity as MainActiivty?)!!.startSupportActionMode(actionModeCallback)
    } 

    private val actionModeCallback: ActionMode.Callback = object : ActionModeCallback {
        ... 
    }
}


将导入android.view.ActionMode的导入更改为导入androidx.appcompt.view.ActionMode我当前正在导入androidx.appcomp.view.ActionMode。