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

質問編集履歴

1

コードの追加

2017/07/26 00:07

投稿

riroholll
riroholll

スコア57

title CHANGED
File without changes
body CHANGED
@@ -54,4 +54,48 @@
54
54
  break;
55
55
  ```
56
56
  checkBox2がcheckされてbuttonを押したときに変数をmainactivityに持ってくるというプログラムを書いているのですが、buttonを押すと落ちてしまいます。
57
- どう直せばよいのでしょうか?
57
+ どう直せばよいのでしょうか?
58
+
59
+ ```ここに言語を入力
60
+ import android.app.Application;
61
+
62
+ public class MyApp extends Application {
63
+
64
+ private String testString = "default";
65
+
66
+ @Override
67
+ public void onCreate() {
68
+
69
+ }
70
+
71
+ public String getTestString() {
72
+ return testString;
73
+ }
74
+ public void setTestString(String str) {
75
+ testString = str;
76
+ }
77
+ }
78
+ ```
79
+
80
+ ```ここに言語を入力
81
+ import android.os.Bundle;
82
+ import android.support.v7.app.AppCompatActivity;
83
+ import android.widget.TextView;
84
+
85
+ public class mainactivity extends AppCompatActivity {
86
+
87
+ @Override
88
+ protected void onCreate(Bundle savedInstanceState) {
89
+ super.onCreate(savedInstanceState);
90
+ setContentView(R.layout.activity_sub);
91
+
92
+ MyApp myApp = (MyApp) this.getApplication();
93
+ String str = myApp.getTestString();
94
+
95
+ TextView TextView2 = (TextView)findViewById(R.id.TextView2);
96
+ TextView2.setText(str);
97
+
98
+
99
+ }
100
+ }
101
+ ```