java - Cannot add function "OnClickListener" to ImageButton -
here java code:
public class mainpage extends appcompatactivity implements navigationview.onnavigationitemselectedlistener { tablayout tablayout; viewpageadapter viewpageadapter; viewpager viewpager; imagebutton pfp; bitmap bitmap_one; private string upload_url = "http://.php"; private int pick_image_request = 1; private string key_image = "image"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main_page); inittypeface(); //id's pfp = (imagebutton) findviewbyid(r.id.imagebutton_one); pfp.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if (v == pfp){ showfilechooser(); } } }); //searchintent intent searchi = getintent(); if (intent.action_search.equals(searchi.getaction())) { string query = searchi.getstringextra(searchmanager.query); toast.maketext(mainpage.this, query, toast.length_short).show(); } //toolbar toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); getsupportactionbar().settitle(null); //tabs tablayout = (tablayout) findviewbyid(r.id.tablayout_two); viewpager = (viewpager) findviewbyid(r.id.viewpager_two); viewpageadapter = new viewpageadapter(getsupportfragmentmanager()); viewpageadapter.addfragments(new feedfragment(), "feed"); viewpageadapter.addfragments(new messagesfragment(), "messages"); viewpageadapter.addfragments(new notificationsfragment(), "notifications"); viewpager.setadapter(viewpageadapter); tablayout.setupwithviewpager(viewpager); //floatingactionbutton floatingactionbutton fab = (floatingactionbutton) findviewbyid(r.id.fab); fab.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { } }); //navigationdrawer drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout); actionbardrawertoggle toggle = new actionbardrawertoggle( this, drawer, toolbar, r.string.navigation_drawer_open, r.string.navigation_drawer_close); drawer.setdrawerlistener(toggle); toggle.syncstate(); navigationview navigationview = (navigationview) findviewbyid(r.id.nav_view); navigationview.setnavigationitemselectedlistener(this); } @override public void onbackpressed() { drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout); if (drawer.isdraweropen(gravitycompat.start)) { drawer.closedrawer(gravitycompat.start); } else { super.onbackpressed(); } } @override public boolean onnavigationitemselected(menuitem item) { int id = item.getitemid(); if (id == r.id.profile) { intent = new intent(this, profile.class); startactivity(i); } else if (id == r.id.whatshot) { intent = new intent(this, whatshot.class); startactivity(i); } else if (id == r.id.trending) { intent = new intent(this, trending.class); startactivity(i); } else if (id == r.id.radioplayer) { intent = new intent(this, radio.class); startactivity(i); } else if (id == r.id.musicplayer) { intent = new intent(this, musicplayer.class); startactivity(i); } else if (id == r.id.settings) { intent = new intent(this, settings.class); startactivity(i); } else if (id == r.id.info) { intent = new intent(this, info.class); startactivity(i); } drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout); drawer.closedrawer(gravitycompat.start); return true; } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.main_menu, menu); searchview searchview = (searchview) menu.finditem(r.id.search_view).getactionview(); searchmanager searchmanager = (searchmanager) getsystemservice(search_service); searchview.setsearchableinfo(searchmanager.getsearchableinfo(getcomponentname())); return super.oncreateoptionsmenu(menu); } //imageinfo private void showfilechooser() { intent intent = new intent(); intent.settype("image/*"); intent.setaction(intent.action_get_content); startactivityforresult(intent.createchooser(intent, "select picture"), pick_image_request); } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if (requestcode == pick_image_request && resultcode == result_ok && data != null && data.getdata() != null) { uri filepath = data.getdata(); try { bitmap_one = mediastore.images.media.getbitmap(getcontentresolver(), filepath); pfp.setimagebitmap(bitmap_one); } catch (ioexception e) { e.printstacktrace(); } } } //typeface private void inittypeface() { typeface mytypeface = typeface.createfromasset(getassets(), "fonts/amble-regular.ttf"); textview text = (textview) findviewbyid(r.id.toolbartitle); text.settypeface(mytypeface); } }
here xml code:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="@dimen/nav_header_height" android:background="@drawable/side_nav_bar" android:gravity="bottom" android:orientation="vertical" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:theme="@style/themeoverlay.appcompat.dark"> <imagebutton android:id="@+id/imagebutton_one" android:layout_width="90dp" android:background="#2a363b" android:onclick="pfpclick" android:layout_height="90dp" android:paddingtop="@dimen/nav_header_vertical_spacing" /> <textview android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingtop="@dimen/nav_header_vertical_spacing" android:text="" android:textappearance="@style/textappearance.appcompat.body1" /> <textview android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" /> </linearlayout>
what trying accomplish onclick of imagebutton, can choose file , add it. keep getting error:
attempt invoke virtual method 'void android.widget.imagebutton.setonclicklistener(android.view.view$onclicklistener)' on null object reference
p.s imagebutton in navigation drawer.
you should either this:
android:onclick="pfpclick"
but need define method in class name pfpclick, , not there
or this
pfp.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if (v == pfp){ showfilechooser(); } } });
both @ same time bad practice , second option not necessary:
if (v == pfp){
because listener implementing anonymous
i suggest remove in layout:
android:onclick="pfpclick"
and clean/modify listener snippet:
pfp.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { showfilechooser(); } });
Comments
Post a Comment