在Java中,我们可以执行以下操作
public class TempClass {
List<Integer> myList = null;
void doSomething() {
myList = new ArrayList<>();
myList.add(10);
myList.remove(10);
}
}
Run Code Online (Sandbox Code Playgroud)
但如果我们直接将其重写为Kotlin,如下所示
class TempClass {
var myList: List<Int>? = null
fun doSomething() {
myList = ArrayList<Int>()
myList!!.add(10)
myList!!.remove(10)
}
}
Run Code Online (Sandbox Code Playgroud)
我从我的列表中得到了没有找到add并remove运行的错误
我努力将它转换为ArrayList,但这需要投射它是奇怪的,而在Java中不需要.这就违背了抽象类List的目的
class TempClass {
var myList: List<Int>? = null
fun doSomething() {
myList = ArrayList<Int>()
(myList!! as ArrayList).add(10)
(myList!! as ArrayList).remove(10)
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法让我使用List但不需要投射它,就像在Java中可以做到的那样?
我在其Manifest中使用了具有以下内容的库.
<application android:allowBackup="true"
android:label="@string/app_name"
android:supportsRtl="true"/>
Run Code Online (Sandbox Code Playgroud)
但是,作为我用来包含库的应用程序的反向设置
<application android:allowBackup="false"
android:label="@string/app_name"
android:supportsRtl="false"/>
Run Code Online (Sandbox Code Playgroud)
因此它会出现合并错误,如库`Manifest中的`android:supportsRtl ="true"`必不可少?它有时会导致错误
要解决它,我们只需要将以下内容添加到我们的Manifest应用程序中.
tools:replace="android:supportsRtl"
Run Code Online (Sandbox Code Playgroud)
和
tools:replace="android:allowBackup"
Run Code Online (Sandbox Code Playgroud)
但是,添加两个tools:replace将在编译时出错.我怎么能把两者结合起来tools:replace?
我尝试了以下,它不起作用.
tools:replace="android:supportsRtl|android:allowBackup"
Run Code Online (Sandbox Code Playgroud) 当我编译时,我得到了上述错误.我的gradle文件如下: -
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 23
buildToolsVersion "24.0.0 rc2"
defaultConfig {
applicationId "package.name"
minSdkVersion 16
targetSdkVersion 23
versionCode 6
versionName "2.0"
}
buildTypes {
debug {
minifyEnabled false
signingConfig signingConfigs.debug
}
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
}
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile "com.android.support:appcompat-v7:$support_version"
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile "org.jetbrains.anko:anko-sdk15:$anko_version"
compile "org.jetbrains.anko:anko-support-v4:$anko_version"
compile "com.android.support:recyclerview-v7:$support_version"
}
buildscript { …Run Code Online (Sandbox Code Playgroud) 当我运行时pod init,出现以下错误。
RuntimeError - [Xcodeproj] Unknown object version.
/Library/Ruby/Gems/2.6.0/gems/xcodeproj-1.19.0/lib/xcodeproj/project.rb:227:in `initialize_from_file'
/Library/Ruby/Gems/2.6.0/gems/xcodeproj-1.19.0/lib/xcodeproj/project.rb:112:in `open'
/Library/Ruby/Gems/2.6.0/gems/cocoapods-1.10.1/lib/cocoapods/command/init.rb:41:in `validate!'
/Library/Ruby/Gems/2.6.0/gems/claide-1.0.3/lib/claide/command.rb:333:in `run'
/Library/Ruby/Gems/2.6.0/gems/cocoapods-1.10.1/lib/cocoapods/command.rb:52:in `run'
/Library/Ruby/Gems/2.6.0/gems/cocoapods-1.10.1/bin/pod:55:in `<top (required)>'
/usr/local/bin/pod:23:in `load'
/usr/local/bin/pod:23:in `<main>'
Run Code Online (Sandbox Code Playgroud) 从Android Studio 3.0(Canary 5)迁移到Android Studio 3.0(Beta 1)后,移动到最新的gradle,即 'com.android.tools.build:gradle:3.0.0-beta1'
当我尝试gradle sync时,它会在下面显示错误.
Failed to resolve: com.android.support:multidex:1.0.2
Failed to resolve: com.android.support:multidex-instrumentation:1.0.2
Run Code Online (Sandbox Code Playgroud)
我检查Android Studio 3.0 Canary 9 - 无法解析包,它不能解决我的问题,因为我已经有了这个
maven {
url 'https://maven.google.com'
}
Run Code Online (Sandbox Code Playgroud)
我很惊讶它甚至要求multidex 1.0.2,因为我只在build.gradle中
compile 'com.android.support:multidex:1.0.1'
Run Code Online (Sandbox Code Playgroud)
我检查使用./gradlew app:dependencies | grep multidex,它显示如下的故障(各种口味等)
+--- com.android.support:multidex-instrumentation:1.0.2 FAILED
+--- com.android.support:multidex:1.0.1
+--- com.android.support:multidex:1.0.2 FAILED
+--- com.android.support:multidex:1.0.1 -> 1.0.2 FAILED
Run Code Online (Sandbox Code Playgroud)
哪儿来的依赖multidex:1.0.2,并multidex-instrumentation:1.0.2从何而来?我怎么能解决这个问题?
android gradle build.gradle android-gradle-plugin android-multidex
我将我的一个Java类转换为Kotlin和类,如下所示.
class MainApplication : Application() {
companion object {
operator fun get(context: Context): MainApplication {
return context.applicationContext as MainApplication
}
}
}
Run Code Online (Sandbox Code Playgroud)
它具有静态功能get.
我仍然有一个访问它的Java函数.
MainApplication application = MainApplication.get(mContext);
Run Code Online (Sandbox Code Playgroud)
当MainApplication在Java中时很好.但是当MainApplication在Kotlin中时,上面的代码错误
Error:(27, 54) error: cannot find symbol method get(Context)
Run Code Online (Sandbox Code Playgroud)
我怎样才能访问get上面的Java代码?
我指的是https://developer.android.com/jetpack/compose/state 中的示例。当我编码时
var expanded by remember { mutableStateOf(false) }
Run Code Online (Sandbox Code Playgroud)
它错误说明
Type 'TypeVariable(T)' has no method 'getValue(Nothing?, KProperty<*>)' and thus it cannot serve as a delegate
Run Code Online (Sandbox Code Playgroud)
下面虽然有效
val expanded = remember { mutableStateOf(false) }
// OR
val (expanded, setExpanded) = remember { mutableStateOf(false) }
Run Code Online (Sandbox Code Playgroud) 在 Dagger Hilt View Model 1.0.0-alpha01 中
implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha01"
implementation 'com.google.dagger:hilt-android:2.28-alpha'
kapt 'androidx.hilt:hilt-compiler:1.0.0-alpha01'
kapt 'com.google.dagger:hilt-android-compiler:2.28-alpha'
Run Code Online (Sandbox Code Playgroud)
我可以使用下面的
class MyViewModel @ViewModelInject constructor(
private val repository: Repository,
@Assisted private val savedStateHandle: SavedStateHandle
) : ViewModel(), LifecycleObserver {
// Some codes...
}
Run Code Online (Sandbox Code Playgroud)
但是,当我迁移到 Dagger Hilt View Model 1.0.0-alpha03 时
implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03"
implementation 'com.google.dagger:hilt-android:2.31.2-alpha'
kapt 'androidx.hilt:hilt-compiler:1.0.0-alpha03'
kapt 'com.google.dagger:hilt-android-compiler:2.31.2-alpha'
Run Code Online (Sandbox Code Playgroud)
我收到了警告
'Assisted' is deprecated. Deprecated in Java
'ViewModelInject' is deprecated. Deprecated in Java
'ViewModelInject' is deprecated. Deprecated in Java
'Assisted' is deprecated. Deprecated in Java
Run Code Online (Sandbox Code Playgroud)
有什么新的工作方式?
以https://kotlinlang.org/docs/reference/coroutines/flow.html#flows-are-cold的直接例子为例
fun simple(): Flow<Int> = flow {
println("Flow started")
for (i in 1..3) {
delay(100)
emit(i)
}
}
fun main() = runBlocking<Unit> {
println("Calling simple function...")
val flow = simple()
println("Calling collect...")
flow.collect { value -> println(value) }
println("Calling collect again...")
flow.collect { value -> println(value) }
}
Run Code Online (Sandbox Code Playgroud)
我得到了错误collect。
This is an internal kotlinx.coroutines API that should not be used from outside of kotlinx.coroutines. No compatibility guarantees are provided.It is recommended to report your use-case of …Run Code Online (Sandbox Code Playgroud) 我正在使用ConstraintLayout,我将在下面显示
我想隐藏First(使用已消失),以及我期望的视图如下所示(ElasticBody将延伸以使用原始First视图空间).
但是,当我实际设置First为时gone,我的视图结果如下(所有图像都来自Android Studio Design视图).我Elastic Body也失踪了,高度奇怪地扩大了.
我的布局代码如下
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<TextView
android:id="@+id/txt_first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#0ff"
android:text="First"
android:visibility="gone"
android:textSize="26sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/txt_body"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/txt_body"
android:layout_width="0dp"
android:background="#f0f"
android:layout_height="wrap_content"
android:text="Elastic Body"
android:textSize="26sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/txt_tail"
app:layout_constraintStart_toEndOf="@+id/txt_first"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/txt_tail"
android:background="#ff0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tail"
android:textSize="26sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/txt_body"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
(注意,如果你删除它gone,你将获得第一个图像视图).为什么会这样?我怎么能解决它在我First离开的地方,我可以Elastic Body伸展正确吗?
p/s:我知道如何在LinearLayout和RelativeLayout中做到这一点......但是想知道这是否是对ConstraintLayout的限制?
android ×5
kotlin ×4
build.gradle ×1
cocoapods ×1
collections ×1
dagger-hilt ×1
gradle ×1
ios ×1
java ×1
kotlin-flow ×1
rubygems ×1
xcode ×1