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
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。