質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.40%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Q&A

解決済

1回答

420閲覧

Androidアプリ画面の画面遷移について(AndroidStudio(java))

sim_m

総合スコア2

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

0グッド

0クリップ

投稿2024/06/22 08:07

編集2024/06/23 09:29

実現したいこと

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のホーム画面に戻ってしまう。

補足

特になし

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

jimbe

2024/06/22 18:56 編集

>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")を書くところです。 ファイル名は ``` の外に書いてください。
jimbe

2024/06/22 09:27 編集

新しいプロジェクトを作って全てコピペすると、 UsersInformation.java の 18行目, "R.id.main" に「users_information.xml にそのような定義は無い」と問題があるのですが、どうしたらよいでしょうか。
sim_m

2024/06/23 02:56

回答ありがとうございます。 xml や java を囲っている ``` に付けられている "activity_main.xml" とか "MainActivity.java" とかのところは言語名("xml" とか "java")を書くところです。 ファイル名は ``` の外に書いてください。 すみませんが、上の3行はどこの箇所かお聞きしてもよろしいでしょうか? UsersInformation.java の 18行目, "R.id.main" に「users_information.xml にそのような定義は無い」との問題自分もでています。確実に、users_information.xmlファイルとそれに適応するjavaファイルは作成している状態です。
jimbe

2024/06/23 04:20

タイトル修正ありがとうございます。 >上の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 を見つけることは出来ません。そのことをこのメッセージは言っているのです。
jimbe

2024/06/23 06:29

マークダウン編集ありがとうございます。 java もレイアウト xml も色が付いて見易くなりました ^^
sim_m

2024/06/23 06:59

ご回答ありがとうございます。 ご説明がわかりやすく、助かりました。 画面を"移動"するなら、 Activity → Activity では無く Activity 内で複数の Fragment を入れ替える形をお勧めします。との、ご説明ありがとうございます。 自分でも色々と調べてみたのですが、お勧めの参考書などありますでしょうか?
sim_m

2024/06/23 09:26

>新しいプロジェクトを作って全てコピペすると、 UsersInformation.java の 18行目, "R.id.main" に「users_information.xml にそのような定義は無い」と問題があるのですが、どうしたらよいでしょうか。 これは、users_information.xmlファイルに android:idを作成していなかったため生じていました。 users_information.xmlファイルに android:id="@+id/sub"を作成しJavaファイルの"main"を"sub"に変えてできました。
jimbe

2024/06/23 10:18

>お勧めの参考書などありますでしょうか 申し訳ない、私は特定の書籍等は見ていませんで例えば「Android Java Fragment」でヒットした記事を読み散らかす感じから自分なりで使っていますので、どのような書籍がどんな内容で書いてあるかも把握していません。 基本的には今の各画面に当たる Activity を Fragment とし、土台となる Activity のコンテナ View に載せる Fragment を随時 FragmentManager で設定することで入れ替えになります。 >users_information.xmlファイルに android:id="@+id/sub"を作成しJavaファイルの"main"を"sub"に変えてできました。 それも解決方法の一つでしたね。 良かったらそれでご自身で回答を書いてベストアンサーとしてください。
sim_m

2024/06/23 12:32

>おすすめの参考書の件 わかりました。ご自分で調べて身に着けていかれているとはお凄いですね! まだ始めたばかりで色々と分からないことが多く、参考資料のおかげで助かりました。 もっと知見を広げて頑張っていきていきたいと思います。 ありがとうございました。
jimbe

2024/06/23 18:00 編集

お疲れさまでした。 好きでやっていれば暇でも「そいえばあの時見つけたあのネット記事をちょっと読んでみるかな」とか自然と(他人からは勉強しているように見える)接する時間が増えますので、いろいろ溜まっていって何とかなるようになりますよ^^ --- たまたま YouTube で良いことを言ってる動画がありましたのでちょっと紹介させて頂きます。 [エンジニアに向いていない人の特徴6選。1年以内に辞めた人の共通点とその後の改善策] https://www.youtube.com/watch?v=-dGZaiZKBP4 sim_m さんが向いてないかもとかではありませんで、向いていてもいなくても対策のほうは考えるに値しそうで、特に 5 番目 ( 9:18 ~) の「新しいことに挑戦・勉強ができない」に対する対策で「普段の日常から新しいことに挑戦する癖」の例『行ったことのないお店に行ってみる』が、それくらいの感覚でやってみるのが良いんじゃない?ということで「知見を広げて頑張っていきていきたい」というコメントに対して良いアドバイスになるかなと思います。
sim_m

2024/06/23 16:52

最後までご親切にありがとうございます。 動画視聴させていただきました。 日々の生活からも意識していきたいと思います。 本当にありがとうございました!
guest

回答1

0

ベストアンサー

質問へのコメントより:

"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 を見つけることは出来ません。そのことをこのメッセージは言っているのです。

ViewCompat.setOnApplyWindowInsetsListener が何の為にあるのか、そのパラメータに何を指定しなければならないのかをご理解されておらずただコピペしてきただけなら、ちゃんと理解されてから利用するか、今は利用を諦めて ('EdgeToEdge.enable(this)' の行共々) 削除したほうが良いと思います。
また画面を"移動"するなら、 Activity → Activity では無く Activity 内で複数の Fragment を入れ替える形をお勧めします。勉強するものが増えるのは大変ですが、 Activity 間で"移動"を実装するのは大抵は後々色々面倒なことになると予想します。


Androidのホーム画面に戻ってしまう

アプリが落ちているものと思われます。
AndroidManifest.xml に Activity として UsersInformation を登録しているでしょうか。

とりあえず画面遷移に関係無いものをとっぱらってシンプルにしてみましょう。
MainActivity(activity_main.xml) settingsButton 押下 → SettingsActivity(activity_settings.xml)

AndroidManifest.xml (一部: application タグ内)

xml

1 <activity 2 android:name=".MainActivity" 3 android:exported="true"> 4 <intent-filter> 5 <action android:name="android.intent.action.MAIN" /> 6 <category android:name="android.intent.category.LAUNCHER" /> 7 </intent-filter> 8 </activity> 9 <activity 10 android:name=".SettingsActivity" 11 android:exported="false"> 12 </activity>

MainActivity.java

java

1import android.content.Intent; 2import android.os.Bundle; 3import android.widget.Button; 4 5import androidx.appcompat.app.AppCompatActivity; 6 7public class MainActivity extends AppCompatActivity { 8 @Override 9 protected void onCreate(Bundle savedInstanceState) { 10 super.onCreate(savedInstanceState); 11 setContentView(R.layout.activity_main); 12 13 Button settingsButton = findViewById(R.id.settings_button); 14 settingsButton.setOnClickListener(v -> { 15 Intent intent = new Intent(this, SettingsActivity.class); 16 startActivity(intent); 17 }); 18 } 19}

acticity_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:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context=".MainActivity"> 8 9 <Button 10 android:id="@+id/settings_button" 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content" 13 android:text="設定へ" 14 app:layout_constraintBottom_toBottomOf="parent" 15 app:layout_constraintEnd_toEndOf="parent" 16 app:layout_constraintStart_toStartOf="parent" 17 app:layout_constraintTop_toTopOf="parent" /> 18 19</androidx.constraintlayout.widget.ConstraintLayout>

SettingsActivity.java

java

1import android.os.Bundle; 2 3import androidx.appcompat.app.AppCompatActivity; 4 5public class SettingsActivity extends AppCompatActivity { 6 @Override 7 protected void onCreate(Bundle savedInstanceState) { 8 super.onCreate(savedInstanceState); 9 setContentView(R.layout.activity_settings); 10 } 11}

activity_settings.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:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context=".SettingsActivity"> 8 9 <TextView 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 android:text="設定画面" 13 android:textSize="30dp" 14 app:layout_constraintBottom_toBottomOf="parent" 15 app:layout_constraintEnd_toEndOf="parent" 16 app:layout_constraintStart_toStartOf="parent" 17 app:layout_constraintTop_toTopOf="parent" /> 18 19</androidx.constraintlayout.widget.ConstraintLayout>

実行時
実行時スクリーンショット
「設定へ」ボタン押下時
設定へボタン押下時スクリーンショット

投稿2024/06/22 09:58

編集2024/06/23 04:45
jimbe

総合スコア13045

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

sim_m

2024/06/23 03:00

新しいプロジェクトを作成して、参考のプログラムを実行させていただきました。 質問させていただいた内容と同じように、SettingsActivity.javaファイルとactivity_settings.xmlファイルを作成したら実行することができました。 参考プログラムを見て、修正箇所を探してみます。
sim_m

2024/06/23 09:01

一旦、資料を参考にしてAndroidアプリ画面上の遷移を行うことはできまし。 ありがとうございます。 しかし、画面を"移動"するなら、 Activity → Activity では無く Activity 内で複数の Fragment を入れ替える形というのがわからないので勉強したいと思います。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.40%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問