this.getView()创建映射片段后返回null [英] this.getView() returns null after creating a map fragment

查看:199
本文介绍了this.getView()创建映射片段后返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想移动的谷歌地图V2我的位置按钮,使用该解决方案作为建议这里

我遇到的问题是,我得到一个NullPointerException异常,而事实证明, this.getView()返回null,这是奇数,由时间达到该行,该片段已创建。该onCreateView()程序已经完成,一些inits已经完成的地图,所以我敢肯定它的存在。该应用程序的其他部分可以访问此地图对象这一优良,为好。

我的main.xml文件的相关部分(有这RelativeLayout的课程多很多,但这是唯一的地图碎片present):

 <的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:地图=htt​​p://schemas.android.com/apk/res-auto
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=垂直><片段
    机器人:ID =@ + ID /图
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    类=com.squirrel.hkairpollution.MySupportMapFragment
/>

而code:

 包com.squirrel.hkairpollution;(进口略)公共类MySupportMapFragment扩展SupportMapFragment {私有静态最后弦乐TAG = HKAirPollution.TAG;公共MySupportMapFragment(){
    返回;
}@覆盖
公共查看onCreateView(LayoutInflater为arg0,ARG1的ViewGroup,捆绑ARG2){
    Log.v(TAG,在重写onCreateView。);
    视图V = super.onCreateView(为arg0,ARG1,ARG2);
    Log.v(TAG,正在初始化地图。);
    initMap();
    Log.v(TAG,移动MyPositionButton);
    resetMyPositionButton();
    返回伏;
}私人无效initMap(){
    UiSettings设置=的GetMap()getUiSettings()。
    settings.setAllGesturesEnabled(真);
    settings.setMyLocationButtonEnabled(真);
    经纬度经纬度=新的经纬度(22.320542,114.185715);
    。的GetMap()moveCamera(CameraUpdateFactory.newLatLngZoom(经纬度,11));
}
/ **
 *把我的位置按钮在地图下方
 * /
私人无效resetMyPositionButton()
{
    在地图控件//深路径
    V1的ViewGroup =(ViewGroup中)this.getView();
    V2的ViewGroup =(ViewGroup中)v1.getChildAt(0);
    V3的ViewGroup =(ViewGroup中)v2.getChildAt(0);
    V4的ViewGroup =(ViewGroup中)v3.getChildAt(1);    //我的位置按钮
    查看位置=(查看)v4.ge​​tChildAt(0);    INT positionWidth = position.getLayoutParams()宽。
    INT positionHeight = position.getLayoutParams()的高度。    //铺陈位置按钮
    RelativeLayout.LayoutParams positionParams =新RelativeLayout.LayoutParams(positionWidth,positionHeight);
    INT保证金= positionWidth / 5;
    positionParams.setMargins(0,0,0,余量);
    positionParams.addRule(RelativeLayout.CENTER_HORIZONTAL,RelativeLayout.TRUE);
    positionParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.TRUE);
    position.setLayoutParams(positionParams);
}
}


解决方案

这是一个快速解决方案,极大的。在这里,完整和工作code:

 包com.squirrel.hkairpollution;进口android.os.Bundle;
进口android.util.Log;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.RelativeLayout;进口com.google.android.gms.maps.CameraUpdateFactory;
进口com.google.android.gms.maps.GoogleMap;
进口com.google.android.gms.maps.SupportMapFragment;
进口com.google.android.gms.maps.UiSettings;
进口com.google.android.gms.maps.model.LatLng;公共类MySupportMapFragment扩展SupportMapFragment {私有静态最后弦乐TAG = HKAirPollution.TAG;公共MySupportMapFragment(){
    返回;
}@覆盖
公共查看onCreateView(LayoutInflater为arg0,ARG1的ViewGroup,捆绑ARG2){
    Log.v(TAG,在重写onCreateView。);
    视图V = super.onCreateView(为arg0,ARG1,ARG2);
    Log.v(TAG,正在初始化地图。);
    initMap();
    返回伏;
}@覆盖
 公共无效onViewCreated(查看视图,捆绑savedInstanceState){
    super.onViewCreated(查看,savedInstanceState);
    resetMyPositionButton();
}私人无效initMap(){
    UiSettings设置=的GetMap()getUiSettings()。
    settings.setAllGesturesEnabled(真);
    settings.setMyLocationButtonEnabled(真);
    经纬度经纬度=新的经纬度(22.320542,114.185715);
    。的GetMap()moveCamera(CameraUpdateFactory.newLatLngZoom(经纬度,11));
}
/ **
 *把我的位置按钮在地图下方
 * /
私人无效resetMyPositionButton()
{
    在地图控件//深路径
    V1的ViewGroup =(ViewGroup中)this.getView();
    V2的ViewGroup =(ViewGroup中)v1.getChildAt(0);
    V3的ViewGroup =(ViewGroup中)v2.getChildAt(0);
    V4的ViewGroup =(ViewGroup中)v3.getChildAt(1);    //我的位置按钮
    查看位置=(查看)v4.ge​​tChildAt(0);    INT positionWidth = position.getLayoutParams()宽。
    INT positionHeight = position.getLayoutParams()的高度。    //铺陈位置按钮
    RelativeLayout.LayoutParams positionParams =新RelativeLayout.LayoutParams(positionWidth,positionHeight);
    INT保证金= positionWidth / 5;
    positionParams.setMargins(0,0,0,余量);
    positionParams.addRule(RelativeLayout.CENTER_HORIZONTAL,RelativeLayout.TRUE);
    positionParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.TRUE);
    position.setLayoutParams(positionParams);
}
}

I am trying to move the My Location button in the Google Maps v2, using the solution as suggested here.

The problem I'm having is that I am getting a nullpointerexception, and it turns out that this.getView() returns null, which is odd, as by the time that line is reached, the fragment has been created. The onCreateView() routine has finished, some inits have been done to the map, so I'm sure it exists. The rest of the app can access this map object this fine, as well.

The relevant part of my main.xml file (there is a lot more in this RelativeLayout of course, but this is the only map fragment present):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.squirrel.hkairpollution.MySupportMapFragment"
/>

And the code:

package com.squirrel.hkairpollution;

(imports omitted)

public class MySupportMapFragment extends SupportMapFragment {

private static final String TAG = HKAirPollution.TAG;

public MySupportMapFragment() {
    return;
}

@Override
public View onCreateView(LayoutInflater arg0, ViewGroup arg1, Bundle arg2) {
    Log.v(TAG, "In overridden onCreateView.");
    View v = super.onCreateView(arg0, arg1, arg2);
    Log.v(TAG, "Initialising map.");
    initMap();
    Log.v(TAG, "Moving the MyPositionButton");
    resetMyPositionButton();
    return v;
}

private void initMap(){
    UiSettings settings = getMap().getUiSettings();
    settings.setAllGesturesEnabled(true);
    settings.setMyLocationButtonEnabled(true);
    LatLng latLong = new LatLng(22.320542, 114.185715);
    getMap().moveCamera(CameraUpdateFactory.newLatLngZoom(latLong,11));
}


/**
 * Move my position button at the bottom of map
 */
private void resetMyPositionButton()
{
    //deep paths for map controls
    ViewGroup v1 = (ViewGroup)this.getView();
    ViewGroup v2 = (ViewGroup)v1.getChildAt(0);
    ViewGroup v3 = (ViewGroup)v2.getChildAt(0);
    ViewGroup v4 = (ViewGroup)v3.getChildAt(1);

    //my position button
    View position =  (View)v4.getChildAt(0);

    int positionWidth = position.getLayoutParams().width;
    int positionHeight = position.getLayoutParams().height;

    //lay out position button
    RelativeLayout.LayoutParams positionParams = new RelativeLayout.LayoutParams(positionWidth,positionHeight);
    int margin = positionWidth/5;
    positionParams.setMargins(0, 0, 0, margin);
    positionParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
    positionParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
    position.setLayoutParams(positionParams);
} 
}

解决方案

That was a quick solution, great. Here the complete and working code:

package com.squirrel.hkairpollution;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.UiSettings;
import com.google.android.gms.maps.model.LatLng;

public class MySupportMapFragment extends SupportMapFragment {

private static final String TAG = HKAirPollution.TAG;

public MySupportMapFragment() {
    return;
}

@Override
public View onCreateView(LayoutInflater arg0, ViewGroup arg1, Bundle arg2) {
    Log.v(TAG, "In overridden onCreateView.");
    View v = super.onCreateView(arg0, arg1, arg2);
    Log.v(TAG, "Initialising map.");
    initMap();
    return v;
}

@Override
 public void onViewCreated (View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    resetMyPositionButton();
}

private void initMap(){
    UiSettings settings = getMap().getUiSettings();
    settings.setAllGesturesEnabled(true);
    settings.setMyLocationButtonEnabled(true);
    LatLng latLong = new LatLng(22.320542, 114.185715);
    getMap().moveCamera(CameraUpdateFactory.newLatLngZoom(latLong,11));
}


/**
 * Move my position button at the bottom of map
 */
private void resetMyPositionButton()
{
    //deep paths for map controls
    ViewGroup v1 = (ViewGroup)this.getView();
    ViewGroup v2 = (ViewGroup)v1.getChildAt(0);
    ViewGroup v3 = (ViewGroup)v2.getChildAt(0);
    ViewGroup v4 = (ViewGroup)v3.getChildAt(1);

    //my position button
    View position =  (View)v4.getChildAt(0);

    int positionWidth = position.getLayoutParams().width;
    int positionHeight = position.getLayoutParams().height;

    //lay out position button
    RelativeLayout.LayoutParams positionParams = new RelativeLayout.LayoutParams(positionWidth,positionHeight);
    int margin = positionWidth/5;
    positionParams.setMargins(0, 0, 0, margin);
    positionParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
    positionParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
    position.setLayoutParams(positionParams);
} 
}

这篇关于this.getView()创建映射片段后返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆