我正在尝试使用Android文档中描述的LiveData实现DB Observer .
只要我在Kotlin编程,我就会调整函数(最初用Java编写).
在尝试保存数据时,我发现了这个问题.
Cannot assign to ‘value’: the setter is protected/*protected and package*/ for synthetic extension in ‘<library Grade: android.arch.livecycle:livedata-core-1.1.1>’
Run Code Online (Sandbox Code Playgroud)
有人有这个问题吗?
这是我的代码:
视图模型:
class ProfileViewModel: ViewModel() {
object FirstName: MutableLiveData<String>()
fun getCurrentName(): LiveData<String> {
return FirstName
}
}
Run Code Online (Sandbox Code Playgroud)
分段
class ProfileFragment{
private lateinit var model: ProfileViewModel
// this is called onViewCreated. inputFirstName is an Edittext.
override fun setUp() {
model = ViewModelProviders.of(this).get(ProfileViewModel::class.java)
val nameObserver = Observer<String> { firstName ->
inputFirstName.text = SpannableStringBuilder(firstName)
}
model.getCurrentName().observe(this, nameObserver)
} …Run Code Online (Sandbox Code Playgroud) android kotlin android-livedata android-architecture-components
我正在尝试通过Retrofit2执行Multipart POST请求,我将API上传到自定义文件.
它随机失败,出现此异常:
W/System.err: java.net.ProtocolException: expected 154 bytes but received 634
Run Code Online (Sandbox Code Playgroud)
有人能说些什么吗?
这是我在界面中的代码:
@Multipart
@POST("recordings/{id}/{rec_id}/")
Call<ResponseBody> uploadRecording(@Path("id") String id, @Path("rec_id") String rec_id, @Part MultipartBody.Part bleFile);
Run Code Online (Sandbox Code Playgroud)
在构造函数中:
public ApiConnectionManager(Context con){
Gson gson = new GsonBuilder()
.setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
.create();
OkHttpClient.Builder client = new OkHttpClient.Builder();
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
client.addInterceptor(loggingInterceptor);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(con.getResources().getString(R.string.api_url)) // API url is hidden
.addConverterFactory(GsonConverterFactory.create(gson))
.client(client.build())
.build();
this.companyAPI = retrofit.create(CompanyAPI.class);
}
Run Code Online (Sandbox Code Playgroud)
并在上传方法:
private void uploadFile(String id, final File bleFile) {
MediaType MEDIA_TYPE = MediaType.parse("multipart/mixed");
RequestBody …Run Code Online (Sandbox Code Playgroud) 显然,Room无法处理MutableLiveData,我们必须坚持使用LiveData,因为它返回以下错误:
error: Not sure how to convert a Cursor to this method's return type
Run Code Online (Sandbox Code Playgroud)
我用这种方式在我的数据库助手中创建了一个"自定义"MutableLiveData:
class ProfileRepository @Inject internal constructor(private val profileDao: ProfileDao): ProfileRepo{
override fun insertProfile(profile: Profile){
profileDao.insertProfile(profile)
}
val mutableLiveData by lazy { MutableProfileLiveData() }
override fun loadMutableProfileLiveData(): MutableLiveData<Profile> = mutableLiveData
inner class MutableProfileLiveData: MutableLiveData<Profile>(){
override fun postValue(value: Profile?) {
value?.let { insertProfile(it) }
super.postValue(value)
}
override fun setValue(value: Profile?) {
value?.let { insertProfile(it) }
super.setValue(value)
}
override fun getValue(): Profile? {
return profileDao.loadProfileLiveData().getValue()
}
}
}
Run Code Online (Sandbox Code Playgroud)
这样,我从DB获取更新并可以保存 …
android kotlin android-room android-livedata android-architecture-components
我正在开发一个白色品牌的应用程序。
我们创建每个客户不同的风味并且每个客户都Debug和ProductionAPI的,所以我试图建立起来的摇篮。
我该怎么做?
这是我尝试过的:
buildTypes {
debug {
// some configurations
}
release {
// some configurations
}
}
flavorDimensions "client"
productFlavors {
company1{
dimension "client"
buildConfigField("String", "BASE_URL", "\"https://app.company1/devApi/\"")
}
company2 {
dimension "client"
buildConfigField("String", "BASE_URL", "\"https://app.company2/devApi/\"")
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:我希望能够为BASE_URL每个 Flavor 和 Buildtype定义一个不同的。
风味公司1,BuildType调试
https://app.company1.com/devApi/
Run Code Online (Sandbox Code Playgroud)
Flavor company1, BuildType 发布
https://app.company1.com/prodApi/
Run Code Online (Sandbox Code Playgroud)
风味公司2,BuildType调试
https://dev.company2.com/api/
Run Code Online (Sandbox Code Playgroud)
Flavor company2, BuildType 发布
https://prod.company2.com/api/
Run Code Online (Sandbox Code Playgroud) android android-gradle-plugin android-productflavors android-build-type
为了在我的应用程序中重用一些片段,我需要知道哪个片段是后端堆栈中的第二个片段.
为了做到这一点我正在使用getFragmentManager().getFragments(),它显示以下错误(但工作!)
FragmentManager.getFragments只能在同一个库组中调用(groupId = com.android.support)
使用安全吗?我可以通过另一种方法获得相同的结果吗?
这是代码:
public Fragment getCallerFragment(){
List<Fragment> fragments = getFragmentManager().getFragments();
return fragments.get(fragments.size()-2);
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试从协程触发LiveData更新:
object AddressList: MutableLiveData<List<Address>>()
fun getAddressesLiveData(): LiveData<List<Address>> {
AddressList.value = listOf()
GlobalScope.launch {
AddressList.value = getAddressList()
}
return AddressList
}
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误:
IllegalStateException:无法在后台线程上调用setValue
有没有办法使它与协程一起工作?
我正在使用 minSdk 23 的项目上测试Java 8+ API 脱糖支持。
java.time例如,当我使用该库时val instant = Instant.now(),我收到以下错误:
调用需要 API 级别 26(当前最低为 23):java.time.Instant#now
该代码可以正确编译并执行。
我可以假设它适用于所有设备吗?
编辑:我尝试过 API 29(物理设备)和 API 23(模拟器)。
我正在尝试使用带有协程的期货创建一个Observable。
这是我尝试过的:
private fun getHelloObservable(): Observable<String>{
val deferred = GlobalScope.async {
"Hello"
}
return Observable.just(deferred.await())
}
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:
只能从协程或其他暂停函数调用暂停函数“ await”。
有没有办法做到这一点?
android ×6
kotlin ×5
android-architecture-components ×2
android-room ×1
java ×1
observable ×1
rest ×1
retrofit2 ×1
rx-java ×1