在没有视图的情况下制作小吃吧?

时间:2016-08-03 15:49:43

标签: java android xml crash android-snackbar

我想在用户打开Goog​​le地图活动后立即显示小吃栏,但事实是活动中没有视图可用作活动的第一个参数(在findViewById()的{ {1}})。我放在那里? 这是java类代码:

Snackbar.make()

此外,这里是活动xml代码:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);

    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    mMap.setBuildingsEnabled(true);
    mMap.getUiSettings().setZoomControlsEnabled(true);
    float cameraZoom = 17;
    LatLng location = new LatLng(43.404032, -80.478184);
    mMap.addMarker(new MarkerOptions().position(location).title("49 McIntyre Place #18, Kitchener, ON N2R 1G3"));
    CameraUpdateFactory.newLatLngZoom(location, cameraZoom);
    Snackbar.make(findViewById(/*WHAT DO I PUT HERE?*/), "Click the pin for more options", Snackbar.LENGTH_LONG).show();
}
}

最后,这是堆栈跟踪错误:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="ca.davesautoservice.davesautoservice.MapsActivity" />

感谢您的帮助! :)

11 个答案:

答案 0 :(得分:56)

我看到一些选项......不确定哪一个可以解决您的问题。

<强> Simpliest

SupportMapFragment扩展了课程android.support.v4.app.Fragment。这样,它有一个方法getView()

Snackbar.make(mapFragment.getView(), "Click the pin for more options", Snackbar.LENGTH_LONG).show();

查找根视图

From this answer,可以通过以下方式获取根视图:

getWindow().getDecorView().getRootView()

所以,也许,你可以这样做:

Snackbar.make(getWindow().getDecorView().getRootView(), "Click the pin for more options", Snackbar.LENGTH_LONG).show();

添加虚拟LinearLayout以获取视图

老实说,我不确定这个解决方案是否可行。我不确定你是否可以在地图片段上方添加一个LinearLayout ...我认为没关系,但由于我以前从未使用过Maps API,我不确定。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/dummy_layout_for_snackbar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <fragment 
        xmlns:map="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="ca.davesautoservice.davesautoservice.MapsActivity" />
</LinearLayout>

然后:

Snackbar.make(findViewById(R.id.dummy_layout_for_snackbar), "Click the pin for more options", Snackbar.LENGTH_LONG).show();

答案 1 :(得分:4)

正如Shahab Rauf指出的那样,通过getDecorView()获取视图可能会将快餐栏放在导航栏的底部。我使用以下代码:(使用并扩展到您的喜悦)

public class BaseActivity extends AppCompatActivity {

    private View getRootView() {
        final ViewGroup contentViewGroup = (ViewGroup) findViewById(android.R.id.content);
        View rootView = null;

        if(contentViewGroup != null)
            rootView = contentViewGroup.getChildAt(0);

        if(rootView == null)
            rootView = getWindow().getDecorView().getRootView();

        return rootView;
    }

    protected void showSnackBarWithOK(@StringRes int res) {
        final View rootView = getRootView();
        if(rootView != null) {
            final Snackbar snackbar = Snackbar.make(getRootView(), res, Snackbar.LENGTH_INDEFINITE);
            snackbar.setAction(R.string.ok, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    snackbar.dismiss();
                }
            });

            snackbar.show();
        }
    }

    protected void showSnackBar(@StringRes int res) {
        final View rootView = getRootView();
        if(rootView != null)
            Snackbar.make(rootView, res, Snackbar.LENGTH_LONG).show();
    }
}

答案 2 :(得分:3)

最简单的方法是利用 Kotlin 中 Extention Function 的优势。

第 1 步:创建一个 kotlin 文件,即 Extensions.kt 并粘贴下面的代码

对于活动:

fun Activity.showSnackBar(msg:String){
    Snackbar.make(this.findViewById(android.R.id.content), msg, Snackbar.LENGTH_SHORT)
        .setAction("Ok"){
        }
        .show()
}

对于片段:

  fun Fragment.showSnackBar(msg:String){
    Snackbar.make(this.requireActivity().findViewById(android.R.id.content), msg, Snackbar.LENGTH_SHORT)
        .setAction("Ok"){
        }
        .show()
}

第 2 步:如下调用:

showSnackBar("PASS HERE YOUR CUSTOM MESSAGE")

答案 3 :(得分:2)

好的,因此上述所有解决方案太复杂或无关紧要。

最简单的解决方案是在任何地方使用此功能

fun showSnackBar(message: String?, activity: Activity?) {
    if (null != activity && null != message) {
        Snackbar.make(
            activity.findViewById(android.R.id.content),
            message, Snackbar.LENGTH_SHORT
        ).show()
    }
}

要在“活动”中调用此功能吗?

showSnackBar("Test Message in snackbar", this)

要在F​​ragment中调用此函数吗?

showSnackBar("Test Message in snackbar", requireActivity())

快乐编码!

答案 4 :(得分:1)

不要这样做。

为什么?

为快餐栏提供锚定视图的整个想法可能是因为它使我们能够将其放置在屏幕上我们最喜欢的位置,

如其他答案中所述,您可以通过多种方式自动将根视图附加到其上,使用getWindow().getDecorView().getRootView()是其中之一,但它可能导致此消息显示在底部导航视图中,看起来不好(至少对我来说)

如果您希望用于常规消息显示的内容用于 toast ,则它更不容易出错并且外观保持一致,如果您需要进行交互,我建议警报对话 strong>

答案 5 :(得分:0)

在任何活动中尝试此操作:

snackbar(findViewById(android.R.id.content),"your text")

答案 6 :(得分:0)

我想说findViewById是最好的方法。使用getWindow().getDecorView().getRootView()将导致该消息显示在底部导航栏的后面。

就我而言,我不需要在XML中创建虚拟元素。我使用了以下代码

Snackbar.make(findViewById(R.id.container),"This will start payment process.", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();

我的XML文件如下

<android.support.design.widget.CoordinatorLayout 
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:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".HomePatientActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/appbar_padding_top"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_weight="1"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/AppTheme.PopupOverlay">

    </android.support.v7.widget.Toolbar>

    <android.support.design.widget.TabLayout
        android:id="@+id/sliding_tabs"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        app:layout_anchorGravity="top"
        app:tabMode="fixed" />

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

<android.support.v4.view.ViewPager
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

</android.support.v4.view.ViewPager>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="end|bottom"
    android:layout_margin="@dimen/fab_margin"
    app:srcCompat="@android:drawable/ic_dialog_email" />

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

答案 7 :(得分:0)

添加任何布局并为该布局分配一个ID示例,如下所示:

<RelativeLayout
    android:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment
        android:id="@+id/map_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        />

</RelativeLayout>

在您的活动中,使用findViewById查找布局:

RelativeLayout relativeLayout = findViewById(R.id.relativeLayout);

分别用于其他布局的示例,只要您为布局分配其ID:

LinearLayout relativeLayout = findViewById(R.id.linearLayout);
FrameLayout frameLayout = findViewById(R.id.frameLayout);

显示快餐栏:

Snackbar.make(relativeLayout,"You are displaying a Snackbar",Snackbar.LENGTH_SHORT).show();

对于其他布局,只需更改布局示例的名称:

Snackbar.make(linearLayout,"You are displaying a Snackbar",Snackbar.LENGTH_SHORT).show();

Snackbar.make(frameLayout,"You are displaying a Snackbar",Snackbar.LENGTH_SHORT).show();

答案 8 :(得分:0)

用作extension function

fun Activity?.showSnackBar(message: String) {
    this ?: return
    Snackbar.make(
        this.findViewById(android.R.id.content),
        message, Snackbar.LENGTH_LONG
    ).show()
}

答案 9 :(得分:0)

随着 Jetpack Compose 稳定版本的发布,现在您不再需要 xml 中的视图。

 @Composable
fun Snackbar() {
    Row {

        if (viewModel.snackbarVisibleState.value) {
            Snackbar(
                action = {
                    Button(onClick = {
                        viewModel.snackbarText.value = ""
                        viewModel.snackbarVisibleState.value = false
                    }) {
                        Text("Ok")
                    }
                },
                modifier = Modifier
                    .padding(8.dp)
                    .align(Alignment.Bottom)
            ) {
                Text(viewModel.snackbarText.value)
                
            }
        }
    }
}

此处的 snackbarVisibleState 是视图模型中的 MutableState,您需要更改其值才能显示和关闭 Snackbar。

有关 Jetpack Compose 的更多代码片段,请参阅此 - https://androidlearnersite.wordpress.com/2021/08/03/jetpack-compose-1-0-0-sample-codes/

答案 10 :(得分:-1)

我认为最简单的是这样:

Snackbar.make(requireView(), R.string.message, Snackbar.LENGTH_SHORT).show();