java - Can't Open Fragment when clicked button -
i have created 2 fragment , layout, when click button on profile fragment should open flat layout, force stop application , not opening second fragment, have included fragment code , layout code..
profilefragment.java
public class profilefragment extends fragment { view rootview; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { rootview = inflater.inflate(r.layout.profile, container, false); button button = (button) rootview.findviewbyid(r.id.button); button.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { onbuttonclicked(v); } }); return rootview; } public void onbuttonclicked(view view) { //do stuff here.. final fragmenttransaction ft = getfragmentmanager().begintransaction(); ft.replace(r.id.button, new flatview(), "newfragmenttag"); ft.commit(); }
profile.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="${packagename}.${activityclass}" android:id="@+id/semester"> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="new button" android:id="@+id/button" android:layout_centervertical="true" android:layout_centerhorizontal="true" />
flatview.java
public class flatview extends fragment { @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { return inflater.inflate(r.layout.flat, container, false); }
flat.xml
<textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancelarge" android:text="second fragement" android:id="@+id/textview3" android:layout_gravity="center_horizontal" />
logcat
06-19 07:57:56.945 26457-26457/com.idealdeveloper.saltechnical e/androidruntime﹕ fatal exception: main process: com.idealdeveloper.saltechnical, pid: 26457 java.lang.classcastexception: android.widget.button cannot cast android.view.viewgroup @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:917) @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1104) @ android.support.v4.app.backstackrecord.run(backstackrecord.java:682) @ android.support.v4.app.fragmentmanagerimpl.execpendingactions(fragmentmanager.java:1460) @ android.support.v4.app.fragmentmanagerimpl$1.run(fragmentmanager.java:440) @ android.os.handler.handlecallback(handler.java:739) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5221) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:899) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:694)
try guess work
<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="${packagename}.${activityclass}" android:id="@+id/semester"> <framelayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="new button" android:id="@+id/fragmentlayout" android:layout_centervertical="true" android:layout_centerhorizontal="true" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="new button" android:id="@+id/button" android:layout_centervertical="true" android:layout_centerhorizontal="true" />
class
public class profilefragment extends fragment { view rootview; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { rootview = inflater.inflate(r.layout.profile, container, false); button button = (button) rootview.findviewbyid(r.id.button); button.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { onbuttonclicked(v); } }); return rootview; } public void onbuttonclicked(view view) { //do stuff here.. final fragmenttransaction ft = getfragmentmanager().begintransaction(); ft.add(r.id.fragmentlayout, new flatview(), "newfragmenttag"); ft.commit(); }
i hope 1 helps you
Comments
Post a Comment