标签: kotlin

多变量让在Kotlin中

有没有办法在kotlin中为多个可变变量链接多个let?

fun example(first: String?, second: String?) {
    first?.let {
        second?.let {
            // Do something just if both are != null
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我的意思是,像这样:

fun example(first: String?, second: String?) {
    first?.let && second?.let { 
        // Do something just if both are != null
    }
}
Run Code Online (Sandbox Code Playgroud)

kotlin

96
推荐指数
7
解决办法
2万
查看次数

如何在Android Studio 3.0.0中使用数据绑定和Kotlin

我刚开始使用Android Studio 3.0.0,但每次尝试构建我的项目时都会收到此错误:

Error:Circular dependency between the following tasks:
:app:compileDebugKotlin
+--- :app:dataBindingExportBuildInfoDebug
|    \--- :app:compileDebugKotlin (*)
\--- :app:kaptDebugKotlin
     \--- :app:dataBindingExportBuildInfoDebug (*)
(*) - details omitted (listed previously)
Run Code Online (Sandbox Code Playgroud)

我在用

kapt "com.android.databinding:compiler:2.2.0"
Run Code Online (Sandbox Code Playgroud)

在我使用之前

androidProcessor "com.android.databinding:compiler:2.2.0"
Run Code Online (Sandbox Code Playgroud)

它工作得很好......我做错了什么?

谢谢!

android kotlin android-studio android-databinding android-studio-3.0

96
推荐指数
3
解决办法
2万
查看次数

Android Room - 简单选择查询 - 无法访问主线程上的数据库

我正在尝试使用Room Persistence Library的示例.我创建了一个实体:

@Entity
public class Agent {
    @PrimaryKey
    public String guid;
    public String name;
    public String email;
    public String password;
    public String phone;
    public String licence;
}
Run Code Online (Sandbox Code Playgroud)

创建了一个DAO类:

@Dao
public interface AgentDao {
    @Query("SELECT COUNT(*) FROM Agent where email = :email OR phone = :phone OR licence = :licence")
    int agentsCount(String email, String phone, String licence);

    @Insert
    void insertAgent(Agent agent);
}
Run Code Online (Sandbox Code Playgroud)

创建了Database类:

@Database(entities = {Agent.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase {
    public abstract …
Run Code Online (Sandbox Code Playgroud)

crash android kotlin android-studio-3.0 android-room

94
推荐指数
15
解决办法
8万
查看次数

"没有足够的信息来推断参数T"与Kotlin和Android

我正在尝试使用Kotlin在我的Android应用程序中复制以下ListView:https://github.com/bidrohi/KotlinListView.

不幸的是我收到了一个错误,我无法解决自己.这是我的代码:

MainActivity.kt:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val listView = findViewById(R.id.list) as ListView
    listView.adapter = ListExampleAdapter(this)
}

private class ListExampleAdapter(context: Context) : BaseAdapter() {
    internal var sList = arrayOf("Eins", "Zwei", "Drei")
    private  val mInflator: LayoutInflater

    init {
        this.mInflator = LayoutInflater.from(context)
    }

    override fun getCount(): Int {
        return sList.size
    }

    override fun getItem(position: Int): Any {
        return sList[position]
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getView(position: Int, convertView: View?, …
Run Code Online (Sandbox Code Playgroud)

android kotlin android-8.0-oreo

94
推荐指数
3
解决办法
3万
查看次数

Flutter 和 Android:您的构建当前配置为使用 Java 17.0.2 和 Gradle 7.0.2

我在 Flutter 的 Android 部分得到了这个。

Unsupported Java. 
Your build is currently configured to use Java 17.0.2 and Gradle 7.0.2.

Possible solution:
 - Open Gradle wrapper settings, change `distributionUrl` property to use compatible Gradle version and reload the project
Run Code Online (Sandbox Code Playgroud)

请告知解决方案。我真的很感激你的回答。

java android gradle kotlin flutter

94
推荐指数
5
解决办法
15万
查看次数

如何在Kotlin中创建一个抽象类的匿名类实例?

假设这KeyAdapter是一个抽象类,有几个可以覆盖的方法.

在java中我可以做到:

KeyListener keyListener = new KeyAdapter() {
    @Override public void keyPressed(KeyEvent keyEvent) {
        // ...
    }
};
Run Code Online (Sandbox Code Playgroud)

如何在Kotlin做同样的事情?

java abstract-class kotlin

93
推荐指数
2
解决办法
3万
查看次数

将Kotlin Array转换为Java varargs

如何将我的Kotlin转换Array为varargs Java String[]

val angularRoutings = 
    arrayOf<String>("/language", "/home")

// this doesn't work        
web.ignoring().antMatchers(angularRoutings)
Run Code Online (Sandbox Code Playgroud)

如何将ArrayList传递给varargs方法参数?

java interop kotlin kotlin-interop

93
推荐指数
1
解决办法
3万
查看次数

Kotlin二级构造函数

如何在Kotlin中声明辅助构造函数?

有没有关于这方面的文件?

以下不编译......

class C(a : Int) {
  // Secondary constructor
  this(s : String) : this(s.length) { ... }
}
Run Code Online (Sandbox Code Playgroud)

syntax constructor kotlin

91
推荐指数
7
解决办法
5万
查看次数

RequiresApi vs TargetApi android注释

什么区别RequiresApiTargetApi

kotlin中的样本:

@RequiresApi(api = Build.VERSION_CODES.M)
@TargetApi(Build.VERSION_CODES.M)
class FingerprintHandlerM() : FingerprintManager.AuthenticationCallback()
Run Code Online (Sandbox Code Playgroud)

注意:FingerprintManager.AuthenticationCallback需要apiM

注2:如果我不使用TargetApi lint失败并出错 class requires api level 23...

android kotlin android-annotations android-support-library

88
推荐指数
5
解决办法
3万
查看次数

如何在Kotlin中使用TypeToken +泛型与Gson

我无法从自定义类(Turns)获取泛型类型列表:

val turnsType = TypeToken<List<Turns>>() {}.type
val turns = Gson().fromJson(pref.turns, turnsType)
Run Code Online (Sandbox Code Playgroud)

它说:

cannot access '<init>' it is 'public /*package*/' in 'TypeToken'
Run Code Online (Sandbox Code Playgroud)

generics gson kotlin typetoken

87
推荐指数
7
解决办法
3万
查看次数