解決したいこと
todoアプリを作っています。
デザインを始めたのですがエラーが出てしまいました。
発生している問題・エラー
Unresolved reference: fab
fabというのはidです。
それとfabボタンに警告マークがありました。
Missing contentDescription attribute on image Non-textual widgets like ImageViews and ImageButtons should use the contentDescription attribute to specify a textual description of the widget such that screen readers and other accessibility tools can adequately describe the user interface. Note that elements in application screens that are purely decorative and do not provide any content or enable a user action should not have accessibility content descriptions. In this case, just suppress the lint warning with a tools:ignore="ContentDescription" attribute. Note that for text fields, you should not set both the hint and the contentDescription attributes since the hint will never be shown. Just set the hint. See https://developer.android.com/guide/topics/ui/accessibility/apps#special-cases
該当するソースコード
MainFragment.kt
1package com.example.todo 2 3import android.os.Bundle 4import android.view.View 5import androidx.fragment.app.Fragment 6import androidx.fragment.app.FragmentContainer 7import androidx.fragment.app.viewModels 8import androidx.navigation.Navigation.findNavController 9import androidx.navigation.fragment.findNavController 10import com.example.todo.databinding.MainFragmentBinding 11import dagger.hilt.android.AndroidEntryPoint 12 13 14@AndroidEntryPoint 15class MainFragment: Fragment(R.layout.activity_main) { 16 private val vm: MainViewMotel by viewModels() 17 18 // ここから下を追加 19 private var _binding: MainFragmentBinding? = null 20 private val binding: MainFragmentBinding get() = _binding!! 21 22 override fun onViewCreated(view: View, savedInstanceState: Bundle?) { 23 super.onViewCreated(view, savedInstanceState) 24 this._binding = MainFragmentBinding.bind(view) 25 26 binding.fab.setOnClickListener { 27 findNavController().navigate(R.id.action_mainFragment2_to_createtodoFragment3) 28 } 29 30 } 31 32 override fun onDestroyView() { 33 super.onDestroyView() 34 this._binding = null 35 } 36}
activity_main.xml
1<?xml version="1.0" encoding="utf-8"?> 2<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context=".MainActivity"> 8 9 <com.google.android.material.floatingactionbutton.FloatingActionButton 10 android:id="@+id/fab" 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content" 13 android:layout_marginStart="342dp" 14 android:layout_marginLeft="342dp" 15 android:layout_marginTop="658dp" 16 android:layout_marginEnd="13dp" 17 android:layout_marginRight="13dp" 18 android:layout_marginBottom="13dp" 19 android:clickable="true" 20 app:layout_constraintBottom_toBottomOf="parent" 21 app:layout_constraintEnd_toEndOf="parent" 22 app:layout_constraintStart_toStartOf="parent" 23 app:layout_constraintTop_toTopOf="parent" 24 app:srcCompat="@drawable/ic_baseline_add_24" /> 25</androidx.constraintlayout.widget.ConstraintLayout>
build.gradle
1plugins { 2 id 'com.android.application' 3 id 'kotlin-android' 4 id 'kotlin-kapt' 5 id 'dagger.hilt.android.plugin' 6 7} 8 9 10 11android { 12 compileSdkVersion 30 13 buildToolsVersion "30.0.3" 14 15 defaultConfig { 16 applicationId "com.example.todo" 17 minSdkVersion 16 18 targetSdkVersion 30 19 versionCode 1 20 versionName "1.0" 21 22 testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 23 24 vectorDrawables.useSupportLibrary = true 25 } 26 27 buildTypes { 28 release { 29 minifyEnabled false 30 proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 31 } 32 } 33 compileOptions { 34 sourceCompatibility JavaVersion.VERSION_1_8 35 targetCompatibility JavaVersion.VERSION_1_8 36 } 37 kotlinOptions { 38 jvmTarget = '1.8' 39 } 40 41 buildFeatures { 42 viewBinding true 43 } 44 45} 46 47dependencies { 48 49 50 implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" 51 implementation 'androidx.core:core-ktx:1.5.0' 52 implementation 'androidx.appcompat:appcompat:1.3.0' 53 implementation 'com.google.android.material:material:1.3.0' 54 implementation 'androidx.constraintlayout:constraintlayout:2.0.4' 55 implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5' 56 implementation 'androidx.navigation:navigation-ui-ktx:2.3.5' 57 testImplementation 'junit:junit:4.+' 58 androidTestImplementation 'androidx.test.ext:junit:1.1.2' 59 androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' 60 61 62 //Navigation 63 implementation("androidx.navigation:navigation-fragment-ktx:2.3.5") 64 implementation("androidx.navigation:navigation-ui-ktx:2.3.5") 65 66 //Room 67 def room_version = "2.3.0" 68 implementation "androidx.room:room-runtime:$room_version" 69 annotationProcessor "androidx.room:room-compiler:$room_version" 70 kapt "androidx.room:room-compiler:$room_version" 71 implementation "androidx.room:room-runtime:$room_version" 72 implementation "androidx.room:room-ktx:$room_version" 73 74 //Hilt 75 implementation "com.google.dagger:hilt-android:2.28-alpha" 76 kapt "com.google.dagger:hilt-android-compiler:2.28-alpha" 77 78 //activity 79 implementation "androidx.activity:activity-ktx:1.1.0" 80 81} 82
自分で試したこと
このサイト(https://stackoverflow.com/questions/41407811/android-vectordrawables-usesupportlibrary-true-is-stopping-app) を見てbuild.gradleファイルに追加
vectorDrawables.useSupportLibrary = true
あなたの回答
tips
プレビュー