java - Android Cannot find views -
for example, have a, b, c , main activity xml a, b, c main java respectively.
i trying put frame layout in main have create fm_a, fm_b , fm_c fragment java linked a, b, c layout xml
since main navigation bar activity, replace frame layout in main fragment, successful.
until tried use button in layout, error saying:
could not find method savesetting(view) in parent or ancestor context android:onclick attribute defined on view class android.support.v7.widget.appcompatbutton id 'btnsave'
with activity alone without accesssing them fragment, can functioned, way of doing mistake?
ref link:https://www.youtube.com/watch?v=ju837bqobfg&spfreload=5
update: code added
setting.java
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_setting); startlinking(); startsharedpreferences(); // check second entry if (sharedpreferences.getstring("name", "") != null) { displayinfo(); btnedit.setvisibility(view.visible); btnsave.setvisibility(view.invisible); btnclear.setvisibility(view.invisible); btngowelcome.setvisibility(view.visible); etxtname.setenabled(false); etxtfood.setenabled(false); spinnerbreed.setenabled(false); } } public void startlinking() { etxtname = (edittext) findviewbyid(r.id.etxtname); etxtfood = (edittext) findviewbyid(r.id.etxtfood); btnsave = (button) findviewbyid(r.id.btnsave); btnclear = (button) findviewbyid(r.id.btnclear); btnedit = (button) findviewbyid(r.id.btnedit); btngowelcome = (button) findviewbyid(r.id.btngowelcome); spinnerbreed = (spinner) findviewbyid(r.id.spinnerbreed); adapter = arrayadapter.createfromresource(this, r.array.breednamearr, android.r.layout.simple_spinner_item); adapter.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); spinnerbreed.setadapter(adapter); } public void startsharedpreferences() { sharedpreferences = preferencemanager.getdefaultsharedpreferences(getapplicationcontext()); editor = sharedpreferences.edit(); } public void savesetting(view view) { string petname, breed, food, userid; if (textutils.isempty(etxtname.gettext().tostring()) == true || spinnerbreed.getcount() == 0 || textutils.isempty(etxtfood.gettext().tostring()) == true) { toast.maketext(this, "please fill info!", toast.length_short).show(); return; } else { editor = sharedpreferences.edit(); editor.putstring("name", etxtname.gettext().tostring()); editor.putstring("food", etxtfood.gettext().tostring()); editor.putint("breedpos", spinnerbreed.getselecteditemposition()); editor.putstring("breed", spinnerbreed.getselecteditem().tostring()); editor.commit(); toast.maketext(this, "save success!", toast.length_short).show(); btnedit.setvisibility(view.visible); btnsave.setvisibility(view.invisible); btnclear.setvisibility(view.invisible); btngowelcome.setvisibility(view.visible); etxtname.setenabled(false); etxtfood.setenabled(false); spinnerbreed.setenabled(false); } }
fm_setting.java (fragment)
public class fm_setting extends fragment { view myview; @nullable @override public view oncreateview(layoutinflater inflater, @nullable viewgroup container, @nullable bundle savedinstancestate) { myview=inflater.inflate(r.layout.activity_setting, container, false); return myview; } }
activity_setting.xml
<button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/save" android:id="@+id/btnsave" android:onclick="savesetting" android:background="@android:color/transparent" android:textcolor="@color/colortitlebar" android:layout_below="@+id/etxtfood" android:layout_centerhorizontal="true"/>
you need corresponding method in main
activity onclick
defined in xml. hard tell no code sample, suspect it's undefined or moved fragment class. onclick
in xml must correspond method in activity.
public void savesetting(view v){ }
alternatively, find view within fragment , set onclicklistener
programmatically, remove onclick
line xml. common practice.
view view = getactivity().findviewbyid(r.id.btnsave); view.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { savesetting(v); } });
Comments
Post a Comment