Android студия Java - вид снизу на карты - PullRequest
1 голос
/ 22 сентября 2019

новичок в программировании 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>

Наконец, в моей основной деятельности мне удалось сослаться на свое днолист, но, вероятно, из-за неопытности, я каждый раз получаю сообщение об ошибке в этой строке:

    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

1 Ответ

0 голосов
/ 23 сентября 2019

Эта ошибка возникает из-за того, что ваша деятельность не может найти ваш нижний лист, почему так?потому что ваш вид нижнего листа не включен в макет 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>

Предупреждение: чтобы макеты выглядели аккуратно, вы можете разделить вид нижнего листа наотдельный макет, так что вы можете поместить свой <android.support.v4.widget.NestedScrollView> в скажем bottom_sheet.xml, а затем сослаться на него из Activity_main.xml с

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

Желание, которое поможет вам. Если вам нужна дополнительная помощь, пожалуйста,дай мне знать

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...