我需要创建一个包含的简单应用程序 1.自定义MapFragment,它可以显示当前位置(LocationListener?),两个标记等。 2.简单的空碎片。
应用程序不得使用支持v4(sdk 11+)/ FragmentActivity。
清空片段。
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black" >
</RelativeLayout>
类别:
public class FirstFragment extends Fragment{
public FirstFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.first_fragment, container, false);
return view;
}
}
映射片段(尝试使其成为单例)。
XML:
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment"/>
类
public class ThirdMapFragment extends MapFragment {
public static MapFragment instance;
@Override
public View onCreateView(LayoutInflater arg0, ViewGroup arg1, Bundle arg2) {
View view;
view = arg0.inflate(R.layout.map_fragment, arg1, false);
return view;
}
public static MapFragment getInstance() {
if (instance == null) {
instance = newInstance();
}
// NullPointerException in getMap() ???
// instance.getMap().addMarker(new MarkerOptions().position(new LatLng(41.1, 39)));
return instance;
}
@Override
public void onCreate(Bundle arg0) {
super.onCreate(arg0);
}
}
主要活动:
public class ActivityMain extends Activity implements OnClickListener {
FragmentManager fragmentManager;
FragmentTransaction transaction;
FirstFragment firstFragment;
MapFragment mapFragment;
// Buttons
Button b4;
int i = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
fragmentManager = getFragmentManager();
transaction = fragmentManager.beginTransaction();
b4 = (Button) findViewById(R.id.button4);
b1.setOnClickListener(this);
firstFragment = new FirstFragment();
mapFragment = ThirdMapFragment.getInstance();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public void onClick(View v) {
makeTransaction(v.getId());
}
private void makeTransaction(int id) {
i++;
transaction = fragmentManager.beginTransaction();
if (i == 1) {
transaction.replace(R.id.relativeLayout, firstFragment).commit();
} else if (i == 2) {
transaction.replace(R.id.relativeLayout, secondFragment).commit();
} else {
transaction.replace(R.id.relativeLayout, mapFragment).commit();
i = 0;
}
}
}
R.id.relativeLayout - main.xml中的布局
但我没有看到地图(带缩放按钮的空屏幕)以及如何制作LocationListener? THX。
答案 0 :(得分:1)
如果您使用的是支持库,则必须使用SupportMapFragment类而不是地图片段的MapFragment。您的活动应该是FragmentActivity以支持库。以及Fragment经理。你必须getSupportFragmentManager()。
看看这段代码:
import com.google.android.gms.common.GooglePlayServicesUtil;
import android.os.Bundle;
import com.google.android.gms.maps.*;
public class Map extends android.support.v4.app.FragmentActivity
{
private GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext()) == 0)
{
map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
}
}}
如果在进行这些更改后仍然没有看到地图。检查您的API控制台是否已启用Google Map ANDROID API V2,而不仅仅是可能导致与您描述的结果相同的Google Map API V2。
答案 1 :(得分:1)
视图尚未创建,这就是为什么getMap()返回null,map不存在,将代码放入onResume
答案 2 :(得分:0)
放入清单,android的关键? 例如:
<application
android:icon="@drawable/android"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyB2s6isdfsghDfgdfdfgDFGDG-yi4-aAYUma0Owc"/>
https://developers.google.com/maps/documentation/android/start