java - Add Images using Spinner in Android Studio -
i'm trying load image in mainactivity based on selected spinner element.
so if spinner consist 2 channel names in layout, , these names stored in array channels = ["skysports", "premierlive"]
i have defined imageview:
imageview logo = (imageview) findviewbyid(r.id.logo);
while creating spinner i'm using channels array , there i'm calling
getlogo(channels[i]);
where channels[i] current selected spinner element.
finally, i'm trying implement image switch switch-case inside getlogo:
public void getlogo(string channel){ switch(channel){ case ("skysports"): logo.setimageresource(r.drawable.skysports); break; case ("premierlive"): logo.setimageresource(r.drawable.premierlive); break; } }
all gives me
java.lang.nullpointerexception: attempt invoke virtual method 'void android.widget.imageview.setimageresource(int)' on null object reference
what shall do? tnx
edit:
activity_main:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:paddingbottom="@dimen/activity_vertical_margin" tools:context=".mainactivity"> <spinner android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/spinnermain" android:paddingleft="30dp" android:paddingright="10dp" android:paddingtop="2dp" android:paddingbottom="2dp" android:clickable="true" android:touchscreenblocksfocus="true" android:layout_alignparenttop="true" android:visibility="visible" android:textsize="@android:dimen/app_icon_size" android:layout_centervertical="true" android:layout_alignparentleft="true" android:layout_alignparentstart="true" <imageview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/logo" android:layout_centerhorizontal="true" android:layout_margintop="20dp" /> </relativelayout>
and mainactivity:
public class mainactivity extends actionbaractivity implements view.onclicklistener { string[] channels; string channel; spinner sp; imageview logo; protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); imageview logo = (imageview) findviewbyid(r.id.logo); sp = (spinner) findviewbyid(r.id.spinnermain); addspinner(); public void addspinner(){ channels = getchannels(); arrayadapter<string> adapter = new arrayadapter<string>(this, r.layout.spinner_item, channels); sp.setadapter(adapter); sp.setonitemselectedlistener(new adapterview.onitemselectedlistener() { public void onitemselected(adapterview<?> parent, view view, int position, long id) { int index = parent.getselecteditemposition(); getlogo(channels[index]); } }); } }
}
i ser in activity private field logo , in oncreate method local variable called logo variable initialized. in oncreate method need initialize private field used in other methods of class, rather have
imageview logo = (imageview) ...
you should have
logo = (imaview) ...
Comments
Post a Comment