実現したいこと
ここに実現したいことを箇条書きで書いてください。
前提
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/ツールのバージョンなど)
ここにより詳細な情報を記載してください。

0 コメント