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

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

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

Q&A

解決済

1回答

1319閲覧

Android Fragmentが表示されない

ooyama_burger

総合スコア5

0グッド

0クリップ

投稿2023/02/13 07:37

編集2023/02/13 07:41

実現したいこと

Fragmentを表示したい。

前提

こちらのサイトで質問するのは初めてなので、情報が不足していたら申し訳ありません。適宜必要な情報を追加していきます。
「technical master はじめてのAndroidアプリ開発Java編」を教材に、Android Studioで開発を1ヶ月ほどしております。
より理解を深めるために、スマートフォンに純正で入っているアプリを真似たアプリを開発しております。今回は連絡先アプリです。
C言語、C++は5年ほど、Javaは3ヶ月程度経験しております。
こちらのサイトを参考にさせていただきました。

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

MainActivityに表示したいFragmentが表示されない。

該当のソースコード

MainActivity.java

Java

1package com.example.fragmenteasy; 2 3import androidx.appcompat.app.AppCompatActivity; 4import android.os.Bundle; 5 6public class MainActivity extends AppCompatActivity 7{ 8 @Override 9 protected void onCreate( Bundle savedInstanceState ) 10 { 11 super.onCreate( savedInstanceState ); 12 setContentView( R.layout.activity_main ); 13 } 14}

SampleFragment.java

Java

1package com.example.fragmenteasy; 2 3import android.os.Bundle; 4import android.view.LayoutInflater; 5import android.view.View; 6import android.view.ViewGroup; 7 8import androidx.annotation.NonNull; 9import androidx.annotation.Nullable; 10import androidx.fragment.app.Fragment; 11 12public class SampleFragment extends Fragment 13{ 14 @Nullable 15 @Override 16 public View onCreateView( @NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState ) 17 { 18 return super.onCreateView( inflater, container, savedInstanceState ); 19 } 20} 21

activity_main.xml

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 android:background="#009688" 8 tools:context=".MainActivity"> 9 10 <androidx.fragment.app.FragmentContainerView 11 android:id="@+id/fragment" 12 android:name="com.example.fragmenteasy.SampleFragment" 13 android:layout_width="0dp" 14 android:layout_height="wrap_content" 15 android:background="#673AB7" 16 app:layout_constraintBottom_toBottomOf="parent" 17 app:layout_constraintEnd_toEndOf="parent" 18 app:layout_constraintStart_toStartOf="parent" 19 app:layout_constraintTop_toTopOf="parent" 20 tools:layout="@layout/fragment_main" /> 21</androidx.constraintlayout.widget.ConstraintLayout>

fragment_main.xml

xml

1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:gravity="center" 7 android:orientation="vertical" > 8 9 <TextView 10 android:id="@+id/textView" 11 android:layout_width="match_parent" 12 android:layout_height="300dp" 13 android:background="#8D8BC34A" 14 android:gravity="center" 15 android:text="Hello Fragment World!" 16 android:textColor="#212121" 17 tools:ignore="HardcodedText" /> 18</LinearLayout>

string.xml

xml

1<resources> 2 <string name="app_name">FragmentEasy</string> 3</resources>

AndroidManifest.xml

xml

1<?xml version="1.0" encoding="utf-8"?> 2<manifest xmlns:android="http://schemas.android.com/apk/res/android"> 3 4 <application 5 android:allowBackup="true" 6 android:icon="@mipmap/ic_launcher" 7 android:label="@string/app_name" 8 android:supportsRtl="true" 9 android:theme="@style/Theme.AddressbookFragment"> 10 <activity 11 android:name=".MainActivity" 12 android:exported="true"> 13 <intent-filter> 14 <action android:name="android.intent.action.MAIN" /> 15 16 <category android:name="android.intent.category.LAUNCHER" /> 17 </intent-filter> 18 </activity> 19 </application> 20 21</manifest>

試したこと

別モジュールでFragmentが理想通りに表示されないため、自分の理解が追いついていないと考え、上記の簡易サンプルを書いてみましたが、こちらも理想通りに表示されません。
また、Androidの実機が手元にないため、エミュレータ上でしかテストをしておりません。

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

開発環境
Android Studio Electric Eel | 2022.1.1 Patch 1
ビルド #AI-221.6008.13.2211.9514443、ビルド日 2023年1月21日
ランタイムバージョン: 11.0.15+0-b2043.56-8887301 aarch64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.

エミュレータ;Pixel6 API 33(プリインストールされていたもの)

macOS Ventura 13.2
M1 MacBook Air RAM 16 GB

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

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

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

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

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

guest

回答1

0

自己解決

自己解決しました。
SampleFragment.java(18行目)

java

1return inflater.inflate( R.layout.fragment_main, container, false );

に書き換え忘れていました。恥ずかしい。

投稿2023/02/13 08:00

ooyama_burger

総合スコア5

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問