質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.49%
Realm

RealmとはSQLiteやCore Dataに代わるモバイルデータベースです。iOSとAndroidの両方でサポートされています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

Q&A

解決済

1回答

5411閲覧

findViewByIdのエラーが解消できない

asdfhorse

総合スコア17

Realm

RealmとはSQLiteやCore Dataに代わるモバイルデータベースです。iOSとAndroidの両方でサポートされています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

0グッド

0クリップ

投稿2018/11/15 02:57

編集2018/11/16 00:41

前提・実現したいこと

AndroidStudio Kotlin初心者です。
家計簿アプリを作成しています。
家計簿の支出一覧画面をListViewを使って表示させようと考えています。
どなたかご教授お願い致します。

発生している問題・エラーメッセージ

エラーメッセージ MainActivityのlistView = findViewById(R.id.kakeibolist)で下記のエラーが出ています。 Cannot infer type parameter T in fun <T : View!> findViewById ( id: Int ) : T! None of the following substitutions ( Int ) ( Int ) can be applied to ( Int )

該当のソースコード

Kotlin

1package com.example.kakeiboapplication 2 3import io.realm.RealmObject 4import io.realm.annotations.PrimaryKey 5 6open class Kakeibo : RealmObject() { 7 @PrimaryKey 8 var shushiId: String? = null 9 var date: String? = null 10 var money: Int = 0 11 var shuushi: String? = null 12 var genre: String? = null 13}

kotlin

1package com.example.kakeiboapplication 2 3import android.view.LayoutInflater 4import android.view.View 5import android.view.ViewGroup 6import android.widget.TextView 7import io.realm.OrderedRealmCollection 8import io.realm.RealmBaseAdapter 9 10 class KakeiboAdapter (data: OrderedRealmCollection<Kakeibo>?) 11 : RealmBaseAdapter<Kakeibo>(data) { 12 13 14 inner class ViewHolder(cell : View) { 15 var kakeiboDate = cell.findViewById<TextView>(R.id.kakeibodate) 16 var kakeiboMoney = cell.findViewById<TextView>(R.id.kakeibomoney) 17 var kakeiboShushi = cell.findViewById<TextView>(R.id.kakeiboshuushi) 18 var kakeiboGenre = cell.findViewById<TextView>(R.id.kakeibogenre) 19 // var recipeImage = cell.findViewById<ImageView>(R.id.re_image) 20 } 21 22 23 override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View { 24 val view : View 25 val viewHolder : ViewHolder 26 27 when(convertView) { 28 null -> { 29 val inflater = LayoutInflater.from(parent?.context) 30 view = inflater.inflate(R.layout.kakeibolist,parent,false) 31 viewHolder = ViewHolder(view) 32 view.tag = viewHolder 33 } 34 else -> { 35 view = convertView 36 viewHolder = view.tag as ViewHolder 37 } 38 } 39 adapterData?.run { 40 val Kakeibo = get(position) 41 viewHolder.kakeiboDate.text = Kakeibo.date 42 viewHolder.kakeiboMoney.setText(Kakeibo.money) 43 viewHolder.kakeiboShushi.text = Kakeibo.shuushi 44 viewHolder.kakeiboGenre.text = Kakeibo.genre 45 // viewHolder.recipeImage.setImageBitmap(Recipe.recipeImage) 46 viewHolder.kakeiboDate.setPadding(15, 0, 0, 0) 47 viewHolder.kakeiboMoney.setPadding(200, 0 , 0 , 0 ) 48 viewHolder.kakeiboShushi.setPadding(15, 0, 0, 0) 49 viewHolder.kakeiboGenre.setPadding(200, 0 , 0 , 0 ) 50 51 } 52 return view 53 } 54 }

Kotlin

1package com.example.kakeiboapplication 2 3import android.graphics.BitmapFactory 4import android.os.Bundle 5import android.support.v7.app.AppCompatActivity 6import android.util.Log 7import android.widget.ListView 8import io.realm.Realm 9import io.realm.kotlin.createObject 10import io.realm.kotlin.where 11import java.util.* 12 13class MainActivity : AppCompatActivity() { 14 private lateinit var realm: Realm 15 @Override 16 override fun onCreate(savedInstanceState: Bundle?) { 17 super.onCreate(savedInstanceState) 18 setContentView(R.layout.activity_main) 19 20 realm = Realm.getDefaultInstance() 21 22 realm.beginTransaction() 23 24 realm.createObject<Kakeibo>(UUID.randomUUID().toString()).apply { 25 date = "2018年11月14日" 26 money = 1000 27 shuushi = "支出" 28 genre = "食費" 29 } 30 realm.commitTransaction() 31 32 var listView:ListView 33 super.onCreate(savedInstanceState) 34 setContentView(R.layout.activity_main) 35 listView = findViewById(R.id.kakeibolist) 36 val Kakeibo = realm.where<Kakeibo>().findAll() 37 listView?.adapter = KakeiboAdapter(Kakeibo) 38 39 } 40}

xml

1<?xml version="1.0" encoding="utf-8"?> 2<android.support.constraint.ConstraintLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:app="http://schemas.android.com/apk/res-auto" 5 xmlns:tools="http://schemas.android.com/tools" 6 android:id="@+id/container" 7 android:layout_width="match_parent" 8 android:layout_height="match_parent" 9 tools:context=".MainActivity"> 10 11 <TextView 12 android:id="@+id/text1" 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:layout_marginLeft="124dp" 16 android:text="@string/title_nengetu" 17 android:textSize="25dp" 18 app:layout_constraintLeft_toLeftOf="parent" 19 app:layout_constraintTop_toTopOf="parent" 20 android:layout_marginTop="16dp"/> 21 <TextView 22 android:id="@+id/text2" 23 android:layout_width="wrap_content" 24 android:layout_height="wrap_content" 25 android:text="@string/calendar" 26 android:textSize="20dp" 27 app:layout_constraintLeft_toLeftOf="parent" 28 app:layout_constraintTop_toTopOf="parent" 29 android:layout_marginTop="20dp" android:layout_marginLeft="268dp"/> 30 <View 31 android:layout_width="match_parent" 32 android:layout_height="2.5dp" 33 android:background="@color/blackColor" 34 android:layout_marginLeft="10dp" 35 android:layout_marginRight="10dp" 36 app:layout_constraintRight_toRightOf="parent" 37 app:layout_constraintLeft_toLeftOf="parent" 38 app:layout_constraintBottom_toBottomOf="parent" 39 app:layout_constraintTop_toTopOf="parent" 40 app:layout_constraintHorizontal_bias="1.0" 41 app:layout_constraintVertical_bias="0.092"/> 42 <View 43 android:layout_width="match_parent" 44 android:layout_height="1dp" 45 android:background="@color/blackColor" 46 app:layout_constraintRight_toRightOf="parent" 47 app:layout_constraintLeft_toLeftOf="parent" 48 app:layout_constraintBottom_toBottomOf="parent" 49 app:layout_constraintTop_toTopOf="parent" 50 app:layout_constraintHorizontal_bias="0.0" 51 app:layout_constraintVertical_bias="0.89" android:id="@+id/view"/> 52 53 <android.support.design.widget.BottomNavigationView 54 android:id="@+id/navigation" 55 android:layout_width="0dp" 56 android:layout_height="wrap_content" 57 android:layout_marginEnd="0dp" 58 android:layout_marginStart="0dp" 59 android:background="?android:attr/windowBackground" 60 app:layout_constraintBottom_toBottomOf="parent" 61 app:layout_constraintLeft_toLeftOf="parent" 62 app:layout_constraintRight_toRightOf="parent" 63 app:menu="@menu/navigation"/> 64 <ListView 65 android:id="@+id/kakeibolist" 66 android:layout_width="382dp" 67 android:layout_height="405dp" 68 app:layout_constraintLeft_toLeftOf="parent" 69 app:layout_constraintRight_toRightOf="parent" 70 app:layout_constraintHorizontal_bias="0.0" app:layout_constraintBottom_toTopOf="@+id/view"/> 71 72</android.support.constraint.ConstraintLayout>

xml

1<?xml version="1.0" encoding="utf-8"?> 2<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 xmlns:app="http://schemas.android.com/apk/res-auto"> 6<TextView 7 android:id="@+id/kakeibodate" 8 android:layout_width="wrap_content" 9 android:layout_height="match_parent" 10 app:layout_constraintStart_toStartOf="parent" 11 android:textAppearance="?android:attr/textAppearanceLarge"/> 12 <TextView 13 android:id="@+id/kakeiboshuushi" 14 android:layout_width="wrap_content" 15 android:layout_height="match_parent" 16 app:layout_constraintStart_toStartOf="parent" 17 android:textAppearance="?android:attr/textAppearanceLarge"/> 18 <TextView 19 android:id="@+id/kakeibomoney" 20 android:layout_width="wrap_content" 21 android:layout_height="match_parent" 22 app:layout_constraintStart_toStartOf="parent" 23 android:textAppearance="?android:attr/textAppearanceLarge"/> 24 <TextView 25 android:id="@+id/kakeibogenre" 26 android:layout_width="wrap_content" 27 android:layout_height="match_parent" 28 app:layout_constraintStart_toStartOf="parent" 29 android:textAppearance="?android:attr/textAppearanceLarge"/> 30 31</android.support.constraint.ConstraintLayout>

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

super.onCreateの位置を変更したら、解決しました。

投稿2018/11/15 06:09

asdfhorse

総合スコア17

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.49%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問