【问题标题】:Android studio Java - Bottom Sheet view over mapsAndroid studio Java - 地图的底页视图
【发布时间】:2019-09-23 13:21:49
【问题描述】:

android 编程新手,java,我正在尝试创建一个“简单的..”BottomSheet,当我单击我的一个位置时,它必须出现。实际上我已经能够创建我的地图并加载我的点数据。我的地图在片段中..

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent">

<fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="true"

    />

</RelativeLayout>

然后我创建了查看在线教程的 BottomSheet。

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinaCoordinatorLayout        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"
tools:context="com.chargesplit.android.activities.MainActivity">


 <android.support.v4.widget.NestedScrollView
    android:id="@+id/nestedScrollView"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:background="@android:color/holo_green_light"
    android:clipToPadding="true"
    app:behavior_hideable="true"
    app:behavior_peekHeight="0dp"
    app:layout_behavior="@string/bottom_sheet_behavior">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="16dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ciao"
            android:textColor="@android:color/white"
            android:textSize="24sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:text="ciao2"
            android:textColor="@android:color/white" />
    </LinearLayout>

</android.support.v4.widget.NestedScrollView>

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

最后,在我的 Main Activity 中,我设法引用了我的底页,但可能由于缺乏经验,我每次都会在这一行出错:

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

   -> View nestedScrollView = (View) findViewById(R.id.nestedScrollView);
    mBottomSheetBehaviour = BottomSheetBehavior.from(nestedScrollView);

我总是遇到这个错误,我可能在这里遗漏了一部分......有人可以尝试让我朝着正确的方向前进以了解问题所在吗?

java.lang.NullPointerException: Attempt to invoke virtual method    'android.view.ViewGroup$LayoutParams android.view.View.getLayoutParams()' on a null object   reference

【问题讨论】:

    标签: java android android-fragments


    【解决方案1】:

    发生此错误是因为您的活动无法找到您的底部工作表视图,为什么会这样?因为您的底部工作表视图未包含在 activity_main.xml 布局中。

    也就是说,您的MainActivity.java 只能查看activity_main.xml,但它仍然不知道您底页的另一个xml 文件。

    要解决这个问题:将 activity_main 布局中的代码替换为

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinaCoordinatorLayout        
        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"
        tools:context="com.chargesplit.android.activities.MainActivity">
    
        <fragment
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:clickable="true"/>
    
    
         <android.support.v4.widget.NestedScrollView
            android:id="@+id/nestedScrollView"
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:background="@android:color/holo_green_light"
            android:clipToPadding="true"
            app:behavior_hideable="true"
            app:behavior_peekHeight="0dp"
            app:layout_behavior="@string/bottom_sheet_behavior">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:padding="16dp">
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="ciao"
                    android:textColor="@android:color/white"
                    android:textSize="24sp" />
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="16dp"
                    android:text="ciao2"
                    android:textColor="@android:color/white" />
            </LinearLayout>
    
        </android.support.v4.widget.NestedScrollView>
    
    </android.support.design.widget.CoordinaCoordinatorLayout>
    

    警告:为了让您的布局看起来整洁,您可以将底部工作表视图分离到一个单独的布局中,这样您就可以将您的&lt;android.support.v4.widget.NestedScrollView&gt; 放入底部_sheet.xml 中,然后从 activity_main.xml 中引用它

    <include layout="@layout/bottom_sheet" />
    

    希望对您有所帮助。如果您需要进一步的帮助,请告诉我

    【讨论】:

      猜你喜欢
      • 2020-02-06
      • 1970-01-01
      • 2022-11-03
      • 2012-06-01
      • 2017-12-11
      • 1970-01-01
      • 2016-08-01
      • 1970-01-01
      • 2020-07-29
      相关资源
      最近更新 更多