R.drawable中的+ imageResource导致Jetpack Compose中的IllegalArgumentException

我正在用名为Homeactivity的新的空JC活动测试Jetpack Compose。我正在尝试将可绘制资源加载到我的代码中。这是我的功能:

@Composable
fun home() {
    MaterialTheme(
        colors = colors,typography = typography
    ) {
        val menuIcon = +imageResource(R.drawable.ic_baseline_menu_24)
        val bottomactions = listOf<Image>()
        var (drawerState,onDrawerStatechangelistener) = +state { DrawerState.Closed }

        BottomDrawerLayout(drawerState = drawerState,onStateChange = onDrawerStatechangelistener,drawerContent = {
            Text(text = "Drawer")
        }) {
            Column(
                crossAxisSize = LayoutSize.Expand
            ) {
                BottomAppBar(
                    navigationIcon = {
                        drawerState = DrawerState.Opened
                        AppBarIcon(icon = menuIcon,onClick = {})
                    },actionData = bottomactions,action = {
                        AppBarIcon(icon = it,onClick = {})
                    }
                )
            }
        }
    }
}

根据JC网站,+imageResource(R.drawable.xyz)应该将资源作为andX.ui Image对象加载 imageResource函数返回Effect<Image>,它不会使应用程序崩溃,但我不知道如何将其解析为AppBarIcon所需的Image对象。

一些Logcat输出:


java.lang.RuntimeException: Unable to start activity ComponentInfo{com.craftmyspace/com.craftmyspace.activities.Homeactivity}: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsnotNull,parameter bitmap
        at android.app.activityThread.performLaunchactivity(activityThread.java:2913)
        at android.app.activityThread.handleLaunchactivity(activityThread.java:3048)
        ...os.ZygoteInit.main(ZygoteInit.java:858)


     Caused by: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsnotNull,parameter bitmap
        at androidx.ui.graphics.AndroidImage.<init>(Unknown Source:2)
        at androidx.ui.graphics.AndroidImageKt.imageFromResource(AndroidImage.kt:29)
        at androidx.ui.res.ImageResourcesKt$imageResource$1$1.invoke(ImageResources.kt:45)
        at androidx.ui.res.ImageResourcesKt$imageResource$1$1.invoke(Unknown Source:0)
        at androidx.compose.ComposerKt.remember(Composer.kt:1867)
        at androidx.compose.EffectsKt$memo$2.invoke(Effects.kt:287)
        at androidx.compose.EffectsKt$memo$2.invoke(Unknown Source:2)
        at androidx.compose.Effect.resolve(Effects.kt:106)
        at androidx.compose.Effect.resolve$default(Effects.kt:843)
        at androidx.compose.Effect.unaryPlus(Effects.kt:115)
        at androidx.ui.res.ImageResourcesKt$imageResource$1.invoke(ImageResources.kt:45)
        at androidx.ui.res.ImageResourcesKt$imageResource$1.invoke(Unknown Source:2)
        at androidx.compose.Effect.resolve(Effects.kt:106)
        at androidx.compose.Effect.resolve$default(Effects.kt:843)
        at androidx.compose.EffectsKt.unaryPlus(Effects.kt:841)
        at com.craftmyspace.activities.HomeactivityKt$home$1$1$1.invoke(Homeactivity.kt:66)

Homeactivity.kt:66是这个 val menuIcon = +imageResource(R.drawable.ic_baseline_menu_24)

cwal10 回答:R.drawable中的+ imageResource导致Jetpack Compose中的IllegalArgumentException

可以变得更容易

   Image(
    vectorResource(R.drawable.folk),contentScale = ContentScale.Crop
)
,

就我而言,我解决了从public static void load_Directory_Files(File directory) { //Get all file in storage File[] fileList = directory.listFiles(); //check storage is empty or not if(fileList != null && fileList.length > 0) { for (int i=0; i<fileList.length; i++) { boolean restricted_directory = false; //check file is directory or other file if(fileList[i].isDirectory()) { for (String path : Constant.removePath){ if (path.equals(fileList[i].getPath())) { restricted_directory = true; break; } } if (!restricted_directory) load_Directory_Files(fileList[i]); } else { String name = fileList[i].getName().toLowerCase(); for (String ext : Constant.videoExtensions){ //Check the type of file if(name.endsWith(ext)) { //first getVideoDuration String videoDuration = Methods.getVideoDuration(fileList[i]); long playbackPosition; long percentage = C.TIME_UNSET; FilesInfo.fileState state; /*First check video already played or not. If not then state is NEW * else load playback position and calculate percentage of it and assign it*/ //check it if already exist or not if yes then start from there else start from start position int existIndex = -1; for (int j = 0; j < Constant.filesPlaybackHistory.size(); j++) { String fListName = fileList[i].getName(); String fPlaybackHisName = Constant.filesPlaybackHistory.get(j).getFileName(); if (fListName.equals(fPlaybackHisName)) { existIndex = j; break; } } try { if (existIndex != -1) { //if true that means file is not new state = FilesInfo.fileState.NOT_NEW; //set playbackPercentage not playbackPosition MediaMetadataRetriever retriever = new MediaMetadataRetriever(); retriever.setDataSource(fileList[i].getPath()); String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION); retriever.release(); int duration = Integer.parseInt(time); playbackPosition = Constant.filesPlaybackHistory.get(existIndex).getPlaybackPosition(); if (duration > 0) percentage = 1000L * playbackPosition / duration; else percentage = C.TIME_UNSET; } else state = FilesInfo.fileState.NEW; //playbackPosition have value in percentage Constant.allMemoryVideoList.add(new FilesInfo(fileList[i],directory,videoDuration,state,percentage,storageType)); //directory portion currentDirectory = directory.getPath(); unique_directory = true; for(int j=0; j<directoryList.size(); j++) { if((directoryList.get(j).toString()).equals(currentDirectory)){ unique_directory = false; } } if(unique_directory){ directoryList.add(directory); } //When we found extension from videoExtension array we will break it. break; }catch (Exception e){ e.printStackTrace(); Constant.allMemoryVideoList.add(new FilesInfo(fileList[i],FilesInfo.fileState.NOT_NEW,C.TIME_UNSET,storageType)); } } } } } } Constant.directoryList = directoryList; } ic_chat_bubble 资源中删除ic_chat_bubble.xml(anydpi)的问题,

enter image description here

,

我遇到了同样的问题,并发现imageFromResource()imageResource()无法加载矢量资源。 您需要通过提供资源ID来创建effectOf<VectorAsset>,并使用DrawVector()绘制矢量资产。 您可以使用下面的@Composable函数来绘制矢量资源。

@Composable
fun VectorImage(@DrawableRes id: Int,tint: Color = Color.Transparent) {
    val vector = +vectorResource(id)
    WithDensity {
        Container(
            width = vector.defaultWidth.toDp(),height = vector.defaultHeight.toDp()
        ) {
            DrawVector(vectorImage = vector,tintColor = tint)
        }
    }
}
本文链接:https://www.f2er.com/3138834.html

大家都在问