Tag: android context

在片段中使用上下文

我怎样才能得到一个片段的上下文? 我需要使用我的数据库的构造函数在上下文中,但getApplicationContext()和FragmentClass.this不工作,所以我能做什么? 数据库构造函数 public Database(Context ctx) { this.context = ctx; DBHelper = new DatabaseHelper(context); }

Android – SharedPreferences – 上下文

我想在android中使用kotlin为我的SharedPreference创建一个辅助类。 不幸的是,我需要Context ,我不希望每次调用一个首选项时将其设置为参数。 如果我使用上下文的伴侣对象,并将其设置在应用程序启动,我得到以下错误: Do not place Android context classes in static fields; this is a memory leak (and also breaks Instant Run) Do not place Android context classes in static fields; this is a memory leak (and also breaks Instant Run) 那么如何获得上下文,而不是每次调用偏好时都通过它? var isWorking: Boolean get() = getBoolean(IS_WORKING) set(isWorking) = setPreference(IS_WORKING, isWorking) private fun […]

Kotlin显示片段中的types不匹配

里面有一个RecyclerAdapter的片段。 我想在onCreateView方法中初始化适配器,但是在这个语句中抛出了”Type mismatch. Required : Context , Found : FragmentActivity”的错误 我不知道为什么第一个显示这个错误,第二个没有包含编译时错误。 显示错误 recyclerView!!.adapter = RestaurantMenuAdapter(activity) 没有错误显示 recyclerView!!.layoutManager = LinearLayoutManager(activity) Fragment.kt override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { // Inflate the layout for this fragment val view = inflater.inflate(R.layout.fragment_restaurant_menu, container, false) recyclerView = view.findViewById(R.id.restaurant_container) recyclerView!!.adapter = RestaurantMenuAdapter(activity) recyclerView!!.layoutManager = LinearLayoutManager(activity) RecyclerAdapter.kt class RestaurantMenuAdapter […]