Android card view error on touching Card view -


i want toast message on touching each card card name:

enter image description here

xml code on android

<relativelayout             android:layout_width="match_parent"             android:layout_height="match_parent">              <imageview                 android:id="@+id/thumbnail"                 android:layout_width="match_parent"                 android:layout_height="@dimen/album_cover_height"                 android:background="?attr/selectableitembackgroundborderless"                 android:clickable="true"                 android:onclick="clikimage"                 android:scaletype="fitxy" /> 

java code

public void clikimage(view view){            toast.maketext(mainactivity.this, "cliked", toast.length_short).show();     } 

in code same toast coming each card..

the texts came code .

.

/**      * adding few albums testing      */     private void preparealbums() {         int[] covers = new int[]{                 r.drawable.album1,                 r.drawable.album2,                 r.drawable.album3,                 r.drawable.album4,                 r.drawable.album5,                 r.drawable.album6,                 r.drawable.album7,                 r.drawable.album8,                 r.drawable.album9,                 r.drawable.album10,                 r.drawable.album11};          album = new album("true romance", 13, covers[0]);         albumlist.add(a);          = new album("xscpae", 8, covers[1]);         albumlist.add(a);          = new album("maroon 5", 11, covers[2]);         albumlist.add(a);          = new album("born die", 12, covers[3]);         albumlist.add(a);          = new album("honeymoon", 14, covers[4]);         albumlist.add(a);          = new album("i need doctor", 1, covers[5]);         albumlist.add(a);          = new album("loud", 11, covers[6]);         albumlist.add(a);          = new album("legend", 14, covers[7]);         albumlist.add(a);          = new album("hello", 11, covers[8]);         albumlist.add(a);          = new album("greatest hits", 17, covers[9]);         albumlist.add(a);            adapter.notifydatasetchanged();     } 

all cards entered manually. want set more cards , put in loop. please .

xml title code.

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"         xmlns:card_view="http://schemas.android.com/apk/res-auto"         android:layout_width="match_parent"         android:layout_height="wrap_content">      <android.support.v7.widget.cardview         android:id="@+id/card_view"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:layout_gravity="center"         android:layout_margin="@dimen/card_margin"         android:elevation="3dp"         card_view:cardcornerradius="@dimen/card_album_radius">          <relativelayout             android:layout_width="match_parent"             android:layout_height="match_parent">              <imageview                 android:id="@+id/thumbnail"                 android:layout_width="match_parent"                 android:layout_height="@dimen/album_cover_height"                 android:background="?attr/selectableitembackgroundborderless"                 android:clickable="true"                 android:onclick="clikimage"                 android:scaletype="fitxy" />              <textview                 android:id="@+id/title"                 android:layout_width="match_parent"                 android:layout_height="wrap_content"                 android:layout_below="@id/thumbnail"                 android:paddingleft="@dimen/album_title_padding"                 android:paddingright="@dimen/album_title_padding"                 android:paddingtop="@dimen/album_title_padding"                 android:textcolor="@color/album_title"                 android:textsize="@dimen/album_title" />              <textview                 android:id="@+id/count"                 android:layout_width="match_parent"                 android:layout_height="wrap_content"                 android:layout_below="@id/title"                 android:paddingbottom="@dimen/songs_count_padding_bottom"                 android:paddingleft="@dimen/album_title_padding"                 android:paddingright="@dimen/album_title_padding"                 android:textsize="@dimen/songs_count" />              <imageview                 android:id="@+id/overflow"                 android:layout_width="@dimen/ic_album_overflow_width"                 android:layout_height="@dimen/ic_album_overflow_height"                 android:layout_alignparentright="true"                 android:layout_below="@id/thumbnail"                 android:layout_margintop="@dimen/ic_album_overflow_margin_top"                 android:scaletype="centercrop"                 android:src="@drawable/ic_dots" />          </relativelayout>      </android.support.v7.widget.cardview>  </linearlayout> 

error when pasted imageview listner code..

08-09 11:35:07.453 3079-3079/com.example.jithin.myapplication e/androidruntime: fatal exception: main                                                                                 process: com.example.jithin.myapplication, pid: 3079                                                                                 java.lang.runtimeexception: unable start activity componentinfo{com.example.jithin.myapplication/com.example.jithin.myapplication.mainactivity}: java.lang.nullpointerexception: attempt invoke virtual method 'void android.widget.imageview.setonclicklistener(android.view.view$onclicklistener)' on null object reference                                                                                     @ android.app.activitythread.performlaunchactivity(activitythread.java:2416)                                                                                     @ android.app.activitythread.handlelaunchactivity(activitythread.java:2476)                                                                                     @ android.app.activitythread.-wrap11(activitythread.java)                                                                                     @ android.app.activitythread$h.handlemessage(activitythread.java:1344)                                                                                     @ android.os.handler.dispatchmessage(handler.java:102)                                                                                     @ android.os.looper.loop(looper.java:148)                                                                                     @ android.app.activitythread.main(activitythread.java:5417)                                                                                     @ java.lang.reflect.method.invoke(native method)                                                                                     @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:726)                                                                                     @ com.android.internal.os.zygoteinit.main(zygoteinit.java:616)                                                                                  caused by: java.lang.nullpointerexception: attempt invoke virtual method 'void android.widget.imageview.setonclicklistener(android.view.view$onclicklistener)' on null object reference                                                                                     @ com.example.jithin.myapplication.mainactivity.oncreate(mainactivity.java:113)                                                                                     @ android.app.activity.performcreate(activity.java:6237)                                                                                     @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1107)                                                                                     @ android.app.activitythread.performlaunchactivity(activitythread.java:2369)                                                                                     @ android.app.activitythread.handlelaunchactivity(activitythread.java:2476)                                                                                      @ android.app.activitythread.-wrap11(activitythread.java)                                                                                      @ android.app.activitythread$h.handlemessage(activitythread.java:1344)                                                                                      @ android.os.handler.dispatchmessage(handler.java:102)                                                                                      @ android.os.looper.loop(looper.java:148)                                                                                      @ android.app.activitythread.main(activitythread.java:5417)                                                                                      @ java.lang.reflect.method.invoke(native method)                                                                                      @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:726)                                                                                      @ com.android.internal.os.zygoteinit.main(zygoteinit.java:616)  

if want text toast view, need change "clicked" string inside toast text view. didn't show textview containing title if in same relativelayout imageview, like:

string textfromtextview = ((textview)((relativelayout)view.getparent()).findviewbyid(r.id.title).gettext(); toast.maketext(mainactivity.this, textfromtextview, toast.length_short).show(); 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -