在 kotlin 中编译时我得到未解决的参考
I am getting Unresolved Reference when compiling in kotlin
我是 kotlin 的新手,编译时我得到了未解决的参考
它无法获得对我的 class 的引用,这里有两个 classes.
这是我的Activity
class
RoomActivity.kt
class RoomActivity : AppCompatActivity() {
val NEW_WORD_ACTIVITY_REQUEST_CODE = 1
// private var mWordViewModel: WordViewModel? = null
private var mWordViewModel: WordViewModel?=null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_room)
val toolbar = findViewById<Toolbar>(R.id.toolbar)
setSupportActionBar(toolbar)
val recyclerView = findViewById<RecyclerView>(R.id.recyclerview)
val adapter = WordListAdapter(this)
recyclerView.adapter = adapter
recyclerView.layoutManager = LinearLayoutManager(this)
// Get a new or existing ViewModel from the ViewModelProvider.
mWordViewModel = ViewModelProviders.of(this).get(WordViewModel::class.java)
// Add an observer on the LiveData returned by getAlphabetizedWords.
// The onChanged() method fires when the observed data changes and the activity is
// in the foreground.
mWordViewModel!!.allWords.observe(this, Observer<List<Word>> {
adapter.setWords(Word)
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
})
val fab = findViewById<FloatingActionButton>(R.id.fab)
fab.setOnClickListener(object : View.OnClickListener {
override fun onClick(view: View) {
val intent = Intent(this@RoomActivity, NewWordActivity::class.java)
startActivityForResult(intent, NEW_WORD_ACTIVITY_REQUEST_CODE)
}
})
}
public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == NEW_WORD_ACTIVITY_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
val word = Word(data!!.getStringExtra(NewWordActivity.EXTRA_REPLY))
mWordViewModel!!.insert(word)
} else {
Toast.makeText(
applicationContext,
R.string.empty_not_saved,
Toast.LENGTH_LONG).show()
}
}
}
这是我的ViewModel
class
WordViewModel.kt
class WordViewModel(application: Application) : AndroidViewModel(application) {
private var parentJob = Job()
private val coroutineContext: CoroutineContext
get() = parentJob + Dispatchers.Main
private val scope = CoroutineScope(coroutineContext)
private val repository: WordRepository
val allWords: LiveData<List<Word>>
init {
val wordsDao = WordRoomDatabase.getDatabase(application)?.wordDao()
repository = WordRepository(wordsDao!!)
allWords = repository.allWords
}
fun insert(word: Word) = scope.launch(Dispatchers.IO) {
repository.insert(word)
}
override fun onCleared() {
super.onCleared()
parentJob.cancel()
}
}
有什么问题请帮帮我。
AndroidViewModel - 当您需要上下文时使用它 class 其他时候使用 ViewModel() class。
如果您使用的是 androidviewmodel 或 viewmodel,则以下代码均有效。
class UserViewModel : ViewModel() {
lateinit var userList: LiveData<PagedList<User>>
private val compositeDisposable = CompositeDisposable()
private val pageSize = 10
lateinit var sourceFactory: UsersDataSourceFactory
lateinit var productList:LiveData<PagedList<ProductsItem>>
var tDataSource: LiveData<ProductListSource>? = null
lateinit var productListSource:ProductDataSourceFactory
var networkState: LiveData<NetworkState>? = null
var productData: LiveData<Product>? = null
fun init(apiListener: ApiListener) {
sourceFactory = UsersDataSourceFactory(compositeDisposable, apiListener)
val config = PagedList.Config.Builder()
.setPageSize(pageSize)
.setEnablePlaceholders(false)
.build()
userList = LivePagedListBuilder<Long, User>(sourceFactory, config).build()
}
fun getProductData(apiListener: ApiListener){
productListSource= ProductDataSourceFactory(apiListener)
tDataSource = productListSource.mutableLiveData
networkState = Transformations.switchMap(productListSource.mutableLiveData) { dataSource -> dataSource.networkState }
productData = Transformations.switchMap(productListSource.mutableLiveData) { dataSource -> dataSource.productList }
val config=PagedList.Config.Builder()
.setInitialLoadSizeHint(5)
.setPageSize(pageSize)
.setEnablePlaceholders(false)
.build()
productList=LivePagedListBuilder<Long,ProductsItem>(productListSource,config).build()
}
override fun onCleared() {
super.onCleared()
compositeDisposable.dispose()
}
fun retryGetProduct() {
productListSource.mutableLiveData.value!!.retry()
}
fun retry() {
sourceFactory.usersDataSourceLiveData.value!!.retry()
}
fun refresh() {
sourceFactory.usersDataSourceLiveData.value!!.invalidate()
}
/*
有趣的 getNetworkStateData(): LiveData = Transformations.switchMap(
productListSource.mutableLiveData, { it.networkState })
fun getNetworkState(): LiveData<NetworkState> = Transformations.switchMap<UsersDataSource, NetworkState>(
sourceFactory.usersDataSourceLiveData, { it.networkState })
fun getRefreshState(): LiveData<NetworkState> = Transformations.switchMap<UsersDataSource, NetworkState>(
sourceFactory.usersDataSourceLiveData, { it.initialLoad })*/
}
这样访问..
viewModel = ViewModelProviders.of(this).get(UserViewModel::class.java)
如果您从另一个项目复制文件 WordViewModel.kt
,请确保将第一行中的包名称更改为您在项目中使用的名称。
package application.hdlakhani.com.myapplication
你的回答已经很晚了,但我认为它会对某人有所帮助。
我今天遇到了同样的问题,我通过重建我的项目解决了它。
只需转到:Build -> Rebuild Project
我是 kotlin 的新手,编译时我得到了未解决的参考
它无法获得对我的 class 的引用,这里有两个 classes.
这是我的Activity
class
RoomActivity.kt
class RoomActivity : AppCompatActivity() {
val NEW_WORD_ACTIVITY_REQUEST_CODE = 1
// private var mWordViewModel: WordViewModel? = null
private var mWordViewModel: WordViewModel?=null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_room)
val toolbar = findViewById<Toolbar>(R.id.toolbar)
setSupportActionBar(toolbar)
val recyclerView = findViewById<RecyclerView>(R.id.recyclerview)
val adapter = WordListAdapter(this)
recyclerView.adapter = adapter
recyclerView.layoutManager = LinearLayoutManager(this)
// Get a new or existing ViewModel from the ViewModelProvider.
mWordViewModel = ViewModelProviders.of(this).get(WordViewModel::class.java)
// Add an observer on the LiveData returned by getAlphabetizedWords.
// The onChanged() method fires when the observed data changes and the activity is
// in the foreground.
mWordViewModel!!.allWords.observe(this, Observer<List<Word>> {
adapter.setWords(Word)
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
})
val fab = findViewById<FloatingActionButton>(R.id.fab)
fab.setOnClickListener(object : View.OnClickListener {
override fun onClick(view: View) {
val intent = Intent(this@RoomActivity, NewWordActivity::class.java)
startActivityForResult(intent, NEW_WORD_ACTIVITY_REQUEST_CODE)
}
})
}
public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == NEW_WORD_ACTIVITY_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
val word = Word(data!!.getStringExtra(NewWordActivity.EXTRA_REPLY))
mWordViewModel!!.insert(word)
} else {
Toast.makeText(
applicationContext,
R.string.empty_not_saved,
Toast.LENGTH_LONG).show()
}
}
}
这是我的ViewModel
class
WordViewModel.kt
class WordViewModel(application: Application) : AndroidViewModel(application) {
private var parentJob = Job()
private val coroutineContext: CoroutineContext
get() = parentJob + Dispatchers.Main
private val scope = CoroutineScope(coroutineContext)
private val repository: WordRepository
val allWords: LiveData<List<Word>>
init {
val wordsDao = WordRoomDatabase.getDatabase(application)?.wordDao()
repository = WordRepository(wordsDao!!)
allWords = repository.allWords
}
fun insert(word: Word) = scope.launch(Dispatchers.IO) {
repository.insert(word)
}
override fun onCleared() {
super.onCleared()
parentJob.cancel()
}
}
有什么问题请帮帮我。
AndroidViewModel - 当您需要上下文时使用它 class 其他时候使用 ViewModel() class。 如果您使用的是 androidviewmodel 或 viewmodel,则以下代码均有效。
class UserViewModel : ViewModel() {
lateinit var userList: LiveData<PagedList<User>>
private val compositeDisposable = CompositeDisposable()
private val pageSize = 10
lateinit var sourceFactory: UsersDataSourceFactory
lateinit var productList:LiveData<PagedList<ProductsItem>>
var tDataSource: LiveData<ProductListSource>? = null
lateinit var productListSource:ProductDataSourceFactory
var networkState: LiveData<NetworkState>? = null
var productData: LiveData<Product>? = null
fun init(apiListener: ApiListener) {
sourceFactory = UsersDataSourceFactory(compositeDisposable, apiListener)
val config = PagedList.Config.Builder()
.setPageSize(pageSize)
.setEnablePlaceholders(false)
.build()
userList = LivePagedListBuilder<Long, User>(sourceFactory, config).build()
}
fun getProductData(apiListener: ApiListener){
productListSource= ProductDataSourceFactory(apiListener)
tDataSource = productListSource.mutableLiveData
networkState = Transformations.switchMap(productListSource.mutableLiveData) { dataSource -> dataSource.networkState }
productData = Transformations.switchMap(productListSource.mutableLiveData) { dataSource -> dataSource.productList }
val config=PagedList.Config.Builder()
.setInitialLoadSizeHint(5)
.setPageSize(pageSize)
.setEnablePlaceholders(false)
.build()
productList=LivePagedListBuilder<Long,ProductsItem>(productListSource,config).build()
}
override fun onCleared() {
super.onCleared()
compositeDisposable.dispose()
}
fun retryGetProduct() {
productListSource.mutableLiveData.value!!.retry()
}
fun retry() {
sourceFactory.usersDataSourceLiveData.value!!.retry()
}
fun refresh() {
sourceFactory.usersDataSourceLiveData.value!!.invalidate()
}
/* 有趣的 getNetworkStateData(): LiveData = Transformations.switchMap( productListSource.mutableLiveData, { it.networkState })
fun getNetworkState(): LiveData<NetworkState> = Transformations.switchMap<UsersDataSource, NetworkState>(
sourceFactory.usersDataSourceLiveData, { it.networkState })
fun getRefreshState(): LiveData<NetworkState> = Transformations.switchMap<UsersDataSource, NetworkState>(
sourceFactory.usersDataSourceLiveData, { it.initialLoad })*/
}
这样访问..
viewModel = ViewModelProviders.of(this).get(UserViewModel::class.java)
如果您从另一个项目复制文件 WordViewModel.kt
,请确保将第一行中的包名称更改为您在项目中使用的名称。
package application.hdlakhani.com.myapplication
你的回答已经很晚了,但我认为它会对某人有所帮助。
我今天遇到了同样的问题,我通过重建我的项目解决了它。
只需转到:Build -> Rebuild Project