I'm new to developing Android apps with Kotlin and was following along with a google course. I need to use the Icon()
function, but every time, I am met with the error Cannot access '<init>': it is package-private in 'Icon
. Attempting to import the packages, including the following:
import androidx.compose.material.icons.Icons
import androidx.compose.material3.Icon
does not seem to work. These are the packages recommended by Android Studio to fix the issue, and I have not found any others that work. This is how the IDE looks:
I have tried to search for this issue online, but have had no luck. Similarly, I have had no luck using snippets relating to icons that I found on the Android Developers documentation.
Here is the MainActivity.kt for addition context:
package com.example.buisness_card
import android.graphics.drawable.Icon
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.icons.Icons
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.FixedScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.sp
import com.example.buisness_card.ui.theme.Buisness_CardTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Buisness_CardTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Introduction(name = "Name Goes Here", jobDescription = "Job Description Goes Here")
}
}
}
}
}
@Composable // Defines the function as a composable function
fun Introduction(
// Values to be passed into the function fo
name: String,
jobDescription: String,
modifier: Modifier = Modifier
) {
// Create a constant with a path to the profile picture
val image = painterResource(id = R.drawable.pfp3)
// Create a column as a container for other objects
Column(
// Centre all objects
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center,
// Unsure why this works, but ensures objects behave as desired
modifier = modifier.fillMaxSize()
) {
Image(
// Reference previously defined path to image
painter = image,
// Description for users with TalkBack enabled
contentDescription = stringResource(R.string.pfp_description),
// Sets scale of the image using a float value
contentScale = FixedScale(0.4F),
// Pass down the modifier for future child objects
modifier = modifier
)
// Creates a new text object
Text(
// Sets value to the parameter of the function
text = name,
fontSize = 40.sp,
// Ensures proper alignment
modifier = Modifier.align(alignment = Alignment.CenterHorizontally)
)
Text(
text = jobDescription,
fontSize = 20.sp,
modifier = Modifier.align(alignment = Alignment.CenterHorizontally)
)
}
}
@Composable
fun Contact_Info(
phoneNumber: String,
github: String,
website: String,
modifier: Modifier = Modifier
){
Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center,
modifier = modifier.fillMaxSize(),
) {
Row(modifier = modifier) {
//Icon will go here
Icon(
)
}
}
}
@Composable
fun TestIcon(modifier: Modifier = Modifier) {
}
@Preview(showBackground = true, showSystemUi = true)
@Composable
fun GreetingPreview() {
Introduction(name = "Name Goes Here", jobDescription = "Job Description Goes Here")
}
I would appreciate any advice or help with this issue
I have attempted to call the Icon()
Composable function so that I can use it within my app, but attempting to use the function results in errors appearing in Android Studio, that also cause the compiler to fail.
I have tried importing the packages using Android Studio, but to no avail, resulting in the error: Cannot access '<init>': it is package-private in 'Icon
. Importing packages from snippets on the Android Developers documentation results in errors stating that the package does not exist/the name is not defined.
You have two imports for Icon
:
import android.graphics.drawable.Icon
import androidx.compose.material3.Icon
You need the second one. You probably added the first one by accident; remove it and all will be fine.