我正在尝试将 Kotlin 与 Android Studio 3.0 与我现有的项目一起使用。我使用 kotlin 创建了一个片段。我试图在我的 Java 活动中使用 kotlin 片段。但是每次我尝试运行它时,我都会得到
Error:(209, 5) error: cannot find symbol class BlankFragment
空白片段.kt
import android.content.Context
import android.net.Uri
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
class BlankFragment : Fragment() {
// TODO: Rename and change types of parameters
private var mParam1: String? = null
private var mParam2: String? = null
private var mListener: OnFragmentInteractionListener? = null
public override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1)
mParam2 = getArguments().getString(ARG_PARAM2)
}
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup, savedInstanceState: Bundle?): View
= FragmentUi<Fragment>().createView(AnkoContext.create(ctx, this))
}
class FragmentUi<T>: AnkoComponent<T> {
override fun createView(ui: AnkoContext<T>) = with(ui) {
verticalLayout {
textView {
text = R.string.app_name
}
}
}
}
项目 - build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.1.1'
ext.anko_version = '0.10.1'
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0-alpha5'
classpath 'com.google.gms:google-services:3.0.0'
classpath "io.realm:realm-gradle-plugin:3.0.0"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
应用程序构建.gradle
apply plugin: 'com.android.application'
apply plugin: 'realm-android'
android {
signingConfigs {
splits {
abi {
enable true //enables the ABIs split mechanism
reset() //reset the list of ABIs to be included to an empty string
include 'x86', 'armeabi', 'armeabi-v7a', 'mips', 'x86_64', 'arm64-v8a'
universalApk true
}
}
project.ext.versionCodes = ['armeabi': 1, 'armeabi-v7a': 2, 'arm64-v8a': 3, 'mips': 5, 'mips64': 6, 'x86': 8, 'x86_64': 9]
android.applicationVariants.all { variant ->
variant.outputs.each { output ->
output.versionCodeOverride =
project.ext.versionCodes.get(output.getFilter(
com.android.build.OutputFile.ABI), 0) * 10000000 + android.defaultConfig.versionCode
}
}
}
compileSdkVersion 25
buildToolsVersion '26'
defaultConfig {
applicationId "com.example"
minSdkVersion 15
targetSdkVersion 23
versionCode 20
versionName "v.32"
testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.releaseConfig
}
}
packagingOptions {
exclude 'LICENSE.txt'
}
repositories {
maven { url "https://jitpack.io" }
}
lintOptions {
disable "ResourceType"
}
dexOptions {
javaMaxHeapSize "4g"
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.google.android.gms:play-services:10.2.6'
compile 'com.android.support:design:25.3.1'
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:cardview-v7:25.3.1'
compile 'com.google.code.gson:gson:2.6.2'
// compile 'io.realm:realm-android:0.87.5'
compile 'com.android.support:recyclerview-v7:25.3.1'
compile 'com.github.jkwiecien:EasyImage:1.3.1'
compile 'com.mcxiaoke.volley:library:1.0.19'
compile 'com.yarolegovich:lovely-dialog:1.0.4'
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
compile 'com.github.ParkSangGwon:TedPermission:v1.0.12'
compile 'com.github.lovetuzitong:MultiImageSelector:1.2'
compile 'com.github.piotrek1543:CustomSpinner:0.1'
compile 'com.github.ganfra:material-spinner:1.1.1'
compile 'com.getbase:floatingactionbutton:1.10.1'
compile 'com.appyvet:materialrangebar:1.3'
compile 'com.afollestad.material-dialogs:core:0.9.4.5'
compile 'com.mixpanel.android:mixpanel-android:4.+'
compile 'com.github.ericwlange:AndroidJSCore:3.0.1'
compile 'com.android.support:multidex:1.0.0'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
}
compile "org.jetbrains.anko:anko-sdk25:$anko_version"
compile "org.jetbrains.anko:anko-sdk25-coroutines:$anko_version"
compile "org.jetbrains.anko:anko-appcompat-v7:$anko_version"
compile files('libs/bcpkix-jdk15on-1.56.0.0.jar')
compile files('libs/core-1.56.0.0.jar')
compile files('libs/prov-1.56.0.0.jar')
}
apply plugin: 'com.google.gms.google-services'