Tag: android fragments

如何根据选定的项目加载BottomNavigationView片段?

我有一个简单的应用程序写在Kotlin有一个BottomNavigationView。 这个想法是有一个单一的活动(MainActivity)与BottomNavigationView的每个选项卡加载片段。 我已经创建了一个片段,当选择一个选项卡(HomeFragment)时,我想加载一个片段,并且当活动选项卡更改时,已经在BottomNavigationView的图标下更改了文本。 现在我想在改变标签时膨胀/加载片段。 我怎么去做这个? MainActivity.kt: class MainActivity : AppCompatActivity() { private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item -> when (item.itemId) { R.id.navigation_enrollments -> { message.setText(R.string.title_enrollments) return@OnNavigationItemSelectedListener true } R.id.navigation_timeline -> { message.setText(R.string.title_timeline) return@OnNavigationItemSelectedListener true } R.id.navigation_home -> { message.setText(R.string.title_home) return@OnNavigationItemSelectedListener true } R.id.navigation_alerts -> { message.setText(R.string.title_alerts) return@OnNavigationItemSelectedListener true } R.id.navigation_profile -> { message.setText(R.string.title_profile) return@OnNavigationItemSelectedListener […]

当重新创建Activity时,lateinit属性不会被初始化

在我的活动我有一个lateinit属性称为controller ,我的片段使用。 该属性在Activity.onCreate()初始化。 我的Fragment通过onAttach()获取其参考。 然后Fragment在Fragment.onCreate()调用myActivity.controller 。 通常controller首先在Activity.onCreate()初始化,然后添加Fragment 。 所以这工作得很好。 但是,当我的Activity被杀害,它试图重新创建自己和它的碎片。 这导致在Activity.onCreate()初始化之前调用Fragment.onCreate() Activity.onCreate() 。 这些是我现在看到的选项: 在super.onCreate()之前初始化controller (如果甚至可能的话) 将调用移动到myActivity.controller到更高的生命周期回调,如onViewCreated() 在Kotlin 1.2中有::controller.isInitialized 这里我最好的选择是什么?

Android:Kotlin TypeCastException:null不能转换为非nulltypes的kotlin.String

我是Kotlin的新手,我正在尝试开发一个音乐应用程序。 我得到这个我无法解决的错误。 当我打开我的应用程序时,歌曲列表显示,点击歌曲重定向到现在显示屏幕,但点击下一个或上一个按钮我的应用程序崩溃。 logcat是一个TypeCast错误,我不明白。 代码如下: class SongPlayingFragment : Fragment() { var myActivity: Activity? = null var mediaplayer: MediaPlayer? = null var startTimeText: TextView? = null var endTimeText: TextView? = null var playpauseImageButton: ImageButton? = null var previousImageButton: ImageButton? = null var nextImageButton: ImageButton? = null var loopImageButton: ImageButton? = null var seekbar: SeekBar? = null […]

为什么设置onClickListener一次工作?

我想把一个非常坚实的项目结构迁移到Kotlin上。 我首先尝试了基础:活动和片段交易。 看起来如此简单和容易: class MainActivity : AppCompatActivity(), SomeInterface { override fun onCreate(savedInstanceState: Bundle?) { setContentView(R.layout.activity_main) val mainFragment = supportFragmentManager.findFragmentById(R.id.fragment_main) as MainActionsFragment? ?: MainActionsFragment.newInstance() supportFragmentManager.inTransaction { add(R.id.container_main, mainFragment) } } private val anotherFragment by lazy { supportFragmentManager.findFragmentById(R.id.another_fragment) as AnotherFragment? ?: AnotherFragment.newInstance() } override fun myInterfaceMethod() { replaceFragment(anotherFragment, R.id.container_main) } } class MainActionsFragment : Fragment() { val […]

键盘隐藏BottomSheetDialogFragment

键盘下方有更多的字段。 这发生在我更新支持库时。 我知道这是Kotlin,但它看起来几乎与Java相同。 我该如何解决这个问题? 这是它的样子: 我的代码: class ProjectsEditBottomSheetFragment(val privateID: String, val publicID: String) : BottomSheetDialogFragment() { private val mBottomSheetBehaviorCallback = object : BottomSheetBehavior.BottomSheetCallback() { override fun onStateChanged(bottomSheet: View, newState: Int) { if (newState == BottomSheetBehavior.STATE_HIDDEN) { dismiss() } } override fun onSlide(bottomSheet: View, slideOffset: Float) { if (slideOffset < -0.15f) { dismiss() } } } […]

将对象列表传递给片段

class CholesterolPagingFragment: Fragment() { companion object { fun newInstance(): CholesterolPagingFragment { val args = Bundle() val fragment = CholesterolPagingFragment() fragment.arguments = args return fragment } } override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { val view = inflater.inflate(R.layout.fragment_paging_cholesterol, container, false) return view } } 我已经写了上面的代码在Kotlin初始化一个片段。 虽然我不能找出一种方法来传递对象列表(例如: List )到这个片段。 我已经尝试与Bundle()但找不到一个正确的方法。

Android:片段上的按钮

嗨,我刚刚开始与Kotlin的Android应用程序有一个小问题,我的应用程序 当我尝试使用OnclickListener时,我的应用程序崩溃 我的代码 class fr_cooking : Fragment() { override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? { button.setOnClickListener({ textView2.text = “blablabla” }) return inflater!!.inflate(R.layout.fragment_fr_cooking, container, false) } }

片剂上的片段视图为空

我正在尝试从Android Developers网站获得的Fragment基础源代码。 我通过使用代码>转换为Kotlinfunction将代码转换为Kotlin,并尝试在Nexus 10 API 23仿真器平板电脑上运行。 我在这一行的ArticleFragment中出现错误: fun updateArticleView(position: Int) { //here, there is no view in activity for ID article, so it’s null val article = activity.findViewById(R.id.article) as TextView article.text = Ipsum.Articles[position] mCurrentPosition = position } 错误是: kotlin.TypeCastException: null cannot be cast to non-null type android.widget.TextView at com.example.fragmentbasics.ArticleFragment.updateArticleView(ArticleFragment.kt:45) 我试着调试它,当它应该是null时,活动中没有View。 我错过了什么?

点击一个文本视图来改变片段

我正在尝试使用Kotlin构建我的第一个Android应用程序,但是我陷入了一个非常简单的情况。 我正在使用Kotlin android扩展,我得到一个空指针exception。 用这个我用了 tvGoToRegisterFrag.setOnClickListener { goToRegister() } 我知道我可以使用 tvGoToRegisterFrag?.setOnClickListener { goToRegister() } 为了摆脱崩溃,但我仍然不知道为什么该对象是空的,单击时不做任何事情。 所有这些都是在片段中完成的,如果这改变了任何东西。

SearchView.onQueryTextListener在Fragment(Kotlin)中不起作用

我提出的问题是关于事件onQueryTextListener ,我声明它是以前在java中使用它,但是当我提交文本或当我更改文本时它不显示日志。 这是我的片段的代码: Fragment_producto.kt override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? { val vG = inflater!!.inflate(R.layout.fragment_producto, container, false) vG.recycler_producto.layoutManager = LinearLayoutManager(activity) vG.recycler_producto.hasFixedSize() vG.recycler_producto.adapter = Producto_Adapter(activity,this) vG.fab_scan.onClick { IntentIntegrator.forSupportFragment(this@Fragment_producto).initiateScan() } adapter = vG.recycler_producto.adapter as Producto_Adapter metodos.attachSwipeCheck(vG.recycler_producto) metodos.attachSwipeWrong(vG.recycler_producto) setHasOptionsMenu(true) return vG } override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { Log.i(TAG,”Llego a create optionsmenu”) activity.menuInflater.inflate(R.menu.menu_producto,menu) /*val […]