質問するログイン新規登録

質問編集履歴

5

更新

2017/12/14 09:46

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -2,10 +2,16 @@
2
2
 
3
3
  ```java
4
4
  public class Myapplication {
5
+ public static void main(String[] args) {
6
+ try {
5
- public int x = 0;
7
+ public int x = 0;
6
- while(true){
8
+ while(true){
7
- x= x+1;
9
+ x= x+1;
8
- }
10
+ }
11
+  }catch (IOException e) {
12
+ e.printStackTrace();
13
+ }
14
+ }
9
15
  }
10
16
  ```
11
17
 

4

変更

2017/12/14 09:46

投稿

退会済みユーザー
title CHANGED
@@ -1,1 +1,1 @@
1
- 他クラスからMainActivity値の受け渡し
1
+ ループ中変数を共有する
body CHANGED
@@ -1,31 +1,11 @@
1
1
  私はandroidstudio でアプリ開発をしています。
2
2
 
3
3
  ```java
4
- public class Myapplication implements Runnable {
4
+ public class Myapplication {
5
5
  public int x = 0;
6
- Thread thread;
7
-
8
- public void start() {
9
- thread = new Thread(this);
10
- thread.setDaemon(true);
11
- thread.start();
6
+ while(true){
7
+ x= x+1;
12
- }
8
+ }
13
-
14
- public void stop() {
15
- thread.interrupt();
16
- thread = null;
17
- }
18
-
19
- @Override
20
- public void run() {
21
- try {
22
- for (;;) {
23
- Thread.sleep(100);
24
- x++;
25
- }
26
- } catch (InterruptedException e) {
27
- }
28
- }
29
9
  }
30
10
  ```
31
11
 
@@ -50,26 +30,10 @@
50
30
  button.setOnClickListener(new View.OnClickListener() {
51
31
  @Override
52
32
  public void onClick(View view) {
53
- Log.d("debug", "x=" + app.x);
33
+ textView.setText(app.x);
54
34
  }
55
- });
35
+ });
56
- textView.setText(app.x);
57
36
  }
58
-
59
-
60
- @Override
61
- public void onResume() {
62
- super.onResume();
63
- app = new Myapplication();
64
- app.start();
65
- }
66
-
67
- @Override
68
- public void onPause() {
69
- super.onPause();
70
- app.stop();
71
- app = null;
72
- }
73
37
  }
74
38
 
75
39
  ```

3

変更

2017/12/12 09:31

投稿

退会済みユーザー
title CHANGED
@@ -1,1 +1,1 @@
1
- Context の渡し
1
+ 他クラスからMainActivity値の受け渡し
body CHANGED
@@ -53,6 +53,7 @@
53
53
  Log.d("debug", "x=" + app.x);
54
54
  }
55
55
  });
56
+ textView.setText(app.x);
56
57
  }
57
58
 
58
59
 
@@ -72,6 +73,6 @@
72
73
  }
73
74
 
74
75
  ```
75
- このコードのxの値を他のクラス(例えばMainActivity)で使うためにcontextを使ってxの値を獲得したいのですが調べても具体的に使われているものを見つけられませんでした。
76
+ このコードのxの値を他のクラス(例えばMainActivity)でxの値を獲得したいのですが調べても具体的に使われているものを見つけられませんでした。
76
77
 
77
78
  わかる方がいましたらご教授ください

2

更新

2017/12/12 07:08

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -1,13 +1,29 @@
1
1
  私はandroidstudio でアプリ開発をしています。
2
2
 
3
3
  ```java
4
- public class MyApplication{
4
+ public class Myapplication implements Runnable {
5
- int x=0;
5
+ public int x = 0;
6
-
6
+ Thread thread;
7
+
7
- public void test()
8
+ public void start() {
9
+ thread = new Thread(this);
10
+ thread.setDaemon(true);
11
+ thread.start();
8
- {
12
+ }
13
+
14
+ public void stop() {
15
+ thread.interrupt();
16
+ thread = null;
17
+ }
18
+
19
+ @Override
9
- while (true) {
20
+ public void run() {
21
+ try {
10
- x = x + 1;
22
+ for (;;) {
23
+ Thread.sleep(100);
24
+ x++;
25
+ }
26
+ } catch (InterruptedException e) {
11
27
  }
12
28
  }
13
29
  }
@@ -16,25 +32,45 @@
16
32
  ```java
17
33
  import android.support.v7.app.AppCompatActivity;
18
34
  import android.os.Bundle;
35
+ import android.util.Log;
36
+ import android.view.View;
37
+ import android.widget.Button;
19
38
  import android.widget.TextView;
20
39
 
21
40
  public class MainActivity extends AppCompatActivity {
22
41
  Myapplication app;
23
- String s;
24
42
 
25
-
26
43
  @Override
27
44
  protected void onCreate(Bundle savedInstanceState) {
28
45
  super.onCreate(savedInstanceState);
29
46
  setContentView(R.layout.activity_main);
47
+ Button button = findViewById(R.id.button);
30
48
  TextView textView = findViewById(R.id.text_view);
49
+
50
+ button.setOnClickListener(new View.OnClickListener() {
51
+ @Override
52
+ public void onClick(View view) {
53
+ Log.d("debug", "x=" + app.x);
54
+ }
55
+ });
56
+ }
57
+
58
+
59
+ @Override
60
+ public void onResume() {
61
+ super.onResume();
31
62
  app = new Myapplication();
32
- int x = app.x;
33
- s = Integer.toString(x);
34
- textView.setText(s);
63
+ app.start();
64
+ }
35
65
 
66
+ @Override
67
+ public void onPause() {
68
+ super.onPause();
69
+ app.stop();
70
+ app = null;
36
71
  }
37
72
  }
73
+
38
74
  ```
39
75
  このコードのxの値を他のクラス(例えばMainActivity)で使うためにcontextを使ってxの値を獲得したいのですが調べても具体的に使われているものを見つけられませんでした。
40
76
 

1

更新

2017/12/12 07:01

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -13,6 +13,29 @@
13
13
  }
14
14
  ```
15
15
 
16
+ ```java
17
+ import android.support.v7.app.AppCompatActivity;
18
+ import android.os.Bundle;
19
+ import android.widget.TextView;
20
+
21
+ public class MainActivity extends AppCompatActivity {
22
+ Myapplication app;
23
+ String s;
24
+
25
+
26
+ @Override
27
+ protected void onCreate(Bundle savedInstanceState) {
28
+ super.onCreate(savedInstanceState);
29
+ setContentView(R.layout.activity_main);
30
+ TextView textView = findViewById(R.id.text_view);
31
+ app = new Myapplication();
32
+ int x = app.x;
33
+ s = Integer.toString(x);
34
+ textView.setText(s);
35
+
36
+ }
37
+ }
38
+ ```
16
39
  このコードのxの値を他のクラス(例えばMainActivity)で使うためにcontextを使ってxの値を獲得したいのですが調べても具体的に使われているものを見つけられませんでした。
17
40
 
18
41
  わかる方がいましたらご教授ください