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

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

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

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

Kotlin

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

Q&A

解決済

1回答

663閲覧

androidstudioでNullPointerExceptionというRuntimeExceptionが投げられました

tera877

総合スコア9

Android Studio

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

Kotlin

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

0グッド

0クリップ

投稿2023/05/13 08:44

編集2023/05/14 10:27

実現したいこと

ここに実現したいことを箇条書きで書いてください。

  • ▲▲機能を動作するようにする

前提

androidが提供しているコードラボのナビゲーションコンポーネントの範囲を勉強していたら上記に書いたようなエラーが出ました。(エラーコードは下記)

実際に行ったことは、メインアクティビティに2つのフラグメントを設定、ナビゲーションようのファイルの作成といったことです。その際にgradleファイルに少し書き足しましたが、チュートリアルの指示通りです。

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

FATAL EXCEPTION: main Process: com.example.wordsapp, PID: 17424 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.wordsapp/com.example.wordsapp.MainActivity}: java.lang.NullPointerException: null cannot be cast to non-null type androidx.navigation.fragment.NavHostFragment at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3635) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3792) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2210) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loopOnce(Looper.java:201) at android.os.Looper.loop(Looper.java:288) at android.app.ActivityThread.main(ActivityThread.java:7839) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003) Caused by: java.lang.NullPointerException: null cannot be cast to non-null type androidx.navigation.fragment.NavHostFragment

該当のソースコード

kotlin

1/* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16package com.example.wordsapp 17 18import android.os.Bundle 19import android.view.Menu 20import android.view.MenuItem 21import androidx.appcompat.app.AppCompatActivity 22import androidx.core.content.ContextCompat 23import androidx.navigation.NavController 24import androidx.navigation.fragment.NavHostFragment 25import androidx.navigation.ui.setupActionBarWithNavController 26import androidx.recyclerview.widget.GridLayoutManager 27import androidx.recyclerview.widget.LinearLayoutManager 28import androidx.recyclerview.widget.RecyclerView 29import com.example.wordsapp.databinding.ActivityMainBinding 30 31/** 32 * Main Activity and entry point for the app. Displays a RecyclerView of letters. 33 */ 34class MainActivity : AppCompatActivity() { 35 36 private lateinit var navController: NavController 37 38 override fun onCreate(savedInstanceState: Bundle?) { 39 super.onCreate(savedInstanceState) 40 41 val navHostFragment = supportFragmentManager 42 .findFragmentById(R.id.nav_host_fragment) as NavHostFragment 43 navController = navHostFragment.navController 44 45 setupActionBarWithNavController(navController) 46 } 47 48 override fun onSupportNavigateUp(): Boolean { 49 return navController.navigateUp() || super.onSupportNavigateUp() 50 } 51} 52

試したこと

ナビゲーションファイルにはしっかりとnav_host_fragmentが設定されており、defaultNavHostもtrueになっています。MainActivityの記述も間違っていないと思われます。コンパイルエラーは出ておらず、logcatでログを見た際のエラーです。アプリが強制終了されます・

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

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

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

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

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

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

hoshi-takanori

2023/05/13 08:55

androidが提供しているコードラボの URL は?
tera877
hoshi-takanori

2023/05/13 11:06

nav_version を更新すれば、あとはちゃんと手順通りにやれば動くはずです。 activity_main.xml の内容はどうなってますか?
tera877

2023/05/13 11:08

<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/nav_host_fragment" android:name="androidx.navigation.fragment.NavHostFragment" android:layout_width="match_parent" android:layout_height="match_parent" app:defaultNavHost="true" app:navGraph="@navigation/nav_graph"/> 以上がactivity_main.xmlです。
tera877

2023/05/13 13:03

エラーの原因はわからないですが、出る場所だけわかりました。下記のコメントアウトをしている2つの行を追加すると必ずエラーになります。 dependencies { implementation "androidx.core:core-ktx:$core_ktx_version" implementation "androidx.appcompat:appcompat:$appcompat_version" implementation "com.google.android.material:material:$material_version" implementation "androidx.constraintlayout:constraintlayout:$constraintlayout_version" //implementation "androidx.navigation:navigation-fragment-ktx:$nav_version" //implementation "androidx.navigation:navigation-ui-ktx:$nav_version" }
guest

回答1

0

ベストアンサー

activity_main.xml の変更ですが、まず「FrameLayout の内容(androidx.recyclerview.widget.RecyclerView)」というのは次の赤い部分です。

diff

1 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 tools:context=".MainActivity"> 6 7- <androidx.recyclerview.widget.RecyclerView 8- android:id="@+id/recycler_view" 9- android:layout_width="match_parent" 10- android:layout_height="match_parent" 11- android:clipToPadding="false" 12- android:padding="16dp" /> 13 </FrameLayout>

これを次の緑の部分で置き換えます。

diff

1 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 tools:context=".MainActivity"> 6 7+ <androidx.fragment.app.FragmentContainerView 8+ android:id="@+id/nav_host_fragment" 9+ android:layout_width="match_parent" 10+ android:layout_height="match_parent" /> 11 </FrameLayout>

さらに何行か追加して、最終的には次のようになるはず。

xml

1<?xml version="1.0" encoding="utf-8"?> 2<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 xmlns:app="http://schemas.android.com/apk/res-auto" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context=".MainActivity"> 8 9 <androidx.fragment.app.FragmentContainerView 10 android:id="@+id/nav_host_fragment" 11 android:name="androidx.navigation.fragment.NavHostFragment" 12 android:layout_width="match_parent" 13 android:layout_height="match_parent" 14 app:defaultNavHost="true" 15 app:navGraph="@navigation/nav_graph" /> 16</FrameLayout>

投稿2023/05/13 18:03

hoshi-takanori

総合スコア7893

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

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

tera877

2023/05/14 01:27

おっしゃる通りでした。解決までずっと付き合っていただき本当にありがとうございました。申し訳ないです。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問