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

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

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

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

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

Kotlin

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

Q&A

解決済

1回答

2097閲覧

【kotlin】【Android Studio】LinearLayoutをfindViewByIdしてもnullが返ってきてしまう

kotatsu828

総合スコア6

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

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

Kotlin

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

0グッド

0クリップ

投稿2020/05/25 17:25

#前提・実現したいこと
kotlinでandroidアプリの開発を行っています。
その中であるLinearLayoutだけfindViewByIdするとnullが返ってくるせいで、
LinearLayoutが含まれるアクティビティを起動するとアプリがクラッシュしてしまう問題が発生しています。
#発生している問題・エラーメッセージ

java.lang.IllegalStateException: findViewById(R.id.legend_linear) must not be null

ほかのViewもfindViewByIdしていますが、なぜかこのViewだけNULLになります。
#該当のソースコード

OriginMyPageFragment.kt

kotlin

1class OriginMyPageFragment : Fragment() , OriginMyPageContract.View { 2 3 override lateinit var presenter: OriginMyPageContract.Presenter 4 5(省略) 6 7 private lateinit var legendLayout: LinearLayout 8 9(省略) 10 11 override fun onResume() { 12 super.onResume() 13 presenter.start() 14 } 15 16 17 override fun onCreateView( 18 inflater: LayoutInflater, 19 container: ViewGroup?, 20 savedInstanceState: Bundle? 21 ): View? 22 { 23(省略) 24 // Inflate the layout for this fragment 25 val root = inflater.inflate(R.layout.fragment_origin_my_page, container, false) 26 27 with(root){ 28 29 (省略) 30 31 legendLayout = findViewById(R.id.legend_linear) 32 33 (省略) 34 35 return root 36 } 37 38 39 40 override fun onViewCreated(view: View, savedInstanceState: Bundle?) { 41 super.onViewCreated(view, savedInstanceState) 42 //planRecyclerViewにアダプターをセット 43 planRecyclerView.adapter = planAdapter 44 45 //ここからカレンダーの設定 46 val daysOfWeek = daysOfWeekFromLocale() 47 legendLayout.children.forEachIndexed{ index, view -> 48 (view as TextView).apply { 49 text = daysOfWeek[index].getDisplayName(TextStyle.SHORT, Locale.ENGLISH).toUpperCase( 50 Locale.ENGLISH) 51 setTextColorRes(R.color.example_1_white_light) 52 } 53 } 54 55省略 56 57 58 } 59}

fragment_origin_my_page.xml

xml

1<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:app="http://schemas.android.com/apk/res-auto" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:id="@+id/whole_frame_layout" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context=".originMyPage.OriginMyPageFragment"> 8 9 省略 10 11 <LinearLayout 12 android:layout_width="match_parent" 13 android:layout_height="wrap_content" 14 android:orientation="horizontal"> 15 16 <FrameLayout 17 android:id="@+id/originmypage_calendar_frame" 18 android:layout_width="match_parent" 19 android:layout_height="match_parent"> 20 21 <ScrollView 22 android:layout_width="match_parent" 23 android:layout_height="match_parent"> 24 25 <androidx.constraintlayout.widget.ConstraintLayout 26 android:id="@+id/mypage_constraintlayout" 27 android:layout_width="match_parent" 28 android:layout_height="wrap_content" 29 android:clickable="false"> 30 31 32 省略 33 34 <include 35 layout="@layout/calendar_day_legend" 36 android:id="@+id/include" 37 android:layout_width="match_parent" 38 android:layout_height="wrap_content" 39 android:layout_marginTop="8dp" 40 app:layout_constraintEnd_toEndOf="parent" 41 app:layout_constraintStart_toStartOf="parent" 42 app:layout_constraintTop_toBottomOf="@+id/helpbutton" /> 43 44          省略 45

calendar_day_legend.xml

xml

1<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:id="@+id/legend_linear" 4 android:layout_width="match_parent" 5 android:layout_height="wrap_content" 6 android:gravity="center" 7 android:orientation="horizontal"> 8 tools:ignore="HardcodedText" 9 10 11 <TextView 12 android:id="@+id/legendText1" 13 android:layout_width="0dp" 14 android:layout_height="wrap_content" 15 android:layout_weight="1" 16 android:gravity="center" 17 android:text="SUN" 18 android:textColor="@color/black" 19 android:textSize="14sp" /> 20 21 省略 22 23</LinearLayout>

#自分で調べたことや試したこと
kotlin extentionsでfindViewByIdを(fragment内には)記述しない→結果は変わらず

#使っているツールのバージョンなど補足情報
Android Gradle Plugin Version 3.6.3
Gradle Version 5.6.4

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

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

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

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

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

kotatsu828

2020/05/27 00:18

おっしゃる通りincludeのidを消したら問題なくできました。 ありがとうございます。
guest

回答1

0

自己解決

includeタグのidを消したら解決しました

投稿2020/05/27 00:19

kotatsu828

総合スコア6

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問