片段中的Android Snackbar NullPointerException

时间:2015-06-01 09:13:07

标签: android snackbar

我想在我的应用程序中添加一个基本的小吃吧,但我收到一个错误,我无法弄清楚原因。

我在片段中的onCreateView()方法中添加了此代码。

Snackbar.make(view, "Snackbar", Snackbar.LENGTH_LONG)
            .setAction("Action", null).show();

并收到此错误:

06-01 10:26:09.955    1232-1232/? E/ActivityThread﹕ Failed to find provider  info for me.muraterdogan.watchme.MetricaContentProvider
06-01 10:26:10.075    1232-1447/? E/ActivityThread﹕ Failed to find provider info for me.muraterdogan.watchme.YPLContentProvider
06-01 10:33:55.044    6646-6646/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: me.muraterdogan.watchme, PID: 6646
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.view.ViewGroup.getContext()' on a null object reference
        at android.support.design.widget.Snackbar.<init>(Snackbar.java:116)
        at android.support.design.widget.Snackbar.make(Snackbar.java:140)
        at me.muraterdogan.watchme.fragments.TrendingFragment.GetData(TrendingFragment.java:90)
        at me.muraterdogan.watchme.fragments.TrendingFragment.onCreateView(TrendingFragment.java:83)
...

5 个答案:

答案 0 :(得分:13)

1.将其移至onActivityCreated方法。

2.在make方法中使用getView(),如下所示:

Snackbar.make(getView(), "Snackbar", Snackbar.LENGTH_LONG)
        .setAction("Action", null).show();

答案 1 :(得分:3)

在我正在研究的代码库中,异步方法回调中显示Snackbar,例如网络调用的错误处理程序。完成Snackbar后,Fragment将不再附加,而View返回的getView()没有CoordinatorLayoutFrameLayout Snackbar想要拥有视图的层次结构。

特别是,Snackbar中的私有findSuitableParent()方法遍历层次结构:

private static ViewGroup findSuitableParent(View view) {
    ViewGroup fallback = null;
    do {
        if (view instanceof CoordinatorLayout) {
            // We've found a CoordinatorLayout, use it
            return (ViewGroup) view;
        } else if (view instanceof FrameLayout) {
            if (view.getId() == android.R.id.content) {
                // If we've hit the decor content view, then we didn't find a CoL in the
                // hierarchy, so use it.
                return (ViewGroup) view;
            } else {
                // It's not the content view but we'll use it as our fallback
                fallback = (ViewGroup) view;
            }
        }

        if (view != null) {
            // Else, we will loop and crawl up the view hierarchy and try to find a parent
            final ViewParent parent = view.getParent();
            view = parent instanceof View ? (View) parent : null;
        }
    } while (view != null);

    // If we reach here then we didn't find a CoL or a suitable content view so we'll fallback
    return fallback;
}

此方法的结果将传递给调用Snackbar的{​​{1}}构造函数,如果此方法返回getContext(),则会获得问题中所见的NPE。

有几种方法可以解决这个问题,假设附加片段中nullCoordinatorLayout的快乐情况仍然存在:

  1. 首先检查FrameLayout,确保您的片段未分离。

  2. 我最终做了什么:代码库中isDetached()已经有一个Builder pattern样式包装器,所以我在构建器中分叉了Snackbar并添加了一个在尝试致电findSuitableParent()之前,请检查它是否会返回!= null。这样,Snackbar构建器调用者不必添加任何其他检查。

答案 2 :(得分:2)

通过过程,小吃店搜索视图。

  • 您使用了片段视图
  • 但片段视图本身尚未附加,因为onAttach of the 片段尚未调用。

因此,应用程序本身在运行时并不真正知道您实际上是指片段的视图。

尝试将快餐栏show方法放在片段的“onResume”上。 希望这会有所帮助。

答案 3 :(得分:1)

你为什么要在onCreate中这样做?

此外,请确保您遵循以下几条规则:

  1. 确保您的主Activity继承自AppCompatActivity,因为这是使用支持库所必需的。
  2. 确保您传递的视图确实存在(这就是为什么我问你为什么要在onCreate中执行此操作以及之后为什么不这样做?)

答案 4 :(得分:0)

正如文档所说&#34; Snackbar将尝试查找父视图以从查看的值中保存Snackbar的视图&#34;。

所以你可以传递目前附加的任何视图。