回答編集履歴
4
コメントへの回答追記
answer
CHANGED
@@ -118,4 +118,92 @@
|
|
118
118
|
|
119
119
|
のどちらかの修正を行うと起動すると思います。(どちらを修正するかはお好みで)
|
120
120
|
|
121
|
-
以上、よろしくお願いいたします。
|
121
|
+
以上、よろしくお願いいたします。
|
122
|
+
|
123
|
+
=== コメントに対する追記
|
124
|
+
stopボタンを押したときのエラーに関しては現状のコードだとMediaPlayerが作られていないので落ちてしまいます。startボタンを押した後にstopボタンを押すとエラーが出なくなると思います。
|
125
|
+
コードの修正としてはreleaseした後にnullを入れてnullのときはstopが押せないようにしておくと、とりあえずエラーで落ちることはなくなると思います。
|
126
|
+
|
127
|
+
stopボタンだけが表示されている件に関しては単純にレイアウトが指定されていないだけです。
|
128
|
+
layout_editor_absoluteXはデザイナー画面で表示されるだけで実際にはレイアウトされないようです(使ったことないので知りませんでした)
|
129
|
+
レイアウトに関しては今回のエラーとは直接関係ないので、とりあえず真ん中にボタンが表示されるレイアウトを示します。レイアウトの仕方についてはどのようにレイアウトしたいかがあるかと思いますので調べてみてください。
|
130
|
+
|
131
|
+
以下に、動かしたときのサンプルコードをおいておきますので適宜読み替えながら修正してみてください。(ちょっと回答が長くなってきたのでまだ修正がありそうでしたら、回答を分けようと思います。)
|
132
|
+
|
133
|
+
```
|
134
|
+
class MainActivity : AppCompatActivity() {
|
135
|
+
|
136
|
+
private var mediaPlayer: MediaPlayer? = null
|
137
|
+
|
138
|
+
override fun onCreate(savedInstanceState: Bundle?) {
|
139
|
+
super.onCreate(savedInstanceState)
|
140
|
+
setContentView(R.layout.activity_main)
|
141
|
+
|
142
|
+
// Button for start music
|
143
|
+
val buttonStart: Button = findViewById(R.id.start)
|
144
|
+
|
145
|
+
// Resister listener
|
146
|
+
buttonStart.setOnClickListener {
|
147
|
+
startMusic()
|
148
|
+
}
|
149
|
+
|
150
|
+
// Button for stop music
|
151
|
+
val buttonStop: Button = findViewById(R.id.stop)
|
152
|
+
|
153
|
+
// Resister listener
|
154
|
+
buttonStop.setOnClickListener {
|
155
|
+
stopMusic()
|
156
|
+
}
|
157
|
+
|
158
|
+
}
|
159
|
+
|
160
|
+
fun startMusic() {
|
161
|
+
mediaPlayer = MediaPlayer.create(this, R.raw.test)
|
162
|
+
mediaPlayer?.isLooping = true
|
163
|
+
mediaPlayer?.start()
|
164
|
+
}
|
165
|
+
|
166
|
+
fun stopMusic() {
|
167
|
+
if(mediaPlayer == null){
|
168
|
+
return
|
169
|
+
}
|
170
|
+
|
171
|
+
mediaPlayer?.stop()
|
172
|
+
mediaPlayer?.reset()
|
173
|
+
mediaPlayer?.release()
|
174
|
+
mediaPlayer = null
|
175
|
+
}
|
176
|
+
}
|
177
|
+
```
|
178
|
+
|
179
|
+
```
|
180
|
+
<?xml version="1.0" encoding="utf-8"?>
|
181
|
+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
182
|
+
xmlns:app="http://schemas.android.com/apk/res-auto"
|
183
|
+
xmlns:tools="http://schemas.android.com/tools"
|
184
|
+
android:layout_width="match_parent"
|
185
|
+
android:layout_height="match_parent"
|
186
|
+
tools:context=".MainActivity">
|
187
|
+
|
188
|
+
<Button
|
189
|
+
android:id="@+id/start"
|
190
|
+
android:layout_width="wrap_content"
|
191
|
+
android:layout_height="wrap_content"
|
192
|
+
android:text="start"
|
193
|
+
app:layout_constraintBottom_toBottomOf="parent"
|
194
|
+
app:layout_constraintLeft_toLeftOf="parent"
|
195
|
+
app:layout_constraintRight_toRightOf="parent"
|
196
|
+
app:layout_constraintTop_toTopOf="parent" />
|
197
|
+
|
198
|
+
<Button
|
199
|
+
android:id="@+id/stop"
|
200
|
+
android:layout_width="wrap_content"
|
201
|
+
android:layout_height="wrap_content"
|
202
|
+
android:text="stop"
|
203
|
+
app:layout_constraintTop_toBottomOf="@+id/start"
|
204
|
+
app:layout_constraintLeft_toLeftOf="parent"
|
205
|
+
app:layout_constraintRight_toRightOf="parent"
|
206
|
+
/>
|
207
|
+
|
208
|
+
</androidx.constraintlayout.widget.ConstraintLayout>
|
209
|
+
```
|
3
コメントへの回答追記
answer
CHANGED
@@ -64,4 +64,58 @@
|
|
64
64
|
そこの部分が落ちないようになれば起動するかと思います。
|
65
65
|
エラーの修正方法がわからないようであればactivity_main.xmlの内容を記載していただけますでしょうか。
|
66
66
|
|
67
|
+
以上、よろしくお願いいたします。
|
68
|
+
|
69
|
+
=== コメントに対する追記
|
70
|
+
`findViewById(R.id.start_music)`、これは読み込んでいるレイアウトからIDを拾ってくるので`activity_main.xml`にstart_musicと言うIDがないとnullで落ちてしまいます。なので
|
71
|
+
|
72
|
+
xmlの方にidを合わせるのであれば
|
73
|
+
```
|
74
|
+
// Button for start music
|
75
|
+
val buttonStart: Button = findViewById(R.id.button)
|
76
|
+
|
77
|
+
// Resister listener
|
78
|
+
buttonStart.setOnClickListener {
|
79
|
+
startMusic()
|
80
|
+
}
|
81
|
+
|
82
|
+
// Button for stop music
|
83
|
+
val buttonStop: Button = findViewById(R.id.button2)
|
84
|
+
|
85
|
+
// Resister listener
|
86
|
+
buttonStop.setOnClickListener {
|
87
|
+
stopMusic()
|
88
|
+
}
|
89
|
+
```
|
90
|
+
|
91
|
+
とするか、もしくはxmlを修正するのであれば
|
92
|
+
```
|
93
|
+
<?xml version="1.0" encoding="utf-8"?>
|
94
|
+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
95
|
+
xmlns:app="http://schemas.android.com/apk/res-auto"
|
96
|
+
xmlns:tools="http://schemas.android.com/tools"
|
97
|
+
android:layout_width="match_parent"
|
98
|
+
android:layout_height="match_parent"
|
99
|
+
tools:context=".MainActivity">
|
100
|
+
|
101
|
+
<Button
|
102
|
+
android:id="@+id/start_music"
|
103
|
+
android:layout_width="wrap_content"
|
104
|
+
android:layout_height="wrap_content"
|
105
|
+
android:text="@string/start"
|
106
|
+
tools:layout_editor_absoluteX="143dp"
|
107
|
+
tools:layout_editor_absoluteY="119dp" />
|
108
|
+
|
109
|
+
<Button
|
110
|
+
android:id="@+id/stop_music"
|
111
|
+
android:layout_width="wrap_content"
|
112
|
+
android:layout_height="wrap_content"
|
113
|
+
android:text="@string/stop"
|
114
|
+
tools:layout_editor_absoluteX="138dp"
|
115
|
+
tools:layout_editor_absoluteY="363dp" />
|
116
|
+
</androidx.constraintlayout.widget.ConstraintLayout>
|
117
|
+
```
|
118
|
+
|
119
|
+
のどちらかの修正を行うと起動すると思います。(どちらを修正するかはお好みで)
|
120
|
+
|
67
121
|
以上、よろしくお願いいたします。
|
2
コメントへの回答追記
answer
CHANGED
@@ -54,4 +54,14 @@
|
|
54
54
|
アプリが落ちているのであればエラー情報がLogcatに出ていると思いますので、エラーログの情報をお願いいたします。
|
55
55
|
|
56
56
|
以下はLogcat Errorの表示場所です。
|
57
|
-

|
57
|
+

|
58
|
+
|
59
|
+
=== コメントに対する追記
|
60
|
+
MainActivity.ktの19行目でエラーが発生しており、findVieewById(R.id.start_music)が見つからなくてエラーが発生しています。
|
61
|
+
Logcatの青い部分を押すと該当行に飛びます。
|
62
|
+
|
63
|
+
activity_main.xmlにstart_musicが無いのかもしれません?
|
64
|
+
そこの部分が落ちないようになれば起動するかと思います。
|
65
|
+
エラーの修正方法がわからないようであればactivity_main.xmlの内容を記載していただけますでしょうか。
|
66
|
+
|
67
|
+
以上、よろしくお願いいたします。
|
1
コメントへの回答追記
answer
CHANGED
@@ -50,4 +50,8 @@
|
|
50
50
|
}
|
51
51
|
```
|
52
52
|
|
53
|
+
=== コメントに対する追記
|
54
|
+
アプリが落ちているのであればエラー情報がLogcatに出ていると思いますので、エラーログの情報をお願いいたします。
|
55
|
+
|
53
|
-
以
|
56
|
+
以下はLogcat Errorの表示場所です。
|
57
|
+

|