[Android Studio] 하단탭 사용하기 (Fragment) 이용

vector13·2021년 7월 21일
0

Android

목록 보기
2/12

Fragment 를 이용해서 하단탭을 만들 경우 내가 놓친 포인트 몇가지
1. Fragment 내용을 onCreateView 부분만 남기기
2. activity_main.xml에서 상위Layout에 id 주기

위 포인트를 놓쳤을 경우 마주치는 에러

java.lang.IllegalArgumentException: No view found for id 0x7f080072 (com.example.hiker:id/container) for fragment FragmentA{a83fc91} (d3e087db-49da-45a2-86bb-17bb2f2efc30 id=0x7f080072) ..(중략)..

MainActivity.java 의 코드

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;

import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity {

    Fragment fragment1;
    Fragment fragment2;
    //FragmentC fragment3;
    //FragmentD fragment4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fragment1 = new FragmentA();
        fragment2 = new FragmentB();

        getSupportFragmentManager().beginTransaction().replace(R.id.container,fragment1).commit();

        BottomNavigationView bottomNavigation = findViewById(R.id.bottom_navigation);
        bottomNavigation.setOnNavigationItemSelectedListener(
                new BottomNavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(@NonNull MenuItem item){
                        switch (item.getItemId()) {
                            case R.id.tab1:
                                Toast.makeText(getApplicationContext(), "첫번째", Toast.LENGTH_SHORT).show();
                                getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment1).commit();
                                return true;

                            case R.id.tab2:
                                Toast.makeText(getApplicationContext(), "두번째", Toast.LENGTH_SHORT).show();
                                getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment2).commit();
                                return true;
                        }
                        return false;
                    }

        });

    }
}

FragmentA.java 들의 코드의 기본(나머지도 동일)

package com.example.hiker;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class FragmentA extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_a, container, false);
    }
}

MainActivity에서 계속 container를 바꿔주고 있으므로
activity_main.xml에 변경될 상위 layout의 id를 부여해야함.
(Kotlin 버전에서는 자동으로 되었던걸로 기억함..! )

activity_main.xml

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    >

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:backgroundTint="#DFF4C7"
        app:labelVisibilityMode="labeled"
        app:menu="@menu/btmmenu" />

</FrameLayout>

도움된 블로그의 주소 (빛.. ☆)
https://gameprograming.tistory.com/140

profile
HelloWorld! 같은 실수를 반복하지 말기위해 적어두자..

0개의 댓글