Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
410 views
in Technique[技术] by (71.8m points)

android - Snackbar is not working within fragment class

I am trying to show snackbar view when I click on button but it shows force close error when I click on button

I have define fragment class below and also 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 I press on button it is not showing snackbar. it gives below error log:

java.lang.NoClassDefFoundError: android.support.design.widget.CoordinatorLayout
            at android.support.design.widget.Snackbar.findSuitableParent(Snackbar.java:237)
            at android.support.design.widget.Snackbar.make(Snackbar.java:206)
            at com.example.test.fragment.HomeFragment$3.onLeftCardExit(HomeFragment.java:168)
            at android.view.ViewPropertyAnimator$AnimatorEventListener.onAnimationEnd(ViewPropertyAnimator.java:1030)
            at android.animation.ValueAnimator.endAnimation(ValueAnimator.java:1056)
            at android.animation.ValueAnimator.access$400(ValueAnimator.java:50)
            at android.animation.ValueAnimator$AnimationHandler.doAnimationFrame(ValueAnimator.java:644)
            at android.animation.ValueAnimator$AnimationHandler.run(ValueAnimator.java:660)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:788)
            at android.view.Choreographer.doCallbacks(Choreographer.java:591)
            at android.view.Choreographer.doFrame(Choreographer.java:559)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:774)
            at android.os.Handler.handleCallback(Handler.java:808)
            at android.os.Handler.dispatchMessage(Handler.java:103)
            at android.os.Looper.loop(Looper.java:193)
            at android.app.ActivityThread.main(ActivityThread.java:5333)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
            at dalvik.system.NativeStart.main(Native Method)

How can I solved this error?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can also use:

getActivity().findViewById(android.R.id.content)

like this:

Snackbar snackBar = Snackbar.make(getActivity().findViewById(android.R.id.content),
           "Look at me, I'm a fancy snackbar", Snackbar.LENGTH_LONG);
snackBar.show();

See this


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...