android - Snackbar is not working within fragment class -
i trying show snackbar view when click on button shows force close error when click on button
i have define fragment class below , error log.
i have fragment class:
public class homefragment extends fragment { @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_home, container, false); context = getactivity(); initui(rootview); return rootview; } private void initui(view view) { btn= (button) view.findviewbyid(r.id.btnpress); btn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { snackbar.make(getview(),"snackbar test" , snackbar.length_long).show(); } }); }
fragment_home.xml:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:id="@+id/mainrl" android:layout_height="match_parent"> <linearlayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_centerhorizontal="true" android:orientation="horizontal" android:paddingbottom="40dp" > <button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="please press" /> </linearlayout> </relativelayout>
when press on button not showing snackbar
. gives below error log:
java.lang.noclassdeffounderror: android.support.design.widget.coordinatorlayout @ android.support.design.widget.snackbar.findsuitableparent(snackbar.java:237) @ android.support.design.widget.snackbar.make(snackbar.java:206) @ com.example.test.fragment.homefragment$3.onleftcardexit(homefragment.java:168) @ android.view.viewpropertyanimator$animatoreventlistener.onanimationend(viewpropertyanimator.java:1030) @ android.animation.valueanimator.endanimation(valueanimator.java:1056) @ android.animation.valueanimator.access$400(valueanimator.java:50) @ android.animation.valueanimator$animationhandler.doanimationframe(valueanimator.java:644) @ android.animation.valueanimator$animationhandler.run(valueanimator.java:660) @ android.view.choreographer$callbackrecord.run(choreographer.java:788) @ android.view.choreographer.docallbacks(choreographer.java:591) @ android.view.choreographer.doframe(choreographer.java:559) @ android.view.choreographer$framedisplayeventreceiver.run(choreographer.java:774) @ android.os.handler.handlecallback(handler.java:808) @ android.os.handler.dispatchmessage(handler.java:103) @ android.os.looper.loop(looper.java:193) @ android.app.activitythread.main(activitythread.java:5333) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:824) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:640) @ dalvik.system.nativestart.main(native method)
how can solved error?
i have solved this:
it fine if not include coordinatedlayout
fragment_home.xml
solution:
defined : private relativelayout mroot;
now initialize in initui(view view)
mroot = (relativelayout) view.findviewbyid(r.id.mainrl);
and on button click event put following code:
snackbar.make(mroot, "had snack @ snackbar", snackbar.length_long).show();
now main thing is:
just change current theme theme.appcompat.light.noactionbar
it done.!!!
Comments
Post a Comment