Call Super Throws "super Is Not An Expression"
I took up for learning implementing MVVM from Google Guide here:
https://codelabs.developers.google.com/codelabs/android-room-with-a-view/#8 (posted link especially to page I'm interested in).
Since I understood implementing it in Java, I decided to switch to Kotlin.
While initializing constructor in class extending AndroidViewModel
I need to call super
and it throws me following error:
"super' is not an expression it can only be used on the left-hand side of a dot ('.')"
As I googled and found similar topic but I haven't understood it at all so I didn't solve my problem.
This my code for ViewModel
class:
class NotesViewModel private constructor(application: Application) : AndroidViewModel(application){
var mRepository: NotesRepository? = null
var mAllNotes: LiveData<List<Notes>>? = null
init {
super(application) // <-- here it throws me an error
mRepository = NotesRepository(application)
mAllNotes = mRepository!!.getAllWords()
}
fun getAllNotes(): LiveData<List<Notes>>{
return mAllNotes!!
}
fun insert(notes: Notes){
mRepository!!.insert(notes)
}
}
So, how should I properly call super, construct a constructor? This is proper java code for this class:
public class WordViewModel extends AndroidViewModel {
private WordRepository mRepository;
private LiveData<List<Word>> mAllWords;
public WordViewModel(Application application) {
super(application);
mRepository = new WordRepository(application);
mAllWords = mRepository.getAllWords();
}
LiveData<List<Word>> getAllWords() {
return mAllWords;
}
void insert(Word word) {
mRepository.insert(word);
}
}
Answer
You already call super here : NotesViewModel private constructor(application: Application) : AndroidViewModel(application)
Another problem is that your constructor is private
Simply make it public
and remove super
call from init()
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM