Tag: 安卓

Android Kotlin:TextView.text无法正常工作

我有一个让用户使用Firebase登录的应用,如果他们尚未登录。如果用户登录其用户个人资料信息显示正确。 在关闭应用程序并重新打开用户名和电子邮件不会设置,并进入我的默认文本。 但是,如果我退出并返回将显示。 我觉得它与更新用户界面有关,但卡住了。 如果需要,我可以在午餐rest后发布代码 用代码更新 这是MainActivity中的相关代码,这是我检查用户是否登录,如果允许他们从那里去,如果不开始登录活动。 class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener { val TAG = “MainActivity” var mAuth: FirebaseAuth? = null var mUser: FirebaseUser? = null var userEmailTV: TextView? = null var userDispNameTV: TextView? = null val RC_SIGN_IN = 123 override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) setSupportActionBar(toolbar) val toggle = ActionBarDrawerToggle( this, drawer_layout, […]

如何用kotlin标记List

我想要通过Bundle传递一个数据类(包含int作为一个属性列表)到其他活动,因此我需要添加Parcelable实现到我的数据类。 任何想法如何包裹这个属性? data class Test(val id: Long, val files: List?) : Parcelable { constructor(parcel: Parcel) : this( parcel.readLong(), TODO(“files”)) override fun writeToParcel(parcel: Parcel, flags: Int) { parcel.writeLong(id) } override fun describeContents(): Int { return 0 } companion object CREATOR : Parcelable.Creator { override fun createFromParcel(parcel: Parcel): Test { return Test(parcel) } override fun newArray(size: Int): […]

Android Rx-java + Kotlin的Retrofit2错误

我正在尝试构建一个MVVM模式的Android应用程序。 除了rx-java部分,一切都很好。 在订阅中使用Observer,我有这样的错误。 Error:(28, 18) None of the following functions can be called with the arguments supplied: public final fun subscribe(p0: ((Flyer!) -> Unit)!): Subscription! defined in rx.Observable public final fun subscribe(p0: Observer!): Subscription! defined in rx.Observable public final fun subscribe(p0: Subscriber!): Subscription! defined in rx.Observable public final fun subscribe(p0: Action1!): Subscription! defined in rx.Observable […]

Dagger2限定符不能与Kotlin一起使用?

我有一个简单的类如下 class MainString(val msg: String) 我想注入不同的参数,所以我使用@Named限定符根据https://google.github.io/dagger/users-guide 与我的AppModule有 @Provides @Named(“Two”) fun provideTwoMainString(): MainString { return MainString(“Two”) } @Provides @Named(“One”) fun provideOneMainString(): MainString { return MainString(“One”) } 在我的主动活动中,我只是打电话 @Inject @Named(“One”) lateinit var stringOne: MainString @Inject @Named(“Two”) lateinit var stringTwo: MainString 然而,当我编译时,它抱怨 Error:(11, 1) error: com.elyeproj.demo_dagger_scope.MainString cannot be provided without an @Inject constructor or from an @Provides- or […]

IllegalStateException textview不能在Kotlin中为null

我试图创建一个方法来更新用户界面,当活动开始通过在抽屉中设置TextView的文本。 我不断收到一个TextView不能为空的错误? 我得到的错误是非法状态exceptionuserEmail不能为空 这是更新导航抽屉中的配置文件信息的updateUI private fun updateUI(user:FirebaseUser?) { if (user != null) { val userEmailText = findViewById(R.id.userEmail) userEmail.text = user.email val errorTriage : String? = user.email println(“User Email is =”+errorTriage) } else { val userEmailText: TextView = findViewById(R.id.userEmail) userEmailText.text = “Johndoe@someemail.com” userNameText.text = “John Doe” }} 如果用户已经登录,那么这是对UpdateUI的调用,如果更新导航抽屉中的配置文件数据,这是什么问题。 如果没有,则启动FirebaseUI登录活动。 override fun onStart() { super.onStart() //Checks to […]

kotlin测试whith mockito:比较失败

我试图用简单的测试主持人来覆盖,看起来像下一个: class Presenter{ fun getData(params:SomeParams) { usecase.execute(getObservable, params) } private fun getObservable() = object :DisposableObserver{ override fun onComplete() {} override fun onNext(t:SomeData) {} override fun onError(e: Throwable) {} } } 这是我的简单测试: @Test fun getContacts() { presenter.getData() var observer = Mockito.mock(DisposableObserver::class.java) as DisposableObserver verify(useCase).execute(observer, someParams) } 和最后一行后我得到下一个错误: Argument(s) are different! Wanted: useCase.execute( com.test.PresenterTest$getObservable$o$1@579d011c, kotlin.Unit ); -> […]

Gson toJson和ArrayList在Kotlin

类似于这个问题,我想将一个对象(实际上,它是一个来自翻新的API响应)转换成json字符串,所以将它存储在某个地方会更简单。 响应结构是这样的: { “metadata”: { “count”: 0, “type”: “string” }, “results”: [ { “obj1”: { “param1”: “s1”, “param2”: “s2” }, “obj2”: { “param3”: 0, “param4”: 0, “param5”: 0 }, “obj3”: 0, “obj4”: “27/12/2017” } ] } 使用List ,我将结果数组存储在一个List ,这是我传递给Gson().toJson ,如下所示: var contentResponse: String = “” try{ this.contentResponse.plus(Gson().toJson(response)) } catch (e: Exception){ Log.e(“Gson error”, e.toString()) } […]

在Android中使用kotlin进行自定义视图的类DSL的OnClick监听器

我正在尝试创建一个类似DSL的OnClick监听器(在kotlin中),用于在我正在使用的Android项目中使用多个位置的自定义视图。 该视图具有ImageView,主要textview和辅助textview。 我正在试图创建一个监听器帮手,它允许你只覆盖一个接口的特定方法,而不是所有的( 本文的启发)。 但是我无法得到它的工作。 实际上它甚至不能使用普通的OnClick监听器。 这是我的ErrorMessageView: class ErrorMessageView @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyle: Int = 0, defStyleRes: Int = 0 ) : FrameLayout(context, attrs, defStyle, defStyleRes) { private var mOnClickListener: OnErrorMessageViewClickListener? = null init { LayoutInflater.from(context).inflate( R.layout.custom_errorview, this, true) ButterKnife.bind(this) } interface OnErrorMessageViewClickListener { fun onImageClick() fun onPrimaryTextClick() fun onSecondaryTextClick() […]

Android中的Kotlin“内部”​​可见性修改器

假设你正在编写一个Android项目( 而不是一个库 )。 所有的文件都汇编在一起,所以…在这种情况下使用internal可见性修饰符是否有意义?

Kotlintypes错配。 预期任何收到的MyObject

我正在制作一个可观察的LiveData对象列表,它应该包含Resource对象( https://developer.android.com/topic/libraries/architecture/guide.html#addendum )。 我不关心Resource对象所包含的数据types。 abstract class LiveResources : LiveData<Resource>() { private val mediatorLiveData = MediatorLiveData<Resource>() protected val resources = HashSet<LiveData<Resource>>() fun addResource(source: LiveData<Resource>) { resources.add(source) mediatorLiveData.addSource(source, resourceObserver) } fun removeResource(source: LiveData<Resource>) { resources.remove(source) mediatorLiveData.removeSource(source) } private val resourceObserver = Observer<Resource> { onSourceChange() } abstract fun onSourceChange() } 不幸的是,当我尝试使用LiveResources.addResource()与LiveData<Resource<List>>我得到TypeMismatch错误在我的IDE,说LiveData<Resource>是预期的。