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

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

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

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

Android Studio

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

Q&A

解決済

1回答

1040閲覧

intentでSubActivity画面からSubActivity2画面に画面遷移したい

numatuka

総合スコア9

Java

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

Android Studio

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

0グッド

0クリップ

投稿2019/12/05 14:04

#1.実現したいこと
intentを使用してMainActivityからSubActivityへ画面遷移した後、もう一度intentを使用してSubActivity2へ画面遷移したいです。

#2.発生している問題
MainActivityとSubActivityでのやり取りは出来ていますが、SubActivityからSubActivity2へ画面遷移するためのボタンが反応していません。

#3.ソースコード

xml

1activity_main.xml 2 3<?xml version="1.0" encoding="utf-8"?> 4<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 5 xmlns:app="http://schemas.android.com/apk/res-auto" 6 xmlns:tools="http://schemas.android.com/tools" 7 android:layout_width="match_parent" 8 android:layout_height="match_parent" 9 tools:context=".MainActivity"> 10 11 <Button 12 android:id="@+id/button3" 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:layout_marginStart="8dp" 16 android:layout_marginEnd="8dp" 17 android:text="Button" 18 app:layout_constraintEnd_toEndOf="parent" 19 app:layout_constraintStart_toStartOf="parent" 20 app:layout_constraintTop_toTopOf="parent" /> 21</android.support.constraint.ConstraintLayout>

java

1MainActivity.java 2 3import android.content.Intent; 4import android.support.v7.app.AppCompatActivity; 5import android.os.Bundle; 6import android.view.View; 7import android.widget.Button; 8 9public class MainActivity extends AppCompatActivity { 10 11 @Override 12 protected void onCreate(Bundle savedInstanceState) { 13 super.onCreate(savedInstanceState); 14 setContentView(R.layout.activity_main); 15 Button sendButton = findViewById(R.id.button3); 16 sendButton.setOnClickListener(new View.OnClickListener() { 17 @Override 18 public void onClick(View v) { 19 Intent intent = new Intent(getApplication(), sub.class); 20 startActivity(intent); 21 } 22 }); 23 } 24}

xml

1sub.xml 2 3<?xml version="1.0" encoding="utf-8"?> 4<android.support.constraint.ConstraintLayout 5 xmlns:android="http://schemas.android.com/apk/res/android" 6 xmlns:app="http://schemas.android.com/apk/res-auto" 7 xmlns:tools="http://schemas.android.com/tools" 8 android:layout_width="match_parent" 9 android:layout_height="match_parent"> 10 11 <Button 12 android:id="@+id/button2" 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:layout_marginStart="8dp" 16 android:layout_marginEnd="8dp" 17 android:layout_marginBottom="8dp" 18 android:text="Button3" 19 app:layout_constraintBottom_toBottomOf="parent" 20 app:layout_constraintEnd_toEndOf="parent" 21 app:layout_constraintStart_toStartOf="parent" 22 app:layout_constraintTop_toTopOf="parent" /> 23</android.support.constraint.ConstraintLayout>

java

1sub.java 2 3import android.content.Intent; 4import android.os.Bundle; 5import android.support.v7.app.AppCompatActivity; 6import android.view.View; 7import android.widget.Button; 8 9public class sub extends AppCompatActivity { 10 @Override 11 protected void onCreate(Bundle savedInstanceState) { 12 super.onCreate(savedInstanceState); 13 14 setContentView(R.layout.sub); 15 16 Button returnButton = findViewById(R.id.button); 17 returnButton.setOnClickListener(new View.OnClickListener() { 18 @Override 19 public void onClick(View v) { 20 finish(); 21 } 22 }); 23 Button botan = findViewById(R.id.button); 24 botan.setOnClickListener(new View.OnClickListener() { 25 @Override 26 public void onClick(View v) { 27 Intent intent = new Intent(getApplication(), sub2.class); 28 startActivity(intent); 29 } 30 }); 31 } 32}

xml

1<android.support.constraint.ConstraintLayout 2 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 8 <Button 9 android:id="@+id/button2" 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 android:layout_marginStart="8dp" 13 android:layout_marginEnd="8dp" 14 android:layout_marginBottom="8dp" 15 android:text="Button3" 16 app:layout_constraintBottom_toBottomOf="parent" 17 app:layout_constraintEnd_toEndOf="parent" 18 app:layout_constraintStart_toStartOf="parent" 19 app:layout_constraintTop_toTopOf="parent" /> 20</android.support.constraint.ConstraintLayout>

java

1sub2.java 2 3import android.os.Bundle; 4import android.support.v7.app.AppCompatActivity; 5import android.view.View; 6import android.widget.Button; 7 8public class sub2 extends AppCompatActivity { 9 @Override 10 protected void onCreate(Bundle savedInstanceState) { 11 super.onCreate(savedInstanceState); 12 13 setContentView(R.layout.sub2); 14 15 Button returnButton = findViewById(R.id.button2); 16 returnButton.setOnClickListener(new View.OnClickListener() { 17 @Override 18 public void onClick(View v) { 19 finish(); 20 } 21 }); 22 } 23}

#自分で調べたこと
Intentの仕組みがよくわかっておらず、検索して画面遷移について調べようとしましたがMainActivityからSubActivityへの遷移は見つかりますがSubActivityからSubActivity2への遷移が見つかりませんでした。もしその事について記述してあるページがあれば教えていただけると幸いです。

#環境
Windows10
AndroidStudio3.4.1

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

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

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

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

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

guest

回答1

0

ベストアンサー

sub.xmlでは

xml

1 <Button 2 android:id="@+id/button2"

のようにボタンにbutton2というIDを付与しているのに、sub.javaでは

java

1 Button botan = findViewById(R.id.button);

のようにbuttonというIDのオブジェクトを得ようとしています。sub.xmlにはbuttonというIDのボタンは配置されていませんから、押下しても反応することはないでしょう。これをR.id.button2に修正する必要があるのではないでしょうか。

あと、subには「MainActivityに戻るボタン」と「sub2に遷移するボタン」の2つ必要なのではありませんか?なぜ1つで済ます設計にしているのか、その辺の意図がよくわかりません。

投稿2019/12/05 14:18

編集2019/12/05 14:43
keicha_hrs

総合スコア6768

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

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

numatuka

2019/12/05 15:02

回答ありがとうございます。確認し直してbotanのidを取得した所、無事画面遷移に成功しました。 細かい部分を気を付けるようにしたいと思います、ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.47%

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

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

質問する

関連した質問