実現したいこと
activity_main.xmlファイル上の「設定」ボタンを押したとき、users_information.xmlファイルに移動したい。
発生している問題・分からないこと
activity_main.xmlファイルとusers_information.xmlファイルの二つのファイルは出来ていますが、それに適応している2つのjavaファイルのプログラムをどうしたら良いのかがわからない。
該当のソースコード
activity_main.xml
xml
1<?xml version="1.0" encoding="utf-8"?> 2<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:id="@+id/main" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent" 8 tools:context=".MainActivity"> 9 10 <Button 11 android:id="@+id/informationBtn1" 12 android:layout_width="wrap_content" 13 android:layout_height="wrap_content" 14 android:layout_margin="16dp" 15 android:text="@string/informationBtn1" 16 android:textSize="25sp" 17 android:textColor="@color/black" 18 style="@style/Widget.Material3.Button.TextButton.Icon" 19 app:layout_constraintStart_toStartOf="parent" 20 app:layout_constraintTop_toTopOf="parent" /> 21 22 <TextView 23 android:id="@+id/name" 24 android:layout_width="wrap_content" 25 android:layout_height="wrap_content" 26 android:layout_margin="25dp" 27 android:text="@string/name_label" 28 android:textIsSelectable="false" 29 android:textSize="30sp" 30 android:textColor="@color/my_custom_color" 31 android:textStyle="bold" 32 app:layout_constraintEnd_toEndOf="parent" 33 app:layout_constraintTop_toTopOf="parent" /> 34 35 <Button 36 android:id="@+id/tellBtn2" 37 android:layout_width="wrap_content" 38 android:layout_height="wrap_content" 39 android:layout_marginTop="-55dp" 40 android:text="@string/telephoneBtn2" 41 android:textSize="56sp" 42 android:textStyle="bold" 43 app:layout_constraintBottom_toBottomOf="parent" 44 app:layout_constraintEnd_toEndOf="parent" 45 app:layout_constraintStart_toStartOf="parent" 46 app:layout_constraintTop_toBottomOf="@id/name" /> 47 48</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
java
1 2import android.content.Intent; 3import android.os.Bundle; 4import android.view.View; 5import android.widget.Button; 6import android.widget.TextView; 7 8import androidx.activity.EdgeToEdge; 9import androidx.appcompat.app.AppCompatActivity; 10import androidx.core.graphics.Insets; 11import androidx.core.view.ViewCompat; 12import androidx.core.view.WindowInsetsCompat; 13 14public class MainActivity extends AppCompatActivity implements View.OnClickListener { 15 16 private Button informationBtn1, tellBtn2; 17 private TextView name; 18 19 @Override 20 protected void onCreate(Bundle savedInstanceState) { 21 super.onCreate(savedInstanceState); 22 EdgeToEdge.enable(this); 23 setContentView(R.layout.activity_main); 24 ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> { 25 Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()); 26 v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom); 27 return insets; 28 }); 29 30 // idからviewを見つける 31 Button information_btn1 = findViewById(R.id.informationBtn1); 32 String inform = "設定"; 33 information_btn1.setText(inform); 34 information_btn1.setOnClickListener(v -> { 35 Intent intent = new Intent(MainActivity.this, UsersInformation.class); 36 startActivity(intent); 37 }); 38 39 TextView name = findViewById(R.id.name); 40 String label = "名前"; 41 name.setText(label); 42 43 Button tell_btn2 = findViewById(R.id.tellBtn2); 44 String call = "○○"; 45 tell_btn2.setText(call); 46 } 47 48 @Override 49 public void onClick(View v) { 50 // ここには他のクリックイベントの処理を書くことができます 51 } 52} 53
users_information.xml
xml
1 2<?xml version="1.0" encoding="utf-8"?> 3<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 4 android:id="@+id/sub" 5 xmlns:tools="http://schemas.android.com/tools" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent" 8 tools:context=".MainActivity"> 9 10 <LinearLayout 11 android:id="@+id/content_layout" 12 android:layout_width="match_parent" 13 android:layout_height="match_parent" 14 android:layout_above="@+id/btn_withdraw" 15 android:layout_marginBottom="-1dp" 16 android:orientation="vertical" 17 android:padding="16dp"> 18 19 <!-- ご使用者様 --> 20 <TextView 21 android:layout_width="wrap_content" 22 android:layout_height="wrap_content" 23 android:paddingTop="3dp" 24 android:paddingBottom="1dp" 25 android:text="@string/use_label" 26 android:textSize="18sp" /> 27 28 <!-- ご使用者様の入力欄 --> 29 <EditText 30 android:layout_width="match_parent" 31 android:layout_height="wrap_content" 32 android:background="@android:drawable/edit_text" 33 android:hint="名前を持ってくる"/> 34 35 <!-- 電話番号 --> 36 <TextView 37 android:layout_width="wrap_content" 38 android:layout_height="wrap_content" 39 android:paddingTop="3dp" 40 android:paddingBottom="1dp" 41 android:text="@string/phoneNo_label" 42 android:textSize="18sp" /> 43 44 <EditText 45 android:layout_width="match_parent" 46 android:layout_height="wrap_content" 47 android:background="@android:drawable/edit_text" 48 android:hint="電話番号を入力" 49 android:inputType="phone" /> 50 51 <!-- メールアドレス --> 52 <TextView 53 android:layout_width="wrap_content" 54 android:layout_height="wrap_content" 55 android:paddingTop="3dp" 56 android:paddingBottom="1dp" 57 android:text="@string/email_label" 58 android:textSize="18sp" /> 59 60 <EditText 61 android:layout_width="match_parent" 62 android:layout_height="wrap_content" 63 android:background="@android:drawable/edit_text" 64 android:hint="メールアドレスを入力" 65 android:inputType="textEmailAddress" /> 66 67 <!-- ご登録者様メールアドレス --> 68 <TextView 69 android:layout_width="wrap_content" 70 android:layout_height="wrap_content" 71 android:paddingTop="3dp" 72 android:paddingBottom="1dp" 73 android:text="@string/entry_label" 74 android:textSize="18sp" /> 75 76 <EditText 77 android:layout_width="match_parent" 78 android:layout_height="wrap_content" 79 android:background="@android:drawable/edit_text" 80 android:hint="@string/email_hint" 81 android:inputType="textEmailAddress" /> 82 83 <!-- ボタンを含むレイアウト --> 84 <LinearLayout 85 android:layout_width="match_parent" 86 android:layout_height="wrap_content" 87 android:gravity="center" 88 android:orientation="horizontal" 89 android:paddingTop="16dp"> 90 91 <Button 92 android:id="@+id/btn_back" 93 android:layout_width="wrap_content" 94 android:layout_height="wrap_content" 95 android:text="@string/back_label" /> 96 97 <Button 98 android:id="@+id/btn_confirm" 99 android:layout_width="wrap_content" 100 android:layout_height="wrap_content" 101 android:layout_marginStart="16dp" 102 android:text="@string/decide_label" /> 103 </LinearLayout> 104 105 </LinearLayout> 106 107 <Button 108 android:id="@+id/btn_withdraw" 109 android:layout_width="wrap_content" 110 android:layout_height="wrap_content" 111 android:text="@string/leave_label" 112 android:layout_alignParentBottom="true" 113 android:layout_alignParentEnd="true" /> 114 115</RelativeLayout> 116
UsersInformation.java
java
1 2import android.os.Bundle; 3 4import androidx.activity.EdgeToEdge; 5import androidx.appcompat.app.AppCompatActivity; 6import androidx.core.graphics.Insets; 7import androidx.core.view.ViewCompat; 8import androidx.core.view.WindowInsetsCompat; 9 10public class UsersInformation extends AppCompatActivity { 11 12 @Override 13 protected void onCreate(Bundle savedInstanceState) { 14 super.onCreate(savedInstanceState); 15 EdgeToEdge.enable(this); 16 setContentView(R.layout.users_information); 17 ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.sub), (v, insets) -> { 18 Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()); 19 v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom); 20 return insets; 21 }); 22 } 23}
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
ChatGPT等にも聞いたが、現状「設定」ボタンを押すとAndroidのホーム画面に戻ってしまう。
補足
特になし
>AndroidStudio(java)での画面遷移
まず何より、 AndroidStudio と Android アプリの違いをご理解ください。
AndroidStudio は Android アプリを作るためのツールであり、アプリ自体とは違うものです。「 AndroidStudio での画面遷移」というタイトルでは、ツールの動作についての質問かと思われます。お作りになっている Android アプリの画面遷移のことであれば「Android アプリの画面遷移」でなければならないと思います。
また、
>activity_main.xmlファイル上の「設定」ボタンを押したとき、users_information.xmlファイルに移動
というのも、 xml ファイルは画面の定義であってそのまま動作するものではありません。『 xml ファイルから xml ファイルへ移動するモノ』では無く、それぞれの xml ファイルから画面を構成する Activity / Fragment 間において、 "移動" がされます。なので移動の実行が (startXml という名前では無く) startActivity なのです。
xml ベースでは無く Activity / Fragment ベースで「MainActivity で設定ボタンを押したら UsersInformation へ遷移」で OK です。
ついでに、 xml や java を囲っている ``` に付けられている "activity_main.xml" とか "MainActivity.java" とかのところは言語名("xml" とか "java")を書くところです。
ファイル名は ``` の外に書いてください。
新しいプロジェクトを作って全てコピペすると、 UsersInformation.java の 18行目, "R.id.main" に「users_information.xml にそのような定義は無い」と問題があるのですが、どうしたらよいでしょうか。
回答ありがとうございます。
xml や java を囲っている ``` に付けられている "activity_main.xml" とか "MainActivity.java" とかのところは言語名("xml" とか "java")を書くところです。
ファイル名は ``` の外に書いてください。
すみませんが、上の3行はどこの箇所かお聞きしてもよろしいでしょうか?
UsersInformation.java の 18行目, "R.id.main" に「users_information.xml にそのような定義は無い」との問題自分もでています。確実に、users_information.xmlファイルとそれに適応するjavaファイルは作成している状態です。
タイトル修正ありがとうございます。
>上の3行はどこの箇所か
現在の修正履歴で見ますと、質問の 62 行目が
```MainActivity.java
と書かれています。これを
MainActivity.java
```java
の 2 行にしますと、私の回答のように表示されます。修正時もリアルタイムにプレビューが変わり、出来ていれば java のキーワードに色が付きますので、確認しながら弄ってみてください。
>"R.id.main" に「users_information.xml にそのような定義は無い」との問題自分もでています
回答で指摘しています「AndroidManifest.xml に Activity として UsersInformation を登録」は行われていて、かつ R.id.main が無いとなっているのでしたら、こちらが原因です。
このメッセージが言っているのは「users_information.xmlファイルとそれに適応するjavaファイルは作成している」かどうかではありません。
Activity の findViewById メソッドの役割は、自分に設定されている View ツリー(=本件では setContentView で設定されている users_information.xml 内の View 群) 内から指定した id を持つ View を見つけることです。
しかし R.id.main は activity_main.xml のみで定義されていて users_information.xml にはありません。従って findViewById メソッドは絶対に View を見つけることは出来ません。そのことをこのメッセージは言っているのです。
マークダウン編集ありがとうございます。
java もレイアウト xml も色が付いて見易くなりました ^^
ご回答ありがとうございます。
ご説明がわかりやすく、助かりました。
画面を"移動"するなら、 Activity → Activity では無く Activity 内で複数の Fragment を入れ替える形をお勧めします。との、ご説明ありがとうございます。
自分でも色々と調べてみたのですが、お勧めの参考書などありますでしょうか?
>新しいプロジェクトを作って全てコピペすると、 UsersInformation.java の 18行目, "R.id.main" に「users_information.xml にそのような定義は無い」と問題があるのですが、どうしたらよいでしょうか。
これは、users_information.xmlファイルに android:idを作成していなかったため生じていました。
users_information.xmlファイルに android:id="@+id/sub"を作成しJavaファイルの"main"を"sub"に変えてできました。
>お勧めの参考書などありますでしょうか
申し訳ない、私は特定の書籍等は見ていませんで例えば「Android Java Fragment」でヒットした記事を読み散らかす感じから自分なりで使っていますので、どのような書籍がどんな内容で書いてあるかも把握していません。
基本的には今の各画面に当たる Activity を Fragment とし、土台となる Activity のコンテナ View に載せる Fragment を随時 FragmentManager で設定することで入れ替えになります。
>users_information.xmlファイルに android:id="@+id/sub"を作成しJavaファイルの"main"を"sub"に変えてできました。
それも解決方法の一つでしたね。
良かったらそれでご自身で回答を書いてベストアンサーとしてください。
>おすすめの参考書の件
わかりました。ご自分で調べて身に着けていかれているとはお凄いですね!
まだ始めたばかりで色々と分からないことが多く、参考資料のおかげで助かりました。
もっと知見を広げて頑張っていきていきたいと思います。
ありがとうございました。
お疲れさまでした。
好きでやっていれば暇でも「そいえばあの時見つけたあのネット記事をちょっと読んでみるかな」とか自然と(他人からは勉強しているように見える)接する時間が増えますので、いろいろ溜まっていって何とかなるようになりますよ^^
---
たまたま YouTube で良いことを言ってる動画がありましたのでちょっと紹介させて頂きます。
[エンジニアに向いていない人の特徴6選。1年以内に辞めた人の共通点とその後の改善策]
https://www.youtube.com/watch?v=-dGZaiZKBP4
sim_m さんが向いてないかもとかではありませんで、向いていてもいなくても対策のほうは考えるに値しそうで、特に 5 番目 ( 9:18 ~) の「新しいことに挑戦・勉強ができない」に対する対策で「普段の日常から新しいことに挑戦する癖」の例『行ったことのないお店に行ってみる』が、それくらいの感覚でやってみるのが良いんじゃない?ということで「知見を広げて頑張っていきていきたい」というコメントに対して良いアドバイスになるかなと思います。
最後までご親切にありがとうございます。
動画視聴させていただきました。
日々の生活からも意識していきたいと思います。
本当にありがとうございました!
回答1件
あなたの回答
tips
プレビュー

