Tag: android

Kotlin Gson自定义解串器列表

如何在Gson注册自定义的Json解串器? 当我在java代码注册反序列化所有工作正常,但是当我把Kotlin转换为Java – 反序列化方法不叫。 Kotlin注册码: val listType: Type = object : TypeToken<List<Advert>>() {}.type val gson = GsonBuilder() .registerTypeAdapter(listType, deserializer) val retrofit = Retrofit.Builder() .addConverterFactory(GsonConverterFactory.create(gson)) Java代码: Type listType = new TypeToken<List<Advert>>() {}.getType(); Gson gson = new GsonBuilder() .registerTypeAdapter(listType, deserializer) Retrofit retrofit = new Retrofit.Builder() .addConverterFactory(GsonConverterFactory.create(gson)) 反序列化器声明: class AdvertsDeserializer : JsonDeserializer<List<Advert>> { override fun deserialize(json: JsonElement?, typeOfT: […]

错误:包含非法的最终字段-Kotlin

我正在使用带有Realm的Kotlin数据类,Gson注释用于从服务器获取数据。 问题:当我在android studio中运行项目时,会出现以下错误 Error:Class "VenderConfig" contains illegal final field "name". 我正在学习Kotlin,所以对此没有太多的想法。 我的VenderConfig类是: @RealmClass class VenderConfig( @SerializedName("name") val name: String? = null, @SerializedName("website") val wb_url: String? = null, @SerializedName("icon") val icon: String? = null, @SerializedName("logo") val logo: String? = null, @SerializedName("description") val description: String? = null, @PrimaryKey @SerializedName("id") val id: Int? = null ) : RealmObject() […]

Android的测试在领域的kotlin

如何才能做一个简单的测试在Android的领域数据库在Kotlin实施测试? 我试图从github上的java realm test中将一个片段调整为kotlin,并得到下一个代码: import io.realm.Realm import io.realm.log.RealmLog import org.hamcrest.CoreMatchers import org.junit.Assert import org.junit.Test import org.junit.Before import org.junit.Rule import org.mockito.Mockito.`when` import org.powermock.api.mockito.PowerMockito import org.powermock.modules.junit4.rule.PowerMockRule class DBTest { @Rule var rule = PowerMockRule() lateinit internal var mockRealm: Realm @Before fun setup() { PowerMockito.mockStatic(RealmLog::class.java) PowerMockito.mockStatic(Realm::class.java) val mockRealm = PowerMockito.mock(Realm::class.java) `when`(Realm.getDefaultInstance()).thenReturn(mockRealm) this.mockRealm = mockRealm } @Test fun shouldBeAbleToGetDefaultInstance() […]

在AndroidStudio2.3.3中需要帮助来设置Kotlin

在我的Android Studio 2.3.3中,我安装了Kotlin插件,然后创建了一个kotlin类的活动。 当我完成并运行该活动时,我得到了ClassNotFoundException。 我确认我已经在清单文件中添加了新的Activity。 我第一次尝试与Android的Kotlin,并不确定是否需要更改Gradle文件或不使用Kotlin。 以下是我的错误日志: FATAL EXCEPTION: main Process: com.example.chandan.myapplication, PID: 16035 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.chandan.myapplication/com.example.chandan.myapplication.RegistrationActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.chandan.myapplication.RegistrationActivity" on path: DexPathList[[zip file "/data/app/com.example.chandan.myapplication-xkv2k_8ODBWxxa3Gsn-HDw==/base.apk", zip file "/data/app/com.example.chandan.myapplication-xkv2k_8ODBWxxa3Gsn-HDw==/split_lib_dependencies_apk.apk", zip file "/data/app/com.example.chandan.myapplication-xkv2k_8ODBWxxa3Gsn-HDw==/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.chandan.myapplication-xkv2k_8ODBWxxa3Gsn-HDw==/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.chandan.myapplication-xkv2k_8ODBWxxa3Gsn-HDw==/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.chandan.myapplication-xkv2k_8ODBWxxa3Gsn-HDw==/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.chandan.myapplication-xkv2k_8ODBWxxa3Gsn-HDw==/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.chandan.myapplication-xkv2k_8ODBWxxa3Gsn-HDw==/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.chandan.myapplication-xkv2k_8ODBWxxa3Gsn-HDw==/split_lib_slice_6_apk.apk", zip […]

用Kotlin在Android中创建Parcelable数据类有没有一种方便的方法?

我目前在我的Java项目中使用了优秀的AutoParcel ,这有助于创建Parcelable类。 现在,我为下一个项目考虑的Kotlin具有这种数据类的概念,它自动生成equals,hashCode和toString方法。 有没有一种方便的方法来创建一个Kotlin数据类Parcelable方便的方式(没有手动实现的方法)?

Kotlin – 如何在ViewPager中添加OnPageChangeListener

我是新的kotlin,我怎样才能在ViewPager中添加OnPageChangeListener 。 viewPager?.addOnPageChangeListener // Now how can I add listener to it.

使用Kotlin Android扩展以编程方式夸大布局

我有以下布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@android:color/white" android:paddingLeft="20dp" android:paddingRight="20dp"> <TextView android:id="@+id/tvErrorTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:textColor="@android:color/background_dark" android:textSize="18sp" /> <TextView android:id="@+id/tvErrorDesc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:textColor="@android:color/darker_gray" android:textSize="16sp" /> <TextView android:id="@+id/tvAction" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:layout_marginBottom="10dp" android:layout_gravity="end" android:padding="5dp" android:textSize="15sp" android:textStyle="bold" android:textAllCaps="true" android:textColor="@android:color/holo_purple" /> </LinearLayout> 当我想在下面的活动之外使用kotlin android扩展时 ,它不起作用。 我结束了做findViewById。 … … import kotlinx.android.synthetic.main.dialog_error.* … … val view […]

与Realm一起使用“kotlin-kapt”时,构建失败

环境:Android Studio 2.3.3,Gradle 2.3.3和Kotlin 1.1.4-3 当我使用Kotlin创建一个新的Android项目时,一切正常,首先,添加 //in project's build.gradle classpath "io.realm:realm-gradle-plugin:3.5.0" //in app's build.gradle apply plugin: 'realm-android' 好的。 但是在我补充之后 apply plugin: 'kotlin-kapt' 总是提高执行失败 e: Wrong plugin option format: null, should be plugin:<pluginId>:<optionName>=<value> FAILED FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':app:kaptDebugKotlin'. 堆栈跟踪 : * Exception is: org.gradle.api.tasks.TaskExecutionException: Execution […]

为什么Kotlin不能推断下面的lambda参数(在Java – > Kotlin转换之后)?

我有以下的Java代码: public class DatabaseManager { public interface Predicate<T> { boolean test(T t); } private interface Consumer<T> { void apply(T t); } private void updateLiveLists() { updateLiveLists(liveList -> true); } private void updateLiveLists(Predicate<MutableLiveList<?>> predicate) { forEachLiveList(liveList -> { if (predicate.test(liveList)) { refresh(liveList); } }); } private void forEachLiveList(Consumer<MutableLiveList<?>> consumer) { … } 然后我在Android Studio中使用Java -> Kotlin […]

Kotlin通用属性问题

在将我的Android项目从Java转换为Kotlin时,我遇到了Kotlin的一些问题。 假设我有接口I和扩展接口I的接口O. interface I{ } interface O: I{ } 通用类A具有扩展接口I的通用参数V,扩展类A的通用类B: abstract class A<V: I> { } class B : A<O>() { } 当我试图创建这样的属性: val returnB: A<I> get() = b 我得到编译器错误“需要A,找到B”。 在Java中,这将工作没有任何问题。 我怎样才能使用Kotlin访问? 我需要在我的应用程序中使用这种方法的基本类。 具有Navigator类的泛型参数的BaseViewModel: abstract class BaseViewModel<N>(application: Application, val repositoryProvider: RepositoryProvider) : AndroidViewModel(application) { var navigator: N? = null fun onDestroyView() { navigator = null […]