小编J_S*_*zle的帖子

findMapFragment的findFragmentById在Android Studio中返回null

最近我将项目从Eclipse迁移到Android Studio.除了我的一个使用SupportMapFragment的片段外,一切都设置正常.下面的findFragmentById(在Eclipse中构建时工作)现在返回null :(

public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    SupportMapFragment m = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.safety_map));
Run Code Online (Sandbox Code Playgroud)

xml的片段...

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

     <fragment
         android:id="@+id/safety_map"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:layout_marginBottom="40dp"
         map:cameraTargetLat="@string/livesafe_latitude"
         map:cameraTargetLng="@string/livesafe_longitude"
         map:uiZoomControls="false"
         class="com.google.android.gms.maps.SupportMapFragment"/>
Run Code Online (Sandbox Code Playgroud)

这是我的build.gradle中的依赖项:

 dependencies {
    //google analytics
    compile 'com.google.apis:google-api-services-analytics:v3-rev103-1.19.0'
    //support library for api 10
    compile 'com.android.support:support-v4:21.0.0'
    //google play services
    compile 'com.google.android.gms:play-services:6.1.11'
    compile project(':facebook')
    compile files('libs/android-support-multidex.jar')
    compile files('libs/aws-android-sdk-1.6.0-debug.jar')
    compile files('libs/FlurryAnalytics_3.3.2.jar')
 }
Run Code Online (Sandbox Code Playgroud)

我没有更改以前在Eclipse中工作的xml文件或Fragment类中的任何代码.

eclipse android gradle android-studio

38
推荐指数
2
解决办法
3万
查看次数

标签 统计

android ×1

android-studio ×1

eclipse ×1

gradle ×1