質問編集履歴

5

更新

2017/12/14 09:46

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -6,13 +6,25 @@
6
6
 
7
7
  public class Myapplication {
8
8
 
9
- public int x = 0;
9
+ public static void main(String[] args) {
10
10
 
11
- while(true){
11
+ try {
12
12
 
13
- x= x+1;
13
+ public int x = 0;
14
14
 
15
+ while(true){
16
+
17
+ x= x+1;
18
+
15
- }
19
+ }
20
+
21
+  }catch (IOException e) {
22
+
23
+ e.printStackTrace();
24
+
25
+ }
26
+
27
+ }
16
28
 
17
29
  }
18
30
 

4

変更

2017/12/14 09:46

投稿

退会済みユーザー
test CHANGED
@@ -1 +1 @@
1
- 他クラスからMainActivity値の受け渡し
1
+ ループ中変数を共有する
test CHANGED
@@ -4,55 +4,15 @@
4
4
 
5
5
  ```java
6
6
 
7
- public class Myapplication implements Runnable {
7
+ public class Myapplication {
8
8
 
9
9
  public int x = 0;
10
10
 
11
- Thread thread;
11
+ while(true){
12
12
 
13
+ x= x+1;
13
14
 
14
-
15
- public void start() {
16
-
17
- thread = new Thread(this);
18
-
19
- thread.setDaemon(true);
20
-
21
- thread.start();
22
-
23
- }
15
+ }
24
-
25
-
26
-
27
- public void stop() {
28
-
29
- thread.interrupt();
30
-
31
- thread = null;
32
-
33
- }
34
-
35
-
36
-
37
- @Override
38
-
39
- public void run() {
40
-
41
- try {
42
-
43
- for (;;) {
44
-
45
- Thread.sleep(100);
46
-
47
- x++;
48
-
49
- }
50
-
51
- } catch (InterruptedException e) {
52
-
53
- }
54
-
55
- }
56
16
 
57
17
  }
58
18
 
@@ -102,43 +62,11 @@
102
62
 
103
63
  public void onClick(View view) {
104
64
 
105
- Log.d("debug", "x=" + app.x);
65
+ textView.setText(app.x);
106
66
 
107
67
  }
108
68
 
109
- });
69
+ });
110
-
111
- textView.setText(app.x);
112
-
113
- }
114
-
115
-
116
-
117
-
118
-
119
- @Override
120
-
121
- public void onResume() {
122
-
123
- super.onResume();
124
-
125
- app = new Myapplication();
126
-
127
- app.start();
128
-
129
- }
130
-
131
-
132
-
133
- @Override
134
-
135
- public void onPause() {
136
-
137
- super.onPause();
138
-
139
- app.stop();
140
-
141
- app = null;
142
70
 
143
71
  }
144
72
 

3

変更

2017/12/12 09:31

投稿

退会済みユーザー
test CHANGED
@@ -1 +1 @@
1
- Context の渡し
1
+ 他クラスからMainActivity値の受け渡し
test CHANGED
@@ -108,6 +108,8 @@
108
108
 
109
109
  });
110
110
 
111
+ textView.setText(app.x);
112
+
111
113
  }
112
114
 
113
115
 
@@ -146,7 +148,7 @@
146
148
 
147
149
  ```
148
150
 
149
- このコードのxの値を他のクラス(例えばMainActivity)で使うためにcontextを使ってxの値を獲得したいのですが調べても具体的に使われているものを見つけられませんでした。
151
+ このコードのxの値を他のクラス(例えばMainActivity)でxの値を獲得したいのですが調べても具体的に使われているものを見つけられませんでした。
150
152
 
151
153
 
152
154
 

2

更新

2017/12/12 07:08

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -4,19 +4,51 @@
4
4
 
5
5
  ```java
6
6
 
7
- public class MyApplication{
7
+ public class Myapplication implements Runnable {
8
8
 
9
- int x=0;
9
+ public int x = 0;
10
10
 
11
-
11
+ Thread thread;
12
12
 
13
- public void test()
14
13
 
15
- {
16
14
 
17
- while (true) {
15
+ public void start() {
18
16
 
17
+ thread = new Thread(this);
18
+
19
+ thread.setDaemon(true);
20
+
21
+ thread.start();
22
+
23
+ }
24
+
25
+
26
+
27
+ public void stop() {
28
+
29
+ thread.interrupt();
30
+
31
+ thread = null;
32
+
33
+ }
34
+
35
+
36
+
37
+ @Override
38
+
39
+ public void run() {
40
+
41
+ try {
42
+
19
- x = x + 1;
43
+ for (;;) {
44
+
45
+ Thread.sleep(100);
46
+
47
+ x++;
48
+
49
+ }
50
+
51
+ } catch (InterruptedException e) {
20
52
 
21
53
  }
22
54
 
@@ -34,6 +66,12 @@
34
66
 
35
67
  import android.os.Bundle;
36
68
 
69
+ import android.util.Log;
70
+
71
+ import android.view.View;
72
+
73
+ import android.widget.Button;
74
+
37
75
  import android.widget.TextView;
38
76
 
39
77
 
@@ -41,10 +79,6 @@
41
79
  public class MainActivity extends AppCompatActivity {
42
80
 
43
81
  Myapplication app;
44
-
45
- String s;
46
-
47
-
48
82
 
49
83
 
50
84
 
@@ -56,21 +90,59 @@
56
90
 
57
91
  setContentView(R.layout.activity_main);
58
92
 
93
+ Button button = findViewById(R.id.button);
94
+
59
95
  TextView textView = findViewById(R.id.text_view);
96
+
97
+
98
+
99
+ button.setOnClickListener(new View.OnClickListener() {
100
+
101
+ @Override
102
+
103
+ public void onClick(View view) {
104
+
105
+ Log.d("debug", "x=" + app.x);
106
+
107
+ }
108
+
109
+ });
110
+
111
+ }
112
+
113
+
114
+
115
+
116
+
117
+ @Override
118
+
119
+ public void onResume() {
120
+
121
+ super.onResume();
60
122
 
61
123
  app = new Myapplication();
62
124
 
63
- int x = app.x;
125
+ app.start();
64
126
 
65
- s = Integer.toString(x);
66
-
67
- textView.setText(s);
127
+ }
68
128
 
69
129
 
130
+
131
+ @Override
132
+
133
+ public void onPause() {
134
+
135
+ super.onPause();
136
+
137
+ app.stop();
138
+
139
+ app = null;
70
140
 
71
141
  }
72
142
 
73
143
  }
144
+
145
+
74
146
 
75
147
  ```
76
148
 

1

更新

2017/12/12 07:01

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -28,6 +28,52 @@
28
28
 
29
29
 
30
30
 
31
+ ```java
32
+
33
+ import android.support.v7.app.AppCompatActivity;
34
+
35
+ import android.os.Bundle;
36
+
37
+ import android.widget.TextView;
38
+
39
+
40
+
41
+ public class MainActivity extends AppCompatActivity {
42
+
43
+ Myapplication app;
44
+
45
+ String s;
46
+
47
+
48
+
49
+
50
+
51
+ @Override
52
+
53
+ protected void onCreate(Bundle savedInstanceState) {
54
+
55
+ super.onCreate(savedInstanceState);
56
+
57
+ setContentView(R.layout.activity_main);
58
+
59
+ TextView textView = findViewById(R.id.text_view);
60
+
61
+ app = new Myapplication();
62
+
63
+ int x = app.x;
64
+
65
+ s = Integer.toString(x);
66
+
67
+ textView.setText(s);
68
+
69
+
70
+
71
+ }
72
+
73
+ }
74
+
75
+ ```
76
+
31
77
  このコードのxの値を他のクラス(例えばMainActivity)で使うためにcontextを使ってxの値を獲得したいのですが調べても具体的に使われているものを見つけられませんでした。
32
78
 
33
79