質問編集履歴
3
タグの変更
title
CHANGED
File without changes
|
body
CHANGED
File without changes
|
2
MainActivity.ktの追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -64,6 +64,33 @@
|
|
64
64
|
```
|
65
65
|
|
66
66
|
### 該当のソースコード
|
67
|
+
MainActivity.kt
|
68
|
+
```kotlin
|
69
|
+
import androidx.appcompat.app.AppCompatActivity
|
70
|
+
import android.os.Bundle
|
71
|
+
import androidx.fragment.app.Fragment
|
72
|
+
import kotlinx.android.synthetic.main.activity_main.*
|
73
|
+
|
74
|
+
class MainActivity : AppCompatActivity() {
|
75
|
+
|
76
|
+
override fun onCreate(savedInstanceState: Bundle?) {
|
77
|
+
super.onCreate(savedInstanceState)
|
78
|
+
setContentView(R.layout.activity_main)
|
79
|
+
// make the list of fragment
|
80
|
+
val fragmentList = arrayListOf<Fragment>(
|
81
|
+
FragmentOne(),
|
82
|
+
FragmentTwo()
|
83
|
+
)
|
84
|
+
|
85
|
+
// create instance of adapter
|
86
|
+
val adapter = SamplePagerAdapter(supportFragmentManager, fragmentList)
|
87
|
+
/// set adapter
|
88
|
+
viewPager.adapter = adapter
|
89
|
+
}
|
90
|
+
}
|
91
|
+
|
92
|
+
```
|
93
|
+
|
67
94
|
FragmentTwo.kt
|
68
95
|
```Kotlin
|
69
96
|
import android.content.Intent
|
1
誤字脱字
title
CHANGED
File without changes
|
body
CHANGED
@@ -3,21 +3,225 @@
|
|
3
3
|
Androidアプリ開発で、FragmentからActivityを実行しようとしています。
|
4
4
|
開発言語はKotlinです。
|
5
5
|
|
6
|
+
具体的には、Fragment間とFragment-Activity間の画面遷移を両立したアプリ作成したいです。
|
7
|
+
|
8
|
+
- Fragment間の画面遷移は、[[kotlin] ViewPagerでフラグメントを切り替える](https://qiita.com/YS-BETA/items/091a84961d5b56fced2b)の記事を参考に
|
9
|
+
|
10
|
+
- Fragment-ActivityのActivityは、[Kotlinページ遷移 フォーム intent RadioButton](https://www.yuulinux.tokyo/14255/)の記事に掲載されているコードを参考にしています。
|
11
|
+
|
12
|
+
ファイル構成は以下のようになっており、
|
13
|
+
- Fragment間の画面遷移は、「FragmentOne, FragmentTwo」
|
14
|
+
- Fragment-Activity間の遷移は、「FragmentTwo, FormActivityOne」
|
15
|
+
で行われます。
|
16
|
+
|
17
|
+

|
18
|
+
|
6
19
|
### 発生している問題・エラーメッセージ
|
20
|
+
FragmentTwoからFormActivityOneを呼び出すときに、ビルドでは問題が起こらないのですが、エミュレータで起動して、`FragmentTwo`のボタンを押すと、`FormActivityOne`に移動せずにクラッシュします。
|
7
21
|
|
22
|
+
強制的にアプリが落ちて、エミュレータ上では何も出ないのですが、Android Studioの`Logcat`では以下のエラーメッセージが表示されていました。
|
23
|
+
|
24
|
+
プログラム上の文法などのエラーではなく、エラー内容の理解と解決策がわからず、困っている状態です。
|
8
25
|
```
|
9
|
-
|
26
|
+
2020-03-11 20:47:33.241 3016-3016/com.example.transition W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@1c9a799
|
27
|
+
2020-03-11 20:47:33.280 3016-3016/com.example.transition D/AndroidRuntime: Shutting down VM
|
28
|
+
|
29
|
+
|
30
|
+
--------- beginning of crash
|
31
|
+
2020-03-11 20:47:33.282 3016-3016/com.example.transition E/AndroidRuntime: FATAL EXCEPTION: main
|
32
|
+
Process: com.example.transition, PID: 3016
|
33
|
+
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.transition/com.example.transition.FormActivityOne}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
|
34
|
+
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
|
35
|
+
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
|
36
|
+
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
|
37
|
+
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
|
38
|
+
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
|
39
|
+
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
|
40
|
+
at android.os.Handler.dispatchMessage(Handler.java:106)
|
41
|
+
at android.os.Looper.loop(Looper.java:193)
|
42
|
+
at android.app.ActivityThread.main(ActivityThread.java:6669)
|
43
|
+
at java.lang.reflect.Method.invoke(Native Method)
|
44
|
+
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
|
45
|
+
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
|
46
|
+
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
|
47
|
+
at com.example.transition.FormActivityOne.onCreate(FormActivityOne.kt:29)
|
48
|
+
at android.app.Activity.performCreate(Activity.java:7136)
|
49
|
+
at android.app.Activity.performCreate(Activity.java:7127)
|
50
|
+
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
|
51
|
+
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
|
52
|
+
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
|
53
|
+
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
|
54
|
+
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
|
55
|
+
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
|
56
|
+
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
|
57
|
+
at android.os.Handler.dispatchMessage(Handler.java:106)
|
58
|
+
at android.os.Looper.loop(Looper.java:193)
|
59
|
+
at android.app.ActivityThread.main(ActivityThread.java:6669)
|
60
|
+
at java.lang.reflect.Method.invoke(Native Method)
|
61
|
+
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
|
62
|
+
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
|
63
|
+
2020-03-11 20:47:33.301 3016-3016/com.example.transition I/Process: Sending signal. PID: 3016 SIG: 9
|
10
64
|
```
|
11
65
|
|
12
66
|
### 該当のソースコード
|
67
|
+
FragmentTwo.kt
|
68
|
+
```Kotlin
|
69
|
+
import android.content.Intent
|
70
|
+
import android.os.Bundle
|
71
|
+
import android.view.LayoutInflater
|
72
|
+
import android.view.View
|
73
|
+
import android.view.ViewGroup
|
74
|
+
import androidx.fragment.app.Fragment
|
75
|
+
import kotlinx.android.synthetic.main.fragment_two.*
|
13
76
|
|
77
|
+
class FragmentTwo : Fragment() {
|
78
|
+
|
79
|
+
override fun onCreateView(
|
80
|
+
inflater: LayoutInflater, container: ViewGroup?,
|
81
|
+
savedInstanceState: Bundle?
|
14
|
-
|
82
|
+
): View? {
|
83
|
+
return inflater.inflate(R.layout.fragment_two, container, false)
|
84
|
+
}
|
85
|
+
|
86
|
+
override fun onActivityCreated(savedInstanceState: Bundle?) {
|
87
|
+
super.onActivityCreated(savedInstanceState)
|
88
|
+
btnClick.setOnClickListener(object:View.OnClickListener{
|
89
|
+
|
90
|
+
// ここでActivityを呼ぶ
|
91
|
+
override fun onClick(v: View?) {
|
92
|
+
activity?.startActivity(Intent(context, FormActivityOne::class.java))
|
93
|
+
}
|
94
|
+
|
15
|
-
|
95
|
+
})
|
96
|
+
}
|
97
|
+
|
98
|
+
}
|
16
99
|
```
|
17
100
|
|
101
|
+
FormActivityOne.kt
|
102
|
+
```kotlin
|
103
|
+
import android.app.DatePickerDialog
|
104
|
+
import android.content.Intent
|
105
|
+
import androidx.appcompat.app.AppCompatActivity
|
106
|
+
import android.os.Bundle
|
107
|
+
import android.view.View
|
108
|
+
import android.widget.*
|
109
|
+
import android.widget.RadioGroup
|
110
|
+
import java.util.*
|
111
|
+
|
112
|
+
|
113
|
+
class FormActivityOne : AppCompatActivity() {
|
114
|
+
|
115
|
+
override fun onCreate(savedInstanceState: Bundle?) {
|
116
|
+
super.onCreate(savedInstanceState)
|
117
|
+
setContentView(R.layout.activity_main)
|
118
|
+
|
119
|
+
/**
|
120
|
+
* 生年月日取得のCalendar
|
121
|
+
*/
|
122
|
+
val bntDate = findViewById<Button>(R.id.btnDate)
|
123
|
+
val editBirth = findViewById<EditText>(R.id.editBirth)
|
124
|
+
val calendar= Calendar.getInstance()
|
125
|
+
val year = calendar.get(Calendar.YEAR)
|
126
|
+
val month = calendar.get(Calendar.MONTH)
|
127
|
+
val day = calendar.get(Calendar.DAY_OF_MONTH)
|
128
|
+
|
129
|
+
bntDate.setOnClickListener{
|
130
|
+
val datePickerDialog = DatePickerDialog(this@FormActivityOne, DatePickerDialog.OnDateSetListener
|
131
|
+
{ view, year, monthOfYear, dayOfMonth ->
|
132
|
+
editBirth.setText("" + year + "-" + (monthOfYear+1) + "-" + dayOfMonth)
|
133
|
+
}, year, month, day)
|
134
|
+
datePickerDialog.show()
|
135
|
+
}
|
136
|
+
|
137
|
+
|
138
|
+
/**
|
139
|
+
* 新規登録ボタンを押して次のページへ繊維
|
140
|
+
*/
|
141
|
+
val btClick = findViewById<Button>(R.id.bt_add)
|
142
|
+
val listener = ClickListener()
|
143
|
+
btClick.setOnClickListener(listener)
|
144
|
+
}
|
145
|
+
|
146
|
+
private inner class ClickListener : View.OnClickListener {
|
147
|
+
override fun onClick(v: View?) {
|
148
|
+
val editNickname = findViewById<EditText>(R.id.editNickname)
|
149
|
+
val editBirth = findViewById<EditText>(R.id.editBirth)
|
150
|
+
|
151
|
+
|
152
|
+
/**
|
153
|
+
* ジェンダーラジオボタン
|
154
|
+
*/
|
155
|
+
val rg_gender: RadioGroup
|
156
|
+
val rb_men: RadioButton
|
157
|
+
val rb_women: RadioButton
|
158
|
+
val rb_other: RadioButton
|
159
|
+
var index = ""
|
160
|
+
|
161
|
+
rg_gender = findViewById(R.id.radioGroup)
|
162
|
+
rb_men = findViewById(R.id.rb_men)
|
163
|
+
rb_women = findViewById(R.id.rb_women)
|
164
|
+
rb_other = findViewById(R.id.rb_other)
|
165
|
+
|
166
|
+
if(rg_gender.checkedRadioButtonId != -1) {
|
167
|
+
if (rb_men.isChecked)
|
168
|
+
index = "Men"
|
169
|
+
else if (rb_women.isChecked)
|
170
|
+
index = "Women"
|
171
|
+
else if (rb_other.isChecked)
|
172
|
+
index = "Other"
|
173
|
+
}
|
174
|
+
|
175
|
+
|
176
|
+
/**
|
177
|
+
* 次のページに値を渡します
|
178
|
+
*/
|
179
|
+
val intent = Intent(applicationContext, FormActivityTwo::class.java)
|
180
|
+
intent.putExtra("nickname", editNickname.text.toString())
|
181
|
+
intent.putExtra("birth", editBirth.text.toString())
|
182
|
+
intent.putExtra("gender", index.toString())
|
183
|
+
startActivity(intent)
|
184
|
+
|
185
|
+
}
|
186
|
+
}
|
187
|
+
}
|
188
|
+
```
|
189
|
+
|
18
190
|
### 試したこと
|
19
191
|
|
20
|
-
|
192
|
+
`AndroidManifest.xml`には、MainActivity以外の新たに作成した`FormActivityOne、Two`も加えていますが、現在のエラーは解決していません。
|
21
193
|
|
194
|
+
```xml
|
195
|
+
<?xml version="1.0" encoding="utf-8"?>
|
196
|
+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
197
|
+
package="com.example.transition">
|
198
|
+
|
199
|
+
<application
|
200
|
+
android:allowBackup="true"
|
201
|
+
android:icon="@mipmap/ic_launcher"
|
202
|
+
android:label="@string/app_name"
|
203
|
+
android:roundIcon="@mipmap/ic_launcher_round"
|
204
|
+
android:supportsRtl="true"
|
205
|
+
android:theme="@style/AppTheme">
|
206
|
+
<activity android:name=".MainActivity">
|
207
|
+
<intent-filter>
|
208
|
+
<action android:name="android.intent.action.MAIN" />
|
209
|
+
|
210
|
+
<category android:name="android.intent.category.LAUNCHER" />
|
211
|
+
</intent-filter>
|
212
|
+
</activity>
|
213
|
+
<activity
|
214
|
+
android:name=".FormActivityOne"
|
215
|
+
android:label="@string/app_name" >
|
216
|
+
</activity>
|
217
|
+
<activity
|
218
|
+
android:name=".FormActivityTwo"
|
219
|
+
android:label="@string/app_name" >
|
220
|
+
</activity>
|
221
|
+
</application>
|
222
|
+
|
223
|
+
</manifest>
|
224
|
+
```
|
225
|
+
|
22
226
|
### 補足情報(FW/ツールのバージョンなど)
|
23
227
|
Android Studio 3.5.3
|