Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
249 views
in Technique[技术] by (71.8m points)

java - Type mismatch. Required: FirebaseRecyclerAdapter<ChatObject, ChatVoiceViewHolders>? Found:

I am porting an Java Code to Kotlin and It's throwing an Type Miss matched error.

Java Code :

private FirebaseRecyclerAdapter<ChatObject, ChatVoiceViewHolders> firebaseRecyclerAdapterChats;
    firebaseRecyclerAdapterChats = new FirebaseRecyclerAdapter<ChatObject, ChatVoiceViewHolders>(
            ChatObject.class,
            R.layout.item_messages_video_call,
            ChatVoiceViewHolders.class,
            dbMessagingMy
    ) {}

Kotlin Code :

private var firebaseRecyclerAdapterChats: FirebaseRecyclerAdapter<ChatObject, ChatVoiceViewHolders>? = null
    firebaseRecyclerAdapterChats = object : FirebaseRecyclerAdapter<ChatObject?, ChatVoiceViewHolders?>(
            ChatObject::class.java,
            R.layout.item_messages_video_call,
            ChatVoiceViewHolders::class.java,
            dbMessagingMy
    ) {}

but my Koltin code is throwing an error :

Type mismatch.
Required:
FirebaseRecyclerAdapter<ChatObject, ChatVoiceViewHolders>?
Found:

None of the following functions can be called with the arguments supplied.

(Class<ChatObject?>!, Int, Class<ChatVoiceViewHolders?>!, FirebaseArray!) ?? where T = ChatObject?, VH = ChatVoiceViewHolders? for constructor FirebaseRecyclerAdapter<T : Any!, VH : RecyclerView.ViewHolder!>(modelClass: Class<T!>!, modelLayout: Int, viewHolderClass: Class<VH!>!, snapshots: FirebaseArray!) defined in com.firebase.ui.database.FirebaseRecyclerAdapter

(Class<ChatObject?>!, Int, Class<ChatVoiceViewHolders?>!, Query!) ?? where T = ChatObject?, VH = ChatVoiceViewHolders? for ?? constructor FirebaseRecyclerAdapter<T : Any!, VH : RecyclerView.ViewHolder!>(modelClass: Class<T!>!, modelLayout: Int, viewHolderClass: Class<VH!>!, ref: Query!) defined in com.firebase.ui.database.FirebaseRecyclerAdapter

One more point, In my Java code I am working with AppcompatActivity and In my Kotlin Code I am working with BaseActivity..

Is there anything I am doing wrong? how can I solve this?

thanks.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You have declared your variable as FirebaseRecyclerAdapter<ChatObject, ChatVoiceViewHolders> but then you try to set value to it which is FirebaseRecyclerAdapter<ChatObject?, ChatVoiceViewHolders?>.

So change

object : FirebaseRecyclerAdapter<ChatObject?, ChatVoiceViewHolders?> {

to this

object : FirebaseRecyclerAdapter<ChatObject, ChatVoiceViewHolders> {

The reason why this gives you an error is because Kotlin is more strict about nullability rules. Java does not care so much about it. You were saying that I declare a variable which can have only non-null values but then after that you try to assign a value to it which can have null values.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...