MapFragment在android中返回null

时间:2014-12-29 15:52:12

标签: java android android-fragments

发生了什么::

map对象返回null

googleMap = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
  • 我能够找到片段管理器,但不能找到导致null的视图 指针异常
  • 我在基本活动中使用了扩展FragmentActivity所以使用 支持片段
  • 我可以清楚地看到xml中存在的地图fragemnt ...然后我不是 能找到吗?

爪哇

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //Make sure the keyboard only pops up when a user clicks into an EditText
        getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        googleMap = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
    }

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rootId"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".fragments.FrgAddNewJobs" >

    <LinearLayout
        android:id="@+id/gridContainerId"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:visibility="gone" >

        <GridView
            android:id="@+id/gridViewId"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:numColumns="3" >
        </GridView>

        <Button
            android:id="@+id/btnGridConfirmId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/commonConfirm" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/nonGalleryId"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <ScrollView
            android:id="@+id/scrollView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

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

                <FrameLayout
                    android:id="@+id/frameHeaderId"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical" >

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:background="@color/windhya_bluedark_color"
                            android:orientation="horizontal"
                            android:padding="5dp" >

                            <TextView
                                android:id="@+id/txtTitleId"
                                android:layout_width="0dp"
                                android:layout_height="wrap_content"
                                android:layout_weight="1"
                                android:gravity="left"
                                android:text="@string/str_FrgAddNewJobs_addNewJobs"
                                android:textAppearance="?android:attr/textAppearanceMedium"
                                android:textColor="@color/windhya_gold"
                                android:textStyle="bold" />
                        </LinearLayout>

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:orientation="horizontal"
                            android:padding="5dp" >

                            <TextView
                                android:id="@+id/txtCategoryId"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:gravity="left"
                                android:text="@string/str_FrgAddNewJobs_text"
                                android:textAppearance="?android:attr/textAppearanceSmall"
                                android:textSize="14sp"
                                android:textStyle="bold" />

                            <TextView
                                android:id="@+id/txtCategoryNameId"
                                android:layout_width="0dp"
                                android:layout_height="wrap_content"
                                android:layout_weight="1"
                                android:gravity="left"
                                android:textAppearance="?android:attr/textAppearanceSmall"
                                android:textSize="14sp"
                                android:textStyle="bold" />
                        </LinearLayout>

                        <View
                            android:id="@+id/view1"
                            android:layout_width="match_parent"
                            android:layout_height="1dp"
                            android:background="@android:color/darker_gray" />

                        <!-- <TextView -->
                        <!-- android:layout_width="wrap_content" -->
                        <!-- android:layout_height="wrap_content" -->
                        <!-- android:layout_marginLeft="5dp" -->
                        <!-- android:layout_marginRight="5dp" -->
                        <!-- android:layout_marginTop="5dp" -->
                        <!-- android:text="@string/str_FrgAddNewJobs_jobTitle" /> -->

                        <EditText
                            android:id="@+id/edtJobTitleId"
                            android:layout_width="match_parent"
                            android:layout_height="0dp"
                            android:layout_marginLeft="5dp"
                            android:layout_marginRight="5dp"
                            android:layout_marginTop="10dp"
                            android:layout_weight="1"
                            android:background="@drawable/edttxt_round_blackcorners"
                            android:ems="10"
                            android:hint="@string/str_FrgAddNewJobs_jobTitle"
                            android:inputType="textMultiLine" >

                            <requestFocus />
                        </EditText>

                        <!-- <TextView -->
                        <!-- android:layout_width="wrap_content" -->
                        <!-- android:layout_height="wrap_content" -->
                        <!-- android:layout_marginLeft="5dp" -->
                        <!-- android:layout_marginRight="5dp" -->
                        <!-- android:layout_marginTop="5dp" -->
                        <!-- android:text="@string/str_FrgAddNewJobs_jobDescription" /> -->

                        <EditText
                            android:id="@+id/edtJobDescId"
                            android:layout_width="match_parent"
                            android:layout_height="60dp"
                            android:layout_marginBottom="5dp"
                            android:layout_marginLeft="5dp"
                            android:layout_marginRight="5dp"
                            android:layout_marginTop="10dp"
                            android:layout_weight="1"
                            android:background="@drawable/edttxt_round_blackcorners"
                            android:ems="10"
                            android:gravity="top|left"
                            android:hint="@string/str_FrgAddNewJobs_jobDescription"
                            android:inputType="textMultiLine"
                            android:minLines="3"
                            android:scrollbars="vertical" >

                            <requestFocus />
                        </EditText>

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:background="@color/windhya_bluedark_color"
                            android:orientation="horizontal"
                            android:padding="5dp" >

                            <TextView
                                android:id="@+id/txtTitleIddd"
                                android:layout_width="0dp"
                                android:layout_height="wrap_content"
                                android:layout_weight="1"
                                android:gravity="left"
                                android:text="@string/str_FrgAddNewJobs_additionalDetails"
                                android:textAppearance="?android:attr/textAppearanceMedium"
                                android:textColor="@color/windhya_gold"
                                android:textStyle="bold" />
                        </LinearLayout>

                        <View
                            android:layout_width="match_parent"
                            android:layout_height="1dp"
                            android:background="@android:color/darker_gray" />

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:background="@color/windhya_bluedark_color"
                            android:orientation="horizontal" >

                            <Button
                                android:id="@+id/btnDurationId"
                                android:layout_width="0dp"
                                android:layout_height="wrap_content"
                                android:layout_marginBottom="-5dp"
                                android:layout_marginLeft="-5dp"
                                android:layout_marginRight="-5dp"
                                android:layout_marginTop="-5dp"
                                android:layout_weight="1"
                                android:minHeight="-2dp"
                                android:minWidth="-2dp"
                                android:text="@string/str_FrgAddNewJobs_duration"
                                android:textColor="@android:color/white" />

                            <Button
                                android:id="@+id/btnPhotosId"
                                android:layout_width="0dp"
                                android:layout_height="wrap_content"
                                android:layout_marginBottom="-5dp"
                                android:layout_marginLeft="-5dp"
                                android:layout_marginRight="-5dp"
                                android:layout_marginTop="-5dp"
                                android:layout_weight="1"
                                android:minHeight="-2dp"
                                android:minWidth="-2dp"
                                android:text="@string/str_FrgAddNewJobs_photos"
                                android:textColor="@android:color/white" />

                            <Button
                                android:id="@+id/btnLocationId"
                                android:layout_width="0dp"
                                android:layout_height="wrap_content"
                                android:layout_marginBottom="-5dp"
                                android:layout_marginLeft="-5dp"
                                android:layout_marginRight="-5dp"
                                android:layout_marginTop="-5dp"
                                android:layout_weight="1"
                                android:minHeight="-2dp"
                                android:minWidth="-2dp"
                                android:text="@string/str_FrgAddNewJobs_location"
                                android:textColor="@android:color/white" />
                        </LinearLayout>
                    </LinearLayout>
                </FrameLayout>
            </LinearLayout>
        </ScrollView>

        <FrameLayout
            android:id="@+id/frameContainerId"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <LinearLayout
                android:id="@+id/rootJobsDurationId"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal" >

                    <RadioGroup
                        android:id="@+id/radioGroopId"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal" >

                        <RadioButton
                            android:id="@+id/btnRdSpecifiedId"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:checked="true"
                            android:text="@string/str_FrgAddNewJobsDuration_atTheTimeSpecified"
                            android:textSize="9sp" />

                        <RadioButton
                            android:id="@+id/btnRdPossibleId"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="@string/str_FrgAddNewJobsDuration_asSoonAsPossible"
                            android:textSize="9sp" />

                        <RadioButton
                            android:id="@+id/btnRdFlexibleId"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="@string/str_FrgAddNewJobsDuration_iAmFlexible"
                            android:textSize="9sp" />
                    </RadioGroup>
                </LinearLayout>

                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@android:color/darker_gray" />

                <TableLayout
                    android:id="@+id/dateTimeId"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="5dp" >

                    <TableRow
                        android:id="@+id/tableRow1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" >

                        <TextView
                            android:id="@+id/textView1"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="@string/str_FrgAddNewJobsDuration_selectDate" />

                        <TextView
                            android:id="@+id/txtDateId"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="20dp"
                            android:layout_weight="1"
                            android:background="@drawable/edttxt_round_blackcorners"
                            android:clickable="true"
                            android:drawableRight="@drawable/calendar_icon"
                            android:ems="10"
                            android:inputType="date"
                            android:singleLine="true" />
                    </TableRow>

                    <TableRow
                        android:id="@+id/tableRow2"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="5dp" >

                        <TextView
                            android:id="@+id/textView2"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="@string/str_FrgAddNewJobsDuration_selectTime" />

                        <TextView
                            android:id="@+id/txtTimeId"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="20dp"
                            android:layout_weight="1"
                            android:background="@drawable/edttxt_round_blackcorners"
                            android:clickable="true"
                            android:drawableRight="@drawable/time_icon"
                            android:ems="10"
                            android:inputType="time"
                            android:singleLine="true" />
                    </TableRow>
                </TableLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:paddingLeft="5dp"
                    android:paddingRight="5dp"
                    android:paddingTop="10dp" >

                    <Button
                        android:id="@+id/button1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:background="@color/windhya_greyMediumDark"
                        android:text="@string/str_FrgAddNewJobsDuration_saveDraft"
                        android:textColor="@android:color/white" />

                    <Button
                        android:id="@+id/btnPostJobId"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="5dp"
                        android:layout_weight="1"
                        android:background="@color/windhya_redBright"
                        android:text="@string/str_FrgAddNewJobsDuration_postJob"
                        android:textColor="@android:color/white"
                        android:textStyle="bold" />
                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:id="@+id/rootJobsPhotosId"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:visibility="gone" >

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="3"
                    android:orientation="horizontal" >

                    <Button
                        android:id="@+id/btnPhoto"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_weight="1"
                        android:text="Select Photos" />

                    <Button
                        android:id="@+id/btnPhotoConfirm"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_weight="1"
                        android:text="Confirm" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:orientation="horizontal" >

                    <com.windhyaworks.utils.HorizontalListView
                        android:id="@+id/lstHorizontalId"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" />
                </LinearLayout>
            </LinearLayout>

            <FrameLayout
                android:id="@+id/rootJobsLocationId"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="vertical" >

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

                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_gravity="center" >

                    <ImageView
                        android:id="@+id/imageView1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerHorizontal="true"
                        android:layout_centerVertical="true"
                        android:src="@drawable/marker" />

                    <TextView
                        android:id="@+id/poiAddress"
                        android:layout_width="300dp"
                        android:layout_height="wrap_content"
                        android:layout_above="@+id/imageView1"
                        android:layout_alignParentLeft="true"
                        android:background="@drawable/edttxt_round_blackcorners" />
                </RelativeLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal" >

                    <EditText
                        android:id="@+id/edtSearchId"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="5dp"
                        android:layout_weight="1"
                        android:background="@drawable/edttxt_round_blackcorners"
                        android:ems="10"
                        android:hint="@string/editTextHintAddress"
                        android:maxWidth="30dp"
                        android:padding="5dp"
                        android:singleLine="true" >

                        <requestFocus />
                    </EditText>

                    <Button
                        android:id="@+id/btnSearchId"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/commonSearch" />

                    <Button
                        android:id="@+id/btnConfirmId"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/commonConfirm" />
                </LinearLayout>
            </FrameLayout>
        </FrameLayout>
    </LinearLayout>

</LinearLayout>

2 个答案:

答案 0 :(得分:0)

检查Google Maps doc

  • MapFragment的GoogleMap对象已异步初始化
  • 如果片段的视图尚未准备好,
  • mapFragment.getMap()将返回Null
  • 考虑使用新的getMapAsync (OnMapReadyCallback callback)方法

答案 1 :(得分:0)

我有同样的问题,我解决了。 解决方案不是添加

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

在您的布局中。

在Fragment / Activity中按代码添加SupportMapFragment!注意:在添加之前,您需要检查它是否已添加。如果您需要,我会稍后展示我的代码!