Android Google Maps Api v2 SupportMapFragment.getMap()在Android Studio中返回null,但在Eclipse ADT中不返回

时间:2014-09-16 02:14:55

标签: android eclipse android-fragments android-studio google-maps-api-2

虽然我看过许多关于SupportMapFragment在.getMap()上返回空地图的帖子,但我还没有看到任何有关此特定问题的帖子。如果我错了,请原谅我,但感谢任何帮助。

我有一个项目,我希望能够在Android Studio中工作。一切都进口没有错误。库依赖项传输正常,Google Play服务被添加到build.gradle以及Android Support v4,正如预期的那样。在ADT IDE中运行代码时,一切运行正常,但是从Android Studio运行时,事情就变得很糟糕。

以下是我对此问题的各种代码:

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        class="com.example.areaFinder.WorkaroundMapFragment" />

</LinearLayout>

WorkAroundMapFragment.java

import android.content.Context;
import android.os.Bundle;
import android.widget.FrameLayout;

import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.maps.SupportMapFragment;

public class WorkaroundMapFragment extends SupportMapFragment {
    private OnTouchListener mListener;

    @Override
    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle savedInstance) {
        View layout = super.onCreateView(layoutInflater, viewGroup, savedInstance);

        TouchableWrapper frameLayout = new TouchableWrapper(getActivity());

        frameLayout.setBackgroundColor(getResources().getColor(android.R.color.transparent));

        ((ViewGroup) layout).addView(frameLayout,
            new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        return layout;
    }

    public void setListener(OnTouchListener listener) {
        mListener = listener;
    }

    public interface OnTouchListener {
        public abstract void onTouch();
    }

    public class TouchableWrapper extends FrameLayout {

        public TouchableWrapper(Context context) {
            super(context);
        }

        @Override
        public boolean dispatchTouchEvent(MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mListener.onTouch();
                break;
            case MotionEvent.ACTION_UP:
                mListener.onTouch();
                break;
            }
            return super.dispatchTouchEvent(event);
        }
    }
}

MainActivity.java(onCreate()方法)

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // This is returning null from Android Studio but not Eclipse
    map = ((WorkaroundMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
    mMarkers = new ArrayList<Marker>();
    ...
}

我收到的唯一错误消息是函数.getMap()返回null,否则无用的信息。

感谢您的帮助。如果我在适当地提出问题时出错,请告诉我。

0 个答案:

没有答案