前提・実現したいこと
1つのActivityで、2つのレイアウト間を遷移させたいのですが、上手くいきません。
setScreen~()と定義し、移動を試みています。
- 移動ボタン(button19)を押すことでactivity_sub.xmlからactivity_main4.xmlに移動する。
- editTextに入力した値をtextViewにセットし、確定ボタン(button17)を押すと、再びactivity_sub.xmlに戻る。
というシステムを作成したいです。
1の移動は実現できましたが、2の移動を試みようとボタンを押すと、落ちてしまいます。
該当のソースコード
JAVA
1 import android.app.Activity; 2 import android.os.Bundle; 3 import android.text.Editable; 4 import android.view.MotionEvent; 5 import android.view.View; 6 import android.view.ViewGroup; 7 import android.widget.Button; 8 import android.widget.EditText; 9 import android.widget.TextView; 10 11 public class SubActivity extends Activity { 12 private EditText editText; 13 private TextView textView; 14 @Override 15 protected void onCreate(Bundle savedInstanceState) { 16 super.onCreate(savedInstanceState); 17 setScreenSub(); 18 } 19 20 private void setScreenSub() { 21 setContentView(R.layout.activity_sub); 22 Button sendButton = findViewById(R.id.button19); 23 sendButton.setOnClickListener(new View.OnClickListener() { 24 @Override 25 public void onClick(View view) { 26 setScreenMain(); 27 } 28 }); 29 30 private void setScreenMain() { 31 setContentView(R.layout.activity_main4); 32 Button returnButton = findViewById(R.id.button17); 33 returnButton.setOnClickListener(new View.OnClickListener() { 34 @Override 35 public void onClick(View view) { 36 textView = findViewById(R.id.textViewSub1); 37 editText = findViewById(R.id.editText); 38 String text = editText.getText().toString(); 39 textView.setText(text); 40 setScreenSub(); 41 } 42 }); 43 } 44}
試したこと
setScreenMain()内のonClickで
setScreenSub();
だけ宣言したら、画面は元に戻ります。
ですが、入力テキストをtextXiewにセットする部分のコードを入れると、落ちます。
java初心者のため、様々な本やサイトを参考に見ているのですが、解決策が分からず、質問させていただきました。
他にsetScreen~()を使用することなく、同じActivity間でレイアウトを移動させる方法もあるのでしょうか?
何でも良いのでアドバイスの方、よろしくお願いいたします。
補足情報(FW/ツールのバージョンなど)
androidstudio3.0.1
回答1件
あなたの回答
tips
プレビュー