Tag: kotlin android extensions

Kotlin参数类型不匹配

我试图将下面的Java代码转换为Kotlin。 它编译和工作正常。 public abstract class MvpViewHolder<P extends BasePresenter> extends RecyclerView.ViewHolder { protected P presenter; public MvpViewHolder(View itemView) { super(itemView); } public void bindPresenter(P presenter) { this.presenter = presenter; presenter.bindView(this); } public void unbindPresenter() { presenter = null; } } 在我目前的代码中,我得到了一个presenter.bindView(this)的错误状态Required: Nothing, Found: MvpViewHolder 。 abstract class MvpViewHolder<P>(itemView: View) : RecyclerView.ViewHolder(itemView) where P : BasePresenter<*,*> { […]

在Android的Kotlin的帮助下开始一个项目

我是Android的Kotlin新手,我刚刚开始学习。请让我知道我应该如何知道Kotlin在Kotlin的帮助下在Android中创建项目。

包含Android扩展的Kotlin类不是建立在随后的构建上的

我最近决定将我们公司的android应用程序移植到kotlin。 它运作良好,但是,当我使用任何kotlin扩展,我得到一个奇怪的行为。 我注意到,任何使用扩展的类(不论是anko,还是自定义的扩展)都只是第一次构建而成,并且在其后的每个构建中,它都不在其他类中。 不使用扩展的Kotlin类始终是正确构建的。 每当我运行该程序(在后续的构建中,第一个运行没有问题),就会导致NoClassDefFoundError。 我已经尝试了一切,我开始有点绝望…这是我的整个项目的build.gradle: buildscript { ext.kotlin_version = '1.0.1-2' ext.support_version = '23.3.0' ext.anko_version = '0.8.3' repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.1.0' classpath 'com.google.gms:google-services:2.0.0-alpha6' classpath 'org.codehaus.groovy:gradle-groovy-android-plugin:0.3.7' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { } 这里是我尝试使用kotlin的模块的build.gradle […]

如何使用kotlin使用片段

我无法找到如何使用kotlin使用片段。 我在onCreateView方法中遇到错误,请帮助我。 ListaFragment.kt: class ListaFragment : Fragment() { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle): View? { val view = inflater.inflate(R.layout.fragment_lista, container, false) return view } } fragment_lista.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAlignment="center" android:text="Hello"/> </LinearLayout> MainActivity.kt: class MainActivity : AppCompatActivity() { private var listaFragment: ListaFragment? = […]

如何在Kotlin中使用AndroidAnnotation @SharedPref

我正在尝试在kotlin中使用AndroidAnnotations @SharefPref,但Iget下面的错误 org.androidannotations.annotations.sharedpreferences.Pref can only be used on an element that extends org.androidannotations.api.sharedpreferences.SharedPreferencesHelper 我究竟做错了什么? //Interface @SharedPref(SharedPref.Scope.APPLICATION_DEFAULT) open interface MyPreferences { @DefaultInt(-1) fun someIntValue():Int } //Fragment @Pref lateinit open var sharedPref:CongressPreferences_ //usage within fragment val get: Int = sharedPref.selectedEventId().get()

Kotlin对象加载延迟

我有一个奇怪的问题,我有一个Android的初始登录活动,而它的onCreateMethod,被触发,我被Object类调用,这是一个名为BaseData的静态对象。 我得到一个奇怪的空错误,而它的一个对象。 发现对象在Kotlin中被延迟加载需要一段时间,我如何强制它首先加载,并在我的主登陆登录活动创建时可用。 class LoginActivity : BaseActivity() { override fun onCreate(savedInstanceState: Bundle?) { if(BaseData.userId == “”) { //At this step BaseData itself comes as null, so app goes blank //Do something } } } object BaseData : Observable() { var initialSyncDone:Boolean = false var isOffline:Boolean = true var userId: String = “” init{ // I […]

使用Kotlin在android中动态创建控件

使用java,动态地创建一个控件,我们使用像TextView textview=new TextView(getApplicationContext()); 在Kotlin中如何做到这一点呢? var textview = TextView不起作用,也不像var textview as TextView那样使用var textview as TextView 不幸的是,甚至没有遇到任何好的android的kotlin教程。 更新 – 其实我试图创建一个自定义布局的动态列表视图。

在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扩展以编程方式夸大布局

我有以下布局: <?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 […]

未解决的参考:kotlinx – Kotlin 1-0-0-RC-1036

我正在一个Android应用程序工作,前几天我更新了我的Kotlin版本从1.0.0-beta-1103到候选版本1.0.0-rc-1036 ,现在我无法运行我的应用程序Kotlin Android扩展。 我使用apply plugin: 'kotlin-android-extensions'就像官方博客所说: http apply plugin: 'kotlin-android-extensions'我的应用程序不能识别任何布局的任何视图。 这是在控制台上显示的内容: 错误:(20,8)未解决的参考:kotlinx 错误:执行任务':app:compileDebugKotlin'失败。 编译错误。 查看日志了解更多详情 请帮助我,我正在失去理智! 更新: 这是我在全球build.gradle buildscript { ext { kotlin_version = "1.0.0-rc-1036" } repositories { jcenter() maven { url 'https://maven.fabric.io/public' } } dependencies { classpath 'com.android.tools.build:gradle:1.5.0' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // The Fabric Gradle plugin uses an open ended version to react // quickly […]