Snackbar在片段类中不起作用

时间:2015-08-31 07:04:09

标签: android android-fragments android-activity android-design-library android-snackbar

我在点击按钮时尝试显示 snackbar 视图,但是当我点击按钮时显示强制关闭错误

我在下面定义了片段类,还有错误日志。

我有片段类:

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>

当我按下按钮时,它没有显示snackbar。它给出了以下错误日志:

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)

我该如何解决这个错误?

4 个答案:

答案 0 :(得分:64)

您也可以使用:

getActivity().findViewById(android.R.id.content)
像这样:

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

请参阅this

答案 1 :(得分:30)

我已经解决了这个问题:

如果我们CoordinatedLayout

中不包含fragment_home.xml,则可以

解决方案:

定义: private RelativeLayout mRoot;

现在在initUI(View view)

中初始化
mRoot = (RelativeLayout) view.findViewById(R.id.mainrl);

并在按钮点击事件中输入以下代码:

 Snackbar.make(mRoot, "Had a snack at Snackbar", Snackbar.LENGTH_LONG).show();

现在主要的是:

只需将当前主题更改为Theme.AppCompat.Light.NoActionBar

它完成了。!!!

答案 2 :(得分:5)

SnackBar make方法接受一个视图,从该视图开始,它会跟踪层次结构,直到找到合适的布局来显示,如果你有异常这意味着你没有将CoordinatedLayout添加到你的项目中

private static ViewGroup findSuitableParent(View view) {
        ViewGroup fallback = null;

        do {
            if(view instanceof CoordinatorLayout) {
                return (ViewGroup)view;
            }

            if(view instanceof FrameLayout) {
                // android.R.id.content
                if(view.getId() == 16908290) {
                    return (ViewGroup)view;
                }

                fallback = (ViewGroup)view;
            }

            if(view != null) {
                ViewParent parent = view.getParent();
                view = parent instanceof View?(View)parent:null;
            }
        } while(view != null);

        return fallback;
    }

答案 3 :(得分:0)

对于Kotlin语言,您必须使用:

requireActivity().findViewById(R.id.context_id)

例如:

Snackbar.make(
    requireActivity().findViewById(R.id.context_id),
    getString(R.string.something_went_wrong),
    Snackbar.LENGTH_LONG
    ).show()