回答編集履歴
1
修正回答
answer
CHANGED
@@ -12,4 +12,57 @@
|
|
12
12
|
textView1 = findViewById(R.id.text_view1);
|
13
13
|
```
|
14
14
|
|
15
|
-
とする必要があるのではないでしょうか。
|
15
|
+
とする必要があるのではないでしょうか。
|
16
|
+
|
17
|
+
---
|
18
|
+
|
19
|
+
あー、これコードで作成したFrameLayoutを描画しているのですね。すると、レイアウトXMLに配置したTextViewは画面上のどこにも存在しませんから、これをfindViewById()で取得することもできません。これは設計の方向性を改めないと解決できないでしょう。
|
20
|
+
|
21
|
+
一つの方法としてですが、次のように修正してみてはどうでしょうか。これだと、レイアウトXMLに配置したTextViewを扱えるので、文字の描画もできるでしょう。
|
22
|
+
|
23
|
+
activity_main.xml (android:idの追加)
|
24
|
+
```xml
|
25
|
+
<?xml version="1.0" encoding="utf-8"?>
|
26
|
+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
27
|
+
xmlns:app="http://schemas.android.com/apk/res-auto"
|
28
|
+
xmlns:tools="http://schemas.android.com/tools"
|
29
|
+
android:id="@+id/root"
|
30
|
+
android:layout_width="match_parent"
|
31
|
+
android:layout_height="match_parent"
|
32
|
+
tools:context=".MainActivity">
|
33
|
+
|
34
|
+
(以下略)
|
35
|
+
```
|
36
|
+
|
37
|
+
MainActivity.java
|
38
|
+
```java
|
39
|
+
public class MainActivity extends AppCompatActivity implements Runnable, SensorEventListener {
|
40
|
+
|
41
|
+
//FrameLayout framelayout; // やめる
|
42
|
+
ConstraintLayout layout; // 代わりに新設
|
43
|
+
|
44
|
+
@Override
|
45
|
+
protected void onCreate(Bundle savedInstanceState) {
|
46
|
+
super.onCreate(savedInstanceState);
|
47
|
+
getWindow().addFlags(
|
48
|
+
WindowManager.LayoutParams.FLAG_FULLSCREEN);
|
49
|
+
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
50
|
+
|
51
|
+
//framelayout = new FrameLayout(this);
|
52
|
+
//framelayout.setBackgroundColor(Color.GREEN);
|
53
|
+
//setContentView(framelayout);
|
54
|
+
|
55
|
+
// 代わりに追加する処理
|
56
|
+
setContentView(R.layout.activity_main);
|
57
|
+
layout = findViewById(R.id.root);
|
58
|
+
layout.setBackgroundColor(Color.GREEN);
|
59
|
+
textView1 = findViewById(R.id.text_view1);
|
60
|
+
|
61
|
+
(中略)
|
62
|
+
|
63
|
+
//framelayout.addView(hole);
|
64
|
+
//framelayout.addView(ball);
|
65
|
+
layout.addView(hole);
|
66
|
+
layout.addView(ball);
|
67
|
+
}
|
68
|
+
```
|