Reputation: 79
When I create a new fragment in Android Studio by clicking:
and then choosing Kotlin as language for the new fragment, I get following warning that I have to implement two members or make the class abstract. When I make it abstract the program doesn't start, because it can't instanciate the abstract class. When I implement the methods I don't know what I have to put into them and the program doesn't start with the error that they don't override anything:
When I create the Fragment in Java it works, I don't understand it. Here is how I call the fragment from the activity:
class MovieListActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_movie_list)
supportFragmentManager.beginTransaction()
.replace(R.id.main_container, MovieListFragment())
.commit()
}
}
I'm using Java and Kotlin files in this project, so maybe that's causing a conflict? Until now it worked using Kotlin and Java in this project, for example with Kotlin and Java Activities. I just had this problem now when I created the first Kotlin fragment. This is my build.gradle file:
plugins {
id 'com.android.application'
id 'kotlin-android'
}
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "...myProjectName"
minSdkVersion 23
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
// Support for Java 8 features
coreLibraryDesugaringEnabled true
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
//kotlin & compose
kotlinOptions {
useIR = true
jvmTarget = JavaVersion.VERSION_1_8.toString()
}
composeOptions {
def kotlin_compiler_version = "1.4.0"
kotlinCompilerVersion kotlin_compiler_version
def kotlin_compiler_extension_version = "1.0.0-alpha05"
kotlinCompilerExtensionVersion kotlin_compiler_extension_version
}
buildFeatures {
compose true
viewBinding true
}
}
dependencies {
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.annotation:annotation:1.1.0'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.2.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'
def retrofitVersion = "2.9.0"
def lifecycle_version = "1.1.1"
def cardview_version = "1.0.0"
def recyclerView_version = "1.0.0"
def glideVersion = "4.11.0"
def supportVersion = "28.0.0"
def aws_version = "2.13.+"
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
//retrofit
implementation "com.squareup.retrofit2:retrofit:$retrofitVersion"
// Retrofit gson converter
implementation "com.squareup.retrofit2:converter-gson:$retrofitVersion"
// ViewModel and LiveData
implementation "android.arch.lifecycle:extensions:$lifecycle_version"
// CardViews
implementation "androidx.cardview:cardview:$cardview_version"
// RecyclerView
implementation "androidx.recyclerview:recyclerview:$recyclerView_version"
//Design library
implementation "com.android.support:design:$supportVersion"
// Glide
implementation 'com.github.bumptech.glide:glide:4.8.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
// Circular ImageView
implementation 'de.hdodenhof:circleimageview:3.1.0'
// Google Tabs
implementation "androidx.browser:browser:1.2.0"
// SwipeCards
implementation 'com.lorentzos.swipecards:library:1.0.9'
// Amplify core dependency (for AWS Cognito)
implementation 'com.amplifyframework:core:1.6.4'
implementation 'com.amplifyframework:aws-auth-cognito:1.6.4'
// AWS Dependencies
implementation "com.amazonaws:aws-android-sdk-core:$aws_version"
implementation "com.amazonaws:aws-android-sdk-auth-core:$aws_version"
implementation("com.amazonaws:aws-android-sdk-auth-userpools:$aws_version@aar") { transitive = true }
implementation("com.amazonaws:aws-android-sdk-auth-ui:$aws_version") { transitive = true }
// Support for Java 8 features
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.0.10'
implementation 'com.pddstudio:encrypted-preferences:1.3.0'
// Required -- JUnit 4 framework
testImplementation 'junit:junit:4.12'
// Optional -- Robolectric environment
testImplementation 'androidx.test:core:1.0.0'
// Optional -- Mockito framework
testImplementation 'org.mockito:mockito-core:1.10.19'
def fragment_version = "1.2.5"
// Java language implementation
implementation "androidx.fragment:fragment:$fragment_version"
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation fileTree(include: ['*.jar'], dir: 'app/libs')
//HTTP Requests https://developer.android.com/training/volley
implementation 'com.android.volley:volley:1.1.1'
//compose
def compose = "1.0.0-alpha08"
implementation "androidx.compose.ui:ui:$compose"
implementation "androidx.compose.foundation:foundation:$compose"
implementation "androidx.compose.runtime:runtime-livedata:$compose"
implementation "androidx.compose.runtime:runtime-rxjava2:$compose"
implementation "androidx.compose.material:material:$compose"
implementation "androidx.compose.material:material-icons-core:$compose"
implementation "androidx.compose.material:material-icons-extended:$compose"
}
Upvotes: 0
Views: 667
Reputation: 1030
I had same issue, that's only with Android Studio Canary, after doing couple experiments i ended up solving this issue by using fragment-ktx library:
implementation "androidx.fragment:fragment-ktx:1.3.0-beta01"
Upvotes: 1