Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:snackbar在片段类中不工作_Android_Android Snackbar_Snackbar - Fatal编程技术网

Android:snackbar在片段类中不工作

Android:snackbar在片段类中不工作,android,android-snackbar,snackbar,Android,Android Snackbar,Snackbar,我想在我的应用程序中使用snackbar,所以我设置了下面的代码 public class FragmentAddProperty extends Fragment { RelativeLayout mRelative public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

我想在我的应用程序中使用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>
我怎样才能解决这个问题


所有建议都是可感知的

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

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

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

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

例如,假设您的现有布局是相对布局,您可以向相对布局添加坐标布局,如下所示:

<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>
这将导致Snackbar显示在提供给make()函数的CoordinatorLayout的底部


如果您传递的视图不是CoordinatorLayout,Snackbar将沿着树向上移动,直到找到CoordinatorLayout或布局的根。

例如,假设您的现有布局是相对布局。您可以按如下方式将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>
这将导致Snackbar显示在提供给make()函数的CoordinatorLayout的底部


如果您传递的视图不是CoordinatorLayout,Snackbar将沿着树向上移动,直到找到CoordinatorLayout或布局的根。

我在google上做了很多工作,试图用android片段显示Snackbar,但是它;这不会发生,因为我需要将父视图添加为“CoordinatorLayout”

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

例如:

=>在.xml文件中

=>Java类文件

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

这对我来说很好


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

我在google上做了很多工作,试图在android片段中显示snackbar,但是它;这不会发生,因为我需要将父视图添加为“CoordinatorLayout”

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

例如:

=>在.xml文件中

=>Java类文件

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

这对我来说很好


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

如果没有
CordinatorLayout
,Snackbar将无法工作,您需要创建
fragment\u list.xml
像这样,或者
Snackbar Snackbar=Snackbar.make(view,R.string.action,Snackbar.LENGTH\u 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>


如果没有
CordinatorLayout
,Snackbar将无法工作,您需要像下面这样创建
片段列表.xml
,或者
Snackbar Snackbar=Snackbar.make(view,R.string.action,Snackbar.LENGTH\u 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>

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

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

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

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

Snackbar.make(查看“无图像”,Snackbar.LENGTH_LONG.show();请将您的xml代码放在IDE上,尝试
build->clean
,并再次运行它,如nackbar.make(查看“无图像”,Snackbar.LENGTH\u LONG).show();请将您的xml代码放在IDE上,尝试
build->clean
,然后再次运行。您最好解释提问者问题存在的原因,而不仅仅是解决方案。区别是什么?您最好解释提问者问题存在的原因,而不仅仅是解决方案。区别是什么?不,不是。该方法将
视图
作为第一个参数。如果他们想强制使用
CoordinatorLayout
,他们会明确地将其用作第一个参数,而不是一般的
视图
不,不是。该方法将
视图
作为第一个参数。如果他们想强制使用
CoordinatorLayout
,他们会明确地将其用作第一个参数,而不是一般的
视图
为什么不呢?文档中说它需要一个
视图
,而不是
协调布局
?在RelativeLayout中显示Snackbar作为根本原因异常在我的情况下,用协调器替换相对布局或修复问题。为什么不?文档中说它需要一个
视图
,而不是
协调布局
?在我的情况下,在RelativeLayout中将Snackbar显示为根本原因异常,用cordinator替换相对布局或修复问题。在与ViewPager2和TableLayout一起使用的片段中不适用于我在与ViewPager2和TableLayout一起使用的片段中不适用于我