質問編集履歴
3
タグの変更
test
CHANGED
File without changes
|
test
CHANGED
File without changes
|
2
MainActivity.ktの追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -130,6 +130,60 @@
|
|
130
130
|
|
131
131
|
### 該当のソースコード
|
132
132
|
|
133
|
+
MainActivity.kt
|
134
|
+
|
135
|
+
```kotlin
|
136
|
+
|
137
|
+
import androidx.appcompat.app.AppCompatActivity
|
138
|
+
|
139
|
+
import android.os.Bundle
|
140
|
+
|
141
|
+
import androidx.fragment.app.Fragment
|
142
|
+
|
143
|
+
import kotlinx.android.synthetic.main.activity_main.*
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
class MainActivity : AppCompatActivity() {
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
override fun onCreate(savedInstanceState: Bundle?) {
|
152
|
+
|
153
|
+
super.onCreate(savedInstanceState)
|
154
|
+
|
155
|
+
setContentView(R.layout.activity_main)
|
156
|
+
|
157
|
+
// make the list of fragment
|
158
|
+
|
159
|
+
val fragmentList = arrayListOf<Fragment>(
|
160
|
+
|
161
|
+
FragmentOne(),
|
162
|
+
|
163
|
+
FragmentTwo()
|
164
|
+
|
165
|
+
)
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
// create instance of adapter
|
170
|
+
|
171
|
+
val adapter = SamplePagerAdapter(supportFragmentManager, fragmentList)
|
172
|
+
|
173
|
+
/// set adapter
|
174
|
+
|
175
|
+
viewPager.adapter = adapter
|
176
|
+
|
177
|
+
}
|
178
|
+
|
179
|
+
}
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
```
|
184
|
+
|
185
|
+
|
186
|
+
|
133
187
|
FragmentTwo.kt
|
134
188
|
|
135
189
|
```Kotlin
|
1
誤字脱字
test
CHANGED
File without changes
|
test
CHANGED
@@ -8,13 +8,121 @@
|
|
8
8
|
|
9
9
|
|
10
10
|
|
11
|
+
具体的には、Fragment間とFragment-Activity間の画面遷移を両立したアプリ作成したいです。
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
- Fragment間の画面遷移は、[[kotlin] ViewPagerでフラグメントを切り替える](https://qiita.com/YS-BETA/items/091a84961d5b56fced2b)の記事を参考に
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
- Fragment-ActivityのActivityは、[Kotlinページ遷移 フォーム intent RadioButton](https://www.yuulinux.tokyo/14255/)の記事に掲載されているコードを参考にしています。
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
ファイル構成は以下のようになっており、
|
24
|
+
|
25
|
+
- Fragment間の画面遷移は、「FragmentOne, FragmentTwo」
|
26
|
+
|
27
|
+
- Fragment-Activity間の遷移は、「FragmentTwo, FormActivityOne」
|
28
|
+
|
29
|
+
で行われます。
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
![files](d439bf35247049c7456587675f7d2572.png)
|
34
|
+
|
35
|
+
|
36
|
+
|
11
37
|
### 発生している問題・エラーメッセージ
|
12
38
|
|
13
|
-
|
39
|
+
FragmentTwoからFormActivityOneを呼び出すときに、ビルドでは問題が起こらないのですが、エミュレータで起動して、`FragmentTwo`のボタンを押すと、`FormActivityOne`に移動せずにクラッシュします。
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
強制的にアプリが落ちて、エミュレータ上では何も出ないのですが、Android Studioの`Logcat`では以下のエラーメッセージが表示されていました。
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
プログラム上の文法などのエラーではなく、エラー内容の理解と解決策がわからず、困っている状態です。
|
14
48
|
|
15
49
|
```
|
16
50
|
|
17
|
-
|
51
|
+
2020-03-11 20:47:33.241 3016-3016/com.example.transition W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@1c9a799
|
52
|
+
|
53
|
+
2020-03-11 20:47:33.280 3016-3016/com.example.transition D/AndroidRuntime: Shutting down VM
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
--------- beginning of crash
|
60
|
+
|
61
|
+
2020-03-11 20:47:33.282 3016-3016/com.example.transition E/AndroidRuntime: FATAL EXCEPTION: main
|
62
|
+
|
63
|
+
Process: com.example.transition, PID: 3016
|
64
|
+
|
65
|
+
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
|
66
|
+
|
67
|
+
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
|
68
|
+
|
69
|
+
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
|
70
|
+
|
71
|
+
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
|
72
|
+
|
73
|
+
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
|
74
|
+
|
75
|
+
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
|
76
|
+
|
77
|
+
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
|
78
|
+
|
79
|
+
at android.os.Handler.dispatchMessage(Handler.java:106)
|
80
|
+
|
81
|
+
at android.os.Looper.loop(Looper.java:193)
|
82
|
+
|
83
|
+
at android.app.ActivityThread.main(ActivityThread.java:6669)
|
84
|
+
|
85
|
+
at java.lang.reflect.Method.invoke(Native Method)
|
86
|
+
|
87
|
+
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
|
88
|
+
|
89
|
+
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
|
90
|
+
|
91
|
+
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
|
92
|
+
|
93
|
+
at com.example.transition.FormActivityOne.onCreate(FormActivityOne.kt:29)
|
94
|
+
|
95
|
+
at android.app.Activity.performCreate(Activity.java:7136)
|
96
|
+
|
97
|
+
at android.app.Activity.performCreate(Activity.java:7127)
|
98
|
+
|
99
|
+
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
|
100
|
+
|
101
|
+
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
|
102
|
+
|
103
|
+
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
|
104
|
+
|
105
|
+
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
|
106
|
+
|
107
|
+
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
|
108
|
+
|
109
|
+
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
|
110
|
+
|
111
|
+
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
|
112
|
+
|
113
|
+
at android.os.Handler.dispatchMessage(Handler.java:106)
|
114
|
+
|
115
|
+
at android.os.Looper.loop(Looper.java:193)
|
116
|
+
|
117
|
+
at android.app.ActivityThread.main(ActivityThread.java:6669)
|
118
|
+
|
119
|
+
at java.lang.reflect.Method.invoke(Native Method)
|
120
|
+
|
121
|
+
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
|
122
|
+
|
123
|
+
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
|
124
|
+
|
125
|
+
2020-03-11 20:47:33.301 3016-3016/com.example.transition I/Process: Sending signal. PID: 3016 SIG: 9
|
18
126
|
|
19
127
|
```
|
20
128
|
|
@@ -22,21 +130,321 @@
|
|
22
130
|
|
23
131
|
### 該当のソースコード
|
24
132
|
|
25
|
-
|
133
|
+
FragmentTwo.kt
|
26
|
-
|
134
|
+
|
27
|
-
```
|
135
|
+
```Kotlin
|
136
|
+
|
28
|
-
|
137
|
+
import android.content.Intent
|
138
|
+
|
139
|
+
import android.os.Bundle
|
140
|
+
|
141
|
+
import android.view.LayoutInflater
|
142
|
+
|
143
|
+
import android.view.View
|
144
|
+
|
145
|
+
import android.view.ViewGroup
|
146
|
+
|
147
|
+
import androidx.fragment.app.Fragment
|
148
|
+
|
149
|
+
import kotlinx.android.synthetic.main.fragment_two.*
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
class FragmentTwo : Fragment() {
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
override fun onCreateView(
|
158
|
+
|
159
|
+
inflater: LayoutInflater, container: ViewGroup?,
|
160
|
+
|
161
|
+
savedInstanceState: Bundle?
|
162
|
+
|
29
|
-
|
163
|
+
): View? {
|
164
|
+
|
165
|
+
return inflater.inflate(R.layout.fragment_two, container, false)
|
166
|
+
|
167
|
+
}
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
override fun onActivityCreated(savedInstanceState: Bundle?) {
|
172
|
+
|
173
|
+
super.onActivityCreated(savedInstanceState)
|
174
|
+
|
175
|
+
btnClick.setOnClickListener(object:View.OnClickListener{
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
// ここでActivityを呼ぶ
|
180
|
+
|
181
|
+
override fun onClick(v: View?) {
|
182
|
+
|
183
|
+
activity?.startActivity(Intent(context, FormActivityOne::class.java))
|
184
|
+
|
185
|
+
}
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
})
|
190
|
+
|
191
|
+
}
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
}
|
30
196
|
|
31
197
|
```
|
32
198
|
|
33
199
|
|
34
200
|
|
201
|
+
FormActivityOne.kt
|
202
|
+
|
203
|
+
```kotlin
|
204
|
+
|
205
|
+
import android.app.DatePickerDialog
|
206
|
+
|
207
|
+
import android.content.Intent
|
208
|
+
|
209
|
+
import androidx.appcompat.app.AppCompatActivity
|
210
|
+
|
211
|
+
import android.os.Bundle
|
212
|
+
|
213
|
+
import android.view.View
|
214
|
+
|
215
|
+
import android.widget.*
|
216
|
+
|
217
|
+
import android.widget.RadioGroup
|
218
|
+
|
219
|
+
import java.util.*
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
class FormActivityOne : AppCompatActivity() {
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
override fun onCreate(savedInstanceState: Bundle?) {
|
230
|
+
|
231
|
+
super.onCreate(savedInstanceState)
|
232
|
+
|
233
|
+
setContentView(R.layout.activity_main)
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
/**
|
238
|
+
|
239
|
+
* 生年月日取得のCalendar
|
240
|
+
|
241
|
+
*/
|
242
|
+
|
243
|
+
val bntDate = findViewById<Button>(R.id.btnDate)
|
244
|
+
|
245
|
+
val editBirth = findViewById<EditText>(R.id.editBirth)
|
246
|
+
|
247
|
+
val calendar= Calendar.getInstance()
|
248
|
+
|
249
|
+
val year = calendar.get(Calendar.YEAR)
|
250
|
+
|
251
|
+
val month = calendar.get(Calendar.MONTH)
|
252
|
+
|
253
|
+
val day = calendar.get(Calendar.DAY_OF_MONTH)
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
bntDate.setOnClickListener{
|
258
|
+
|
259
|
+
val datePickerDialog = DatePickerDialog(this@FormActivityOne, DatePickerDialog.OnDateSetListener
|
260
|
+
|
261
|
+
{ view, year, monthOfYear, dayOfMonth ->
|
262
|
+
|
263
|
+
editBirth.setText("" + year + "-" + (monthOfYear+1) + "-" + dayOfMonth)
|
264
|
+
|
265
|
+
}, year, month, day)
|
266
|
+
|
267
|
+
datePickerDialog.show()
|
268
|
+
|
269
|
+
}
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
/**
|
276
|
+
|
277
|
+
* 新規登録ボタンを押して次のページへ繊維
|
278
|
+
|
279
|
+
*/
|
280
|
+
|
281
|
+
val btClick = findViewById<Button>(R.id.bt_add)
|
282
|
+
|
283
|
+
val listener = ClickListener()
|
284
|
+
|
285
|
+
btClick.setOnClickListener(listener)
|
286
|
+
|
287
|
+
}
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
private inner class ClickListener : View.OnClickListener {
|
292
|
+
|
293
|
+
override fun onClick(v: View?) {
|
294
|
+
|
295
|
+
val editNickname = findViewById<EditText>(R.id.editNickname)
|
296
|
+
|
297
|
+
val editBirth = findViewById<EditText>(R.id.editBirth)
|
298
|
+
|
299
|
+
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
/**
|
304
|
+
|
305
|
+
* ジェンダーラジオボタン
|
306
|
+
|
307
|
+
*/
|
308
|
+
|
309
|
+
val rg_gender: RadioGroup
|
310
|
+
|
311
|
+
val rb_men: RadioButton
|
312
|
+
|
313
|
+
val rb_women: RadioButton
|
314
|
+
|
315
|
+
val rb_other: RadioButton
|
316
|
+
|
317
|
+
var index = ""
|
318
|
+
|
319
|
+
|
320
|
+
|
321
|
+
rg_gender = findViewById(R.id.radioGroup)
|
322
|
+
|
323
|
+
rb_men = findViewById(R.id.rb_men)
|
324
|
+
|
325
|
+
rb_women = findViewById(R.id.rb_women)
|
326
|
+
|
327
|
+
rb_other = findViewById(R.id.rb_other)
|
328
|
+
|
329
|
+
|
330
|
+
|
331
|
+
if(rg_gender.checkedRadioButtonId != -1) {
|
332
|
+
|
333
|
+
if (rb_men.isChecked)
|
334
|
+
|
335
|
+
index = "Men"
|
336
|
+
|
337
|
+
else if (rb_women.isChecked)
|
338
|
+
|
339
|
+
index = "Women"
|
340
|
+
|
341
|
+
else if (rb_other.isChecked)
|
342
|
+
|
343
|
+
index = "Other"
|
344
|
+
|
345
|
+
}
|
346
|
+
|
347
|
+
|
348
|
+
|
349
|
+
|
350
|
+
|
351
|
+
/**
|
352
|
+
|
353
|
+
* 次のページに値を渡します
|
354
|
+
|
355
|
+
*/
|
356
|
+
|
357
|
+
val intent = Intent(applicationContext, FormActivityTwo::class.java)
|
358
|
+
|
359
|
+
intent.putExtra("nickname", editNickname.text.toString())
|
360
|
+
|
361
|
+
intent.putExtra("birth", editBirth.text.toString())
|
362
|
+
|
363
|
+
intent.putExtra("gender", index.toString())
|
364
|
+
|
365
|
+
startActivity(intent)
|
366
|
+
|
367
|
+
|
368
|
+
|
369
|
+
}
|
370
|
+
|
371
|
+
}
|
372
|
+
|
373
|
+
}
|
374
|
+
|
375
|
+
```
|
376
|
+
|
377
|
+
|
378
|
+
|
35
379
|
### 試したこと
|
36
380
|
|
37
381
|
|
38
382
|
|
383
|
+
`AndroidManifest.xml`には、MainActivity以外の新たに作成した`FormActivityOne、Two`も加えていますが、現在のエラーは解決していません。
|
384
|
+
|
385
|
+
|
386
|
+
|
387
|
+
```xml
|
388
|
+
|
389
|
+
<?xml version="1.0" encoding="utf-8"?>
|
390
|
+
|
391
|
+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
392
|
+
|
393
|
+
package="com.example.transition">
|
394
|
+
|
395
|
+
|
396
|
+
|
397
|
+
<application
|
398
|
+
|
399
|
+
android:allowBackup="true"
|
400
|
+
|
401
|
+
android:icon="@mipmap/ic_launcher"
|
402
|
+
|
403
|
+
android:label="@string/app_name"
|
404
|
+
|
405
|
+
android:roundIcon="@mipmap/ic_launcher_round"
|
406
|
+
|
407
|
+
android:supportsRtl="true"
|
408
|
+
|
409
|
+
android:theme="@style/AppTheme">
|
410
|
+
|
411
|
+
<activity android:name=".MainActivity">
|
412
|
+
|
413
|
+
<intent-filter>
|
414
|
+
|
415
|
+
<action android:name="android.intent.action.MAIN" />
|
416
|
+
|
417
|
+
|
418
|
+
|
419
|
+
<category android:name="android.intent.category.LAUNCHER" />
|
420
|
+
|
421
|
+
</intent-filter>
|
422
|
+
|
423
|
+
</activity>
|
424
|
+
|
425
|
+
<activity
|
426
|
+
|
427
|
+
android:name=".FormActivityOne"
|
428
|
+
|
429
|
+
android:label="@string/app_name" >
|
430
|
+
|
431
|
+
</activity>
|
432
|
+
|
433
|
+
<activity
|
434
|
+
|
435
|
+
android:name=".FormActivityTwo"
|
436
|
+
|
437
|
+
android:label="@string/app_name" >
|
438
|
+
|
439
|
+
</activity>
|
440
|
+
|
39
|
-
|
441
|
+
</application>
|
442
|
+
|
443
|
+
|
444
|
+
|
445
|
+
</manifest>
|
446
|
+
|
447
|
+
```
|
40
448
|
|
41
449
|
|
42
450
|
|