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

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

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

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

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

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

Q&A

解決済

2回答

3522閲覧

imageButton を押して 画面遷移したい

syyyow

総合スコア28

Java

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

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

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

0グッド

0クリップ

投稿2018/06/16 08:36

編集2018/06/16 11:19

Product.xmlで指定しているimageButtonの画像をクリックした際に
新しい画面への遷移を行いたいです。

イメージは
商品写真クリック➡︎その商品の詳細画面へ遷移

必要とされる新しいxmlは作成済みです。
(product_box)としています。

作業①
新しくProductに、こちらのコードを追記いたしました。

java

1Product.java 2 3import android.content.Intent; 4import android.os.Bundle; 5import android.support.annotation.NonNull; 6import android.support.annotation.Nullable; 7import android.support.v4.app.Fragment; 8import android.view.LayoutInflater; 9import android.view.View; 10import android.view.ViewGroup; 11import android.widget.Button; 12import android.widget.ImageButton; 13import android.widget.TextView; 14 15public class Product extends Fragment { 16 17 @Nullable 18 @Override 19 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 20 return inflater.inflate(R.layout.product, container, false); 21 } 22 23ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー 24 25【追記】 26 27 ImageButton imageButton = (ImageButton)findViewById(R.id.imageButton); 28 imageButton.setOnClickListener(new OnClickListener(){ 29 public void onClick(View v){ 30 Intent i = new Intent(Product.this,Product_box.class); 31 startActivity(i); 32 } 33 } 34} 35

商品情報が載っている
product.xmlのコードはこちらです。

java

1 2product.xml 3 4<?xml version="1.0" encoding="utf-8"?> 5<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent" 8 xmlns:tools="http://schemas.android.com/tools" 9 xmlns:app="http://schemas.android.com/apk/res-auto"> 10 11 <ScrollView 12 android:layout_width="wrap_content" 13 android:layout_height="wrap_content" 14 tools:layout_editor_absoluteX="2dp" 15 tools:layout_editor_absoluteY="0dp" 16 tools:ignore="MissingConstraints"> 17 18 <android.support.v7.widget.LinearLayoutCompat 19 android:layout_width="match_parent" 20 android:layout_height="460dp" 21 android:orientation="vertical"> 22 23 <LinearLayout 24 android:layout_width="match_parent" 25 android:layout_height="match_parent" 26 android:layout_weight="1" 27 android:orientation="vertical"> 28 29 <TableRow 30 android:layout_width="match_parent" 31 android:layout_height="match_parent" /> 32 33 <ImageButton 34 android:id="@+id/imageButton" 35 android:layout_width="match_parent" 36 android:layout_height="201dp" 37 android:scaleType="centerCrop" 38 android:background="@drawable/box"/> 39 40 <GridLayout 41 xmlns:android="http://schemas.android.com/apk/res/android" 42 xmlns:tools="http://schemas.android.com/tools" 43 android:layout_width="match_parent" 44 android:layout_height="match_parent" 45 android:columnCount="2" 46 android:rowCount="2"> 47 48 <TableLayout 49 android:layout_width="match_parent" 50 android:layout_height="match_parent"> 51 52 <TableRow 53 android:layout_width="190dp" 54 android:layout_height="150dp"> 55 56 <ImageButton 57 android:id="@+id/imageButton2" 58 android:layout_width="190dp" 59 android:layout_height="150dp" 60 android:scaleType="fitCenter" 61 android:background="#ffffff" 62 android:src="@drawable/satou"/> 63 64 <ImageButton 65 android:id="@+id/imageButton3" 66 android:layout_width="190dp" 67 android:layout_height="150dp" 68 android:scaleType="fitCenter" 69 android:background="#ffffff" 70 android:src="@drawable/su" /> 71 </TableRow> 72 73 <TableRow 74 android:layout_width="match_parent" 75 android:layout_height="match_parent"> 76 77 <ImageButton 78 android:id="@+id/imageButton4" 79 android:layout_width="190dp" 80 android:layout_height="150dp" 81 android:scaleType="fitCenter" 82 android:background="#ffffff" 83 android:src="@drawable/sho_yu" /> 84 85 <ImageButton 86 android:id="@+id/imageButton5" 87 android:layout_width="190dp" 88 android:layout_height="150dp" 89 android:scaleType="fitCenter" 90 android:background="#ffffff" 91 android:src="@drawable/miso" /> 92 </TableRow> 93 94 <TableRow 95 android:layout_width="match_parent" 96 android:layout_height="match_parent"> 97 98 <ImageButton 99 android:id="@+id/imageButton6" 100 android:layout_width="190dp" 101 android:layout_height="150dp" 102 android:scaleType="fitCenter" 103 android:background="#ffffff" 104 android:src="@drawable/oil" /> 105 106 <ImageButton 107 android:id="@+id/imageButton7" 108 android:layout_width="190dp" 109 android:layout_height="150dp" 110 android:scaleType="fitCenter" 111 android:background="#ffffff" 112 android:src="@drawable/to_nyu"/> 113 </TableRow> 114 115 </TableLayout> 116 117 </GridLayout> 118 </LinearLayout> 119 120 </android.support.v7.widget.LinearLayoutCompat> 121 122 </ScrollView> 123 124</RelativeLayout>

現在出ているエラー内容としましては
■Cannot resolve method 'findViewById(int)'
■Cannot resolve symbol 'setOnClickListener'
■new OnClickListener()のOnClickListenerのところで Invalid method declaration;return type required
■(Product.thisProduct_box.class)のところで Cannot resolve constructor 'Intent(jp..myapplication.Product,java.lang.Class<jp..myapplication.product_box>)'

※.___.の中身はパッケージ名なので伏せました。

・次にどのように進んでいけばいいか
・エラーの理由と改善方法

こちらを教えていただきたく思います。
追記もいたしますのでアドバイス宜しくお願い致します。

【追記】

java

1 2product_box.java 3 4import android.content.Intent; 5import android.support.v7.app.AppCompatActivity; 6import android.os.Bundle; 7import android.widget.TextView; 8 9public class Product_box extends AppCompatActivity { 10 11 @Override 12 protected void onCreate(Bundle savedInstanceState) { 13 super.onCreate(savedInstanceState); 14 setContentView(R.layout.activity_product_box); 15 16 Intent intent = getIntent(); 17 String text = intent.getStringExtra("sendText"); 18 TextView textView = (TextView)this.findViewById(R.id.imageButton); 19 textView.setText(text); 20 } 21 22 23} 24

java

1 2product_box.xml 3 4<?xml version="1.0" encoding="utf-8"?> 5<LinearLayout 6 xmlns:android="http://schemas.android.com/apk/res/android" 7 android:layout_width="match_parent" 8 android:layout_height="match_parent"> 9 10 <TextView 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content" 13 android:text="Box野菜" 14 android:textSize="30dp" 15 android:id="@+id/textView"/> 16 17</LinearLayout> 18

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

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

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

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

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

guest

回答2

0

自己解決

java

1product.boxをこの様に変更することで 2解決しましたmm 3 4 5import android.content.Intent; 6import android.support.v7.app.AppCompatActivity; 7import android.os.Bundle; 8 9public class Product_box extends AppCompatActivity { 10 11@Override 12protected void onCreate(Bundle savedInstanceState) { 13super.onCreate(savedInstanceState); 14 15setContentView(R.layout.activity_product_box); 16 17Intent intent = getIntent(); 18} 19}

投稿2018/06/19 06:11

syyyow

総合スコア28

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

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

0

Fragmentの中では、Activityのように直接findViewByIdを呼び出すことはできません。Fragmentに紐づくViewを取得して、それに対しfindViewByIdを呼ぶ感じになります。

また、Intentの部分のエラーは、第1引数がContextを渡すべきところを、Fragmentを渡してしまっているのが原因です。(FragmentはContextのサブクラスではありません)Activityを取得するか、ViewからContextを取得して渡すようにしてください。

以上を考慮して、onCreateViewのところを下のように修正してみてはいかがでしょう?

@Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.product, container, false); ImageButton imageButton = (ImageButton) view.findViewById(R.id.imageButton); // view経由でimageButtonを探す imageButton.setOnClickListener(new OnClickListener(){ public void onClick(View v){ Intent i = new Intent(v.getContext(), Product_box.class); startActivity(i); } }); return view; }

投稿2018/06/16 08:56

編集2018/06/16 08:57
kakajika

総合スコア3131

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

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

syyyow

2018/06/16 11:17

ありがとうございます! 先ほど遷移できたのですが レイアウトが何も写っていない状態でしたので 色々調べてコードを作成しましたが 今度はアプリが落ちるようになってしまいました。。 遷移先(product_box)のコードも追記致しましたので 何かアドバイスいただけますと幸いです、、
kakajika

2018/06/16 23:25

TextView textView = (TextView)this.findViewById(R.id.imageButton); このコードを書いていて何か違和感はなかったでしょうか?idがimageButtonになっていますし、実際にxml内で使われているidとは異なるようです。
syyyow

2018/06/17 06:41

調べましたら、そもそもでTextViewでは必要がないように思えました。 Product➡︎Product_boxへ遷移するのに public void onClick(View v) { Intent i = new Intent(v.getContext(), Product_box.class); startActivity(i); } }); return view; このようにProductではコード変更をしました。 ちなみにこれの前のonCreateViewでは @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.product, container, false); ImageButton imageButton = (ImageButton) view.findViewById(R.id.imageButton); imageButton.setOnClickListener(new View.OnClickListener() { と、コードを記載しています。
kakajika

2018/06/17 14:21

うーん、おっしゃりたいことがよくわかりません。Product_boxという画面を表示しようとしてアプリが落ちる件についての話ではないのですか?
syyyow

2018/06/19 01:56

product.boxをこの様に変更することで 解決しましたmm import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class Product_box extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_product_box); Intent intent = getIntent(); } }
kakajika

2018/06/19 06:08

でしたら、この質問は解決済みにしてください。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問