java - Android app crashes upon replacing fragment: -
i followed tutorial here on how make fragment transactions whenever hit button transact fragment app crashes. code:
public class mainactivity extends activity { public fragmenttransaction transaction = getfragmentmanager().begintransaction(); public fragment loginfragment = new loginfragment(); public fragment mainfragment = new mainfragment(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); if (savedinstancestate == null) { getfragmentmanager().begintransaction().add(r.id.container, loginfragment).commit(); } else { getfragmentmanager().begintransaction().add(r.id.container, mainfragment).commit(); } } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.main, menu); return true; } public void login() { transaction.replace(r.id.container, mainfragment); transaction.addtobackstack(null); transaction.commit(); } public static class loginfragment extends fragment { public loginfragment() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_login, container, false); return rootview; } } public static class mainfragment extends fragment { public mainfragment() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_main, container, false); return rootview; } } }
xml:
<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" tools:context="com.pointlight.kingdomcraft.mainactivity$loginfragment" > <button android:id="@+id/button_submit" android:onclick="login" /> </relativelayout>
so in conclusion submit button on login fragment replaces login fragment main fragment, or should instead crashes.
to narrow down, in xml set button android:onclick= "login"
in turn calls login method inside activity class:
public void login() { transaction.replace(r.id.container, mainfragment); transaction.addtobackstack(null); transaction.commit(); }
what have done wrong, i've been working on fragments transactions 2 days no justice.
logcat:
?:??: w/?(?): --------- beginning of /dev/log/main
?:??: w/?(?): --------- beginning of /dev/log/system
edit:
if debug , hit submit button instead of crashing button gets highlighted blue , app freezes
the signature onclick
handler android:onclick="login"
wrong:
public void login() {
should be
public void login(view v) {
android:onclick
attribute gets translated code looks handler method name using reflection. lookup process requires method signature i.e. parameter types. if signature doesn't match, method cannot found , exception thrown.
Comments
Post a Comment