【问题标题】:Android : snackbar not working in fragment classAndroid:小吃栏在片段类中不起作用
【发布时间】:2016-04-12 21:21:46
【问题描述】:

我想在我的应用程序中使用snackbar,所以我为此设置了以下代码

public class FragmentAddProperty extends Fragment
{
   RelativeLayout mRelative 
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)  {
        view = inflater.inflate(R.layout.layout_1,
                container, false);
    mRelative = (RelativeLayout) view.findViewById(R.id.edt_pro_city);
    Snackbar.make(mRelative, "No images.", Snackbar.LENGTH_LONG).show();
    }

}

编辑

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/edt_pro_city"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f8f8f8"
android:orientation="vertical" >



        <ImageView
            android:id="@+id/sample_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:src="@drawable/icon_list"
            android:visibility="visible" />
            </RelativeLayout>

结果出现以下错误

java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.support.design.widget.Snackbar$SnackbarLayout

我该如何解决这个问题?

所有的建议都是可以理解的

【问题讨论】:

  • Snackbar.make(view, "No images.", Snackbar.LENGTH_LONG).show();
  • 请输入您的xml代码。
  • 在 IDE 上尝试 build -&gt; clean 并再次运行它

标签: android android-snackbar snackbar


【解决方案1】:

你可以在Frgamnet中使用snackbar。只需使用ViewGroup的对象。

 public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) 
 {
  Snackbar.make(container, "Please Check your Internet Connection",Snackbar.LENGTH_LONG).show(); 
 }

【讨论】:

  • 在与 ViewPager2 和 TabLayout 一起使用的 Fragment 中对我不起作用
  • @Sam 嗨!你知道如何在 viewPager2 的片段中创建 Snackbar 吗?我也有同样的烦恼
【解决方案2】:

没有CordinatorLayout,Snackbar 将无法工作,您需要像这样创建您的fragment_list.xml,否则Snackbar snackbar = Snackbar.make(view, R.string.action, Snackbar.LENGTH_LONG); 将以java.lang.NullPointerException 结尾

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</android.support.design.widget.CoordinatorLayout>

【讨论】:

  • 为什么不呢?文档here 说它需要View,而不是CoordinatorLayout
  • 在RelativeLayout中显示Snackbar作为根本原因在我的情况下出现异常,替换相对布局并使用cordinator修复问题。
【解决方案3】:

我在 google 上做了很多工作,试图在 android 片段中显示小吃栏,但它没有发生,因为我需要将父视图添加为“CoordinatorLayout”。

在 CoordinatorLayout 中,我使用了整个布局结构,并将 CoordinatorLayout 作为小吃店的父视图。

如下:

=> 在 .xml 文件中

=> Java 类文件

CoordinatorLayout parentLayout = (CoordinatorLayout)rootView.findViewById(R.id.parentLayout);

    private void showInternetAlert(){

    Snackbar snackbar = Snackbar
            .make(parentLayout, "No internet connection!", Snackbar.LENGTH_LONG)
            .setAction("RETRY", new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                }
            });

    // Changing message text color
    snackbar.setActionTextColor(Color.RED);

    // Changing action button text color
    View sbView = snackbar.getView();
    TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
    textView.setTextColor(Color.YELLOW);

    snackbar.show();

}

对我来说效果很好。

xml 文件中出现的问题,我们需要将父视图放置为“CoordinatorLayout”

【讨论】:

    【解决方案4】:

    例如,假设您现有的布局是 RelativeLayout,您可以将 CoordinatorLayout 添加到相对布局,如下所示:

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:id="@+id/myCoordinatorLayout"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">
    </android.support.design.widget.CoordinatorLayout>
    

    然后,确保将 CoordinatorLayout 作为 Snackbar.make() 命令的第一个参数传递。

    final View viewPos = findViewById(R.id.myCoordinatorLayout);    
    Snackbar.make(viewPos, R.string.snackbar_text, Snackbar.LENGTH_LONG)
                                .setAction(R.string.snackbar_action_undo, showListener)
                                .show();
    

    这将导致 Snackbar 显示在提供给 make() 函数的 CoordinatorLayout 的底部。

    如果您传递一个不是 CoordinatorLayout 的 View,Snackbar 将向上遍历树,直到找到 CoordinatorLayout 或布局的根。

    【讨论】:

    • 不,不是。该方法将View 作为第一个参数。如果他们想强制使用CoordinatorLayout,他们会明确地将其用作第一个参数,而不是通用View
    【解决方案5】:

    在 Snakebar 中,您必须传递视图或父布局视图。因此,只需将您的 id 替换为 view 并将您的代码替换为 this 。

    Snackbar.make(view, "No images.", Snackbar.LENGTH_LONG).show();
    

    【讨论】:

    • 你最好解释一下为什么存在提问者的问题,而不仅仅是解决方案。
    • 会有什么不同?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-04
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-25
    相关资源
    最近更新 更多