相关疑难解决方法(0)

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万
查看次数

Fragment中的getMapAsync()

我在Fragment中实现Google Map时遇到了麻烦.

这是我的片段类的一部分:

public class FragmentStoreFinderMap extends Fragment implements OnMapReadyCallback {

// Google Map
private GoogleMap googleMap;
private int i;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

        googleMap = ((SupportMapFragment)getActivity().getSupportFragmentManager().findFragmentById(
              R.id.map)).getMapAsync(this);
Run Code Online (Sandbox Code Playgroud)

我在getMapAsync(this)中收到错误.我告诉不兼容的类型.

它说:

必需:com.google.android.gms.map.GoogleMap

发现:无效

顺便说一句,这是我的xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment class="com.google.android.gms.maps.SupportMapFragment"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

android google-maps android-fragments supportmapfragment android-studio

14
推荐指数
1
解决办法
3万
查看次数