回答編集履歴
4
追記
answer
CHANGED
|
@@ -91,4 +91,4 @@
|
|
|
91
91
|
}
|
|
92
92
|
```
|
|
93
93
|
|
|
94
|
-
本当はこういう丸写しで解決できてしまう回答をすることは、私が他の回答者から邪道だと言われても仕方ないものだということをご理解くださいね。
|
|
94
|
+
本当はこういう丸写しで解決できてしまう回答をすることは、私が他の回答者から邪道だと言われても仕方ないものだということをご理解くださいね。本来、最初の回答で初心者であっても理解できなければならない内容のはずです。
|
3
丸写しできる回答
answer
CHANGED
|
@@ -44,4 +44,51 @@
|
|
|
44
44
|
```
|
|
45
45
|
のような記述をされているのだろうと思います(ボタンのラベルとするため?)。しかし、このような状況で前述のimport文を記述すると、このstrings.xmlの情報を参照しようとしてしまうため、コンパイラーはbutton2という名前をe.masaya.myapplication.R.string.button2という完全修飾名の変数として扱い、Buttonコンポーネントとして取り扱ってくれません(R.xxx.yyyの形式で表現されるのは全てint型変数です)。そのためにエラーとなっています。
|
|
46
46
|
|
|
47
|
-
`button.setOnClickListener`ではエラーにならなかったとのことですが、これはレイアウトXMLファイルにbuttonというIDを付けたコンポーネントが存在していたからではありませんか?
|
|
47
|
+
`button.setOnClickListener`ではエラーにならなかったとのことですが、これはレイアウトXMLファイルにbuttonというIDを付けたコンポーネントが存在していたからではありませんか?
|
|
48
|
+
|
|
49
|
+
---
|
|
50
|
+
|
|
51
|
+
Activityのど真ん中に置いたボタンにbutton2というIDを付けて、このボタンを押したときにSecondActivityに遷移するコードです。なお、Kotlinコードからは1行目のpackage文だけ除いています。
|
|
52
|
+
|
|
53
|
+
```xml
|
|
54
|
+
<?xml version="1.0" encoding="utf-8"?>
|
|
55
|
+
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
|
56
|
+
xmlns:app="http://schemas.android.com/apk/res-auto"
|
|
57
|
+
xmlns:tools="http://schemas.android.com/tools"
|
|
58
|
+
android:layout_width="match_parent"
|
|
59
|
+
android:layout_height="match_parent"
|
|
60
|
+
tools:context=".MainActivity">
|
|
61
|
+
|
|
62
|
+
<Button
|
|
63
|
+
android:id="@+id/button2"
|
|
64
|
+
android:layout_width="wrap_content"
|
|
65
|
+
android:layout_height="wrap_content"
|
|
66
|
+
android:text="Button"
|
|
67
|
+
app:layout_constraintBottom_toBottomOf="parent"
|
|
68
|
+
app:layout_constraintEnd_toEndOf="parent"
|
|
69
|
+
app:layout_constraintStart_toStartOf="parent"
|
|
70
|
+
app:layout_constraintTop_toTopOf="parent" />
|
|
71
|
+
</android.support.constraint.ConstraintLayout>
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
```kotlin
|
|
75
|
+
import android.content.Intent
|
|
76
|
+
import android.os.Bundle
|
|
77
|
+
import android.support.v7.app.AppCompatActivity
|
|
78
|
+
import kotlinx.android.synthetic.main.activity_main.*
|
|
79
|
+
|
|
80
|
+
class MainActivity : AppCompatActivity() {
|
|
81
|
+
|
|
82
|
+
override fun onCreate(savedInstanceState: Bundle?) {
|
|
83
|
+
super.onCreate(savedInstanceState)
|
|
84
|
+
setContentView(R.layout.activity_main)
|
|
85
|
+
|
|
86
|
+
button2.setOnClickListener {
|
|
87
|
+
val intent = Intent(this, SecondActivity::class.java)
|
|
88
|
+
startActivity(intent)
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
本当はこういう丸写しで解決できてしまう回答をすることは、私が他の回答者から邪道だと言われても仕方ないものだということをご理解くださいね。
|
2
追加
answer
CHANGED
|
@@ -28,6 +28,10 @@
|
|
|
28
28
|
|
|
29
29
|
---
|
|
30
30
|
|
|
31
|
+
setOnClickListenerはButtonクラスに属するメソッドです。ですから、変数がButton型でなければ使えません。上記のKotlinコードでは、buttonという変数がButton型として扱われているためにsetOnClickListenerを用いることができます。
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
31
35
|
ここからが主題ですが、エラーになっているコードを見ると
|
|
32
36
|
|
|
33
37
|
```kotlin
|
1
修正
answer
CHANGED
|
@@ -38,6 +38,6 @@
|
|
|
38
38
|
```xml
|
|
39
39
|
<string name="button2">button2</string>
|
|
40
40
|
```
|
|
41
|
-
のような記述をされているのだろうと思います(ボタンのラベルとするため?)。しかし、
|
|
41
|
+
のような記述をされているのだろうと思います(ボタンのラベルとするため?)。しかし、このような状況で前述のimport文を記述すると、このstrings.xmlの情報を参照しようとしてしまうため、コンパイラーはbutton2という名前をe.masaya.myapplication.R.string.button2という完全修飾名の変数として扱い、Buttonコンポーネントとして取り扱ってくれません(R.xxx.yyyの形式で表現されるのは全てint型変数です)。そのためにエラーとなっています。
|
|
42
42
|
|
|
43
43
|
`button.setOnClickListener`ではエラーにならなかったとのことですが、これはレイアウトXMLファイルにbuttonというIDを付けたコンポーネントが存在していたからではありませんか?
|