質問編集履歴
1
MainActivityを編集しました
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
### 前提・実現したいこと
|
2
|
-
現在、android用のアプリを作りたいと思っていて、Bottom navigationで画面を切り替え、その切り替え後の画面によっては画面上部に設置したTabによってさらに画面を切り替えられるようにしたいと思っています。
|
3
|
-
現在の認識として、TabもBottom navigationもどの画面を表示するかの操作をするもの(マスター?のようなもの)はActivityでしかできないという認識なのですがその認識であっているかについてもご教授いただけますと幸いです。
|
2
|
+
現在、android用のアプリを作りたいと思っていて、Bottom navigationで画面を切り替え、その切り替え後の画面によっては画面上部に設置したTabによってさらに画面を切り替えられるようにしたいと思っています。ただ、ビルドは通るのですがエミュレータ上では即強制終了となってしまい原因がわからず困っています。
|
4
3
|
|
5
4
|
### 該当のソースコード
|
6
5
|
|
@@ -20,20 +19,93 @@
|
|
20
19
|
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
|
21
20
|
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
|
22
21
|
NavigationUI.setupWithNavController(navView, navController);
|
22
|
+
//以下、Tab
|
23
|
+
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
|
24
|
+
ViewPager viewPager = findViewById(R.id.view_pager);
|
25
|
+
viewPager.setAdapter(sectionsPagerAdapter);
|
26
|
+
TabLayout tabs = findViewById(R.id.tabs);
|
27
|
+
tabs.setupWithViewPager(viewPager);
|
28
|
+
FloatingActionButton fab = findViewById(R.id.fab);
|
29
|
+
|
30
|
+
fab.setOnClickListener(new View.OnClickListener() {
|
31
|
+
@Override
|
32
|
+
public void onClick(View view) {
|
33
|
+
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
|
34
|
+
.setAction("Action", null).show();
|
35
|
+
}
|
36
|
+
});
|
37
|
+
|
23
38
|
}
|
24
39
|
|
25
40
|
}
|
41
|
+
```
|
42
|
+
```Java
|
43
|
+
//activity_main.xml
|
44
|
+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
45
|
+
xmlns:app="http://schemas.android.com/apk/res-auto"
|
46
|
+
android:id="@+id/container"
|
47
|
+
android:layout_width="match_parent"
|
48
|
+
android:layout_height="match_parent"
|
49
|
+
android:paddingTop="?attr/actionBarSize">
|
26
50
|
|
51
|
+
<com.google.android.material.bottomnavigation.BottomNavigationView
|
52
|
+
android:id="@+id/nav_view"
|
53
|
+
android:layout_width="0dp"
|
54
|
+
android:layout_height="wrap_content"
|
55
|
+
android:layout_marginStart="0dp"
|
56
|
+
android:layout_marginEnd="0dp"
|
57
|
+
android:background="?android:attr/windowBackground"
|
58
|
+
app:layout_constraintBottom_toBottomOf="parent"
|
59
|
+
app:layout_constraintLeft_toLeftOf="parent"
|
60
|
+
app:layout_constraintRight_toRightOf="parent"
|
61
|
+
app:menu="@menu/bottom_nav_menu" />
|
62
|
+
|
63
|
+
<androidx.fragment.app.FragmentContainerView
|
64
|
+
android:id="@+id/nav_host_fragment"
|
65
|
+
android:name="androidx.navigation.fragment.NavHostFragment"
|
66
|
+
android:layout_width="match_parent"
|
67
|
+
android:layout_height="match_parent"
|
68
|
+
app:defaultNavHost="true"
|
69
|
+
app:layout_constraintBottom_toTopOf="@id/nav_view"
|
70
|
+
app:layout_constraintLeft_toLeftOf="parent"
|
71
|
+
app:layout_constraintRight_toRightOf="parent"
|
72
|
+
app:layout_constraintTop_toTopOf="parent"
|
73
|
+
app:navGraph="@navigation/mobile_navigation" />
|
74
|
+
|
75
|
+
<com.google.android.material.appbar.AppBarLayout
|
76
|
+
android:layout_width="match_parent"
|
77
|
+
android:layout_height="wrap_content"
|
78
|
+
app:layout_constraintTop_toTopOf="parent"
|
79
|
+
android:theme="@style/AppTheme.AppBarOverlay">
|
80
|
+
|
81
|
+
<TextView
|
82
|
+
android:id="@+id/title"
|
83
|
+
android:layout_width="wrap_content"
|
84
|
+
android:layout_height="wrap_content"
|
85
|
+
android:gravity="center"
|
86
|
+
android:minHeight="?actionBarSize"
|
87
|
+
android:padding="@dimen/appbar_padding"
|
88
|
+
android:text="@string/app_name"
|
89
|
+
android:textAppearance="@style/TextAppearance.Widget.AppCompat.Toolbar.Title" />
|
90
|
+
|
91
|
+
<com.google.android.material.tabs.TabLayout
|
92
|
+
android:id="@+id/tabs"
|
93
|
+
android:layout_width="match_parent"
|
94
|
+
android:layout_height="wrap_content"
|
95
|
+
android:background="?attr/colorPrimary" />
|
96
|
+
</com.google.android.material.appbar.AppBarLayout>
|
97
|
+
|
98
|
+
<androidx.viewpager.widget.ViewPager
|
99
|
+
android:id="@+id/view_pager"
|
100
|
+
android:layout_width="match_parent"
|
101
|
+
android:layout_height="match_parent"
|
102
|
+
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
|
103
|
+
|
27
104
|
```
|
28
105
|
|
29
|
-
###
|
106
|
+
### 追加情報(何かの判断材料になれば)
|
107
|
+
MainActivityの「//以下、Tab」と記載してある場所から下のものを追加してから今の状態になっています。
|
30
108
|
|
31
|
-
Bottom navigationによって呼び出されるFragmentの1つに
|
32
|
-
@Override
|
33
|
-
public void startActivity(Intent intent) {
|
34
|
-
super.startActivity(intent);
|
35
|
-
}
|
36
|
-
を書き、Tabを設置してあるActivityを呼び出そうと思ったのですがうまくいきませんでした。
|
37
109
|
|
38
110
|
### 補足情報(FW/ツールのバージョンなど)
|
39
111
|
|