リサイクラービューに表示した画像を押すと画像名をToastで表示させたいのですが、 onCreateViewHolderでview.setOnClickListener(ImageClickListener())を追加すると Constructor of inner class ImageClickListener can be called only with receiver of containing class とのエラーが発生してしまいます。 包含クラスのレシーバーでのみ呼び出すことができるとはどういうことなのでしょうか。
MainActivitykt
1import android.os.Bundle 2import android.util.Log 3import android.view.LayoutInflater 4import android.view.View 5import android.view.ViewGroup 6import android.widget.ImageView 7import android.widget.TextView 8import android.widget.Toast 9import androidx.appcompat.app.AppCompatActivity 10import androidx.recyclerview.widget.GridLayoutManager 11import androidx.recyclerview.widget.RecyclerView 12import kotlinx.android.synthetic.main.row.* 13 14 15class MainActivity : AppCompatActivity() { 16 17 lateinit var gAdapter: GazouAdapter 18 lateinit var gGazouList: ArrayList<Gazou> 19 20 override fun onCreate(savedInstanceState: Bundle?) { 21 Log.i("GazouAdapter", "onCreate") 22 super.onCreate(savedInstanceState) 23 setContentView(R.layout.activity_main) 24 25 val map = Gazou("地図", R.drawable.map_2) 26 val money = Gazou("お金", R.drawable.money_2) 27 val noimage = Gazou("ノーイメージ", R.drawable.noimage_2) 28 val refresh = Gazou("リフレッシュ", R.drawable.refresh_2) 29 val time = Gazou("時間", R.drawable.time_2) 30 val train = Gazou("電車", R.drawable.train_2) 31 val img1 = Gazou("画像1",R.drawable.img_1) 32 val map3 = Gazou("画像3",R.drawable.map_3) 33 gGazouList = arrayListOf(map, money, noimage, refresh, time, train, img1, map3) 34 35 val recyclerView = findViewById<RecyclerView>(R.id.lvGazou) 36 37 val layoutManager = GridLayoutManager(this,2) 38 recyclerView.layoutManager = layoutManager 39 40 gAdapter = GazouAdapter(gGazouList) 41 recyclerView.adapter = gAdapter 42 43 } 44 private inner class ImageClickListener : View.OnClickListener { 45 override fun onClick(view: View) { 46 Log.i("GazouAdapter", "onClick") 47 val tvGazouName = view.findViewById<TextView>(R.id.tvGazouName) 48 49 val gazouName = tvGazouName.text.toString() 50 Toast.makeText(applicationContext, gazouName, Toast.LENGTH_SHORT).show() 51 } 52 } 53 class GazouAdapter(private val gazouList: ArrayList<Gazou>): RecyclerView.Adapter<GazouAdapter.ViewHolder>() { 54 class ViewHolder(view: View) : RecyclerView.ViewHolder(view) { 55 56 val lvGazouView: ImageView = view.findViewById(R.id.lvGazouView) 57 val tvGazouName: TextView = view.findViewById(R.id.tvGazouName) 58 } 59 override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): ViewHolder { 60 val view = LayoutInflater.from(viewGroup.context).inflate(R.layout.row, viewGroup, false) 61 Log.i("GazouAdapter", "onCreateViewHolder") 62 //view.setOnClickListener(ItemClickListener()) 63 64 view.setOnClickListener(ImageClickListener()) 65 66 return ViewHolder(view) 67 } 68 override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) { 69 Log.i("GazouAdapter", "onBindViewHolder") 70 val gazou = gazouList[position] 71 72 viewHolder.lvGazouView.setImageResource(gazou.imageId) 73 viewHolder.tvGazouName.text = gazou.name 74 } 75 override fun getItemCount() = gazouList.size 76 } 77}
rowxml
1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 android:id="@+id/container" 5 android:orientation="vertical" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent"> 8 9 <ImageButton 10 android:id="@+id/lvGazouView" 11 android:layout_width="match_parent" 12 android:layout_height="wrap_content" 13 /> 14 15 <TextView 16 android:id="@+id/tvGazouName" 17 android:layout_width="match_parent" 18 android:layout_height="wrap_content" 19 android:layout_marginLeft="10dp" 20 android:layout_marginTop="10dp" 21 android:textSize="18sp" /> 22 23 24 25</LinearLayout>
activitymainxml
1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:app="http://schemas.android.com/apk/res-auto" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent"> 7 8 9 10 <androidx.recyclerview.widget.RecyclerView 11 android:id="@+id/lvGazou" 12 android:scrollbars="vertical" 13 android:layout_width="match_parent" 14 android:layout_height="match_parent" 15 /> 16 17</LinearLayout>
E/AndroidRuntime: FATAL EXCEPTION: main Process: jp.co.stv_tech.test2112, PID: 623 java.lang.RuntimeException: Unable to start activity ComponentInfo{jp.co.stv_tech.test2112/jp.co.stv_tech.test2112.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6669) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at jp.co.stv_tech.test2112.MainActivity.onCreate(MainActivity.kt:44) at android.app.Activity.performCreate(Activity.java:7136) at android.app.Activity.performCreate(Activity.java:7127) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6669) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。