我想使用viewPager在活动中显示地图,但我仍然在xml文件中出现渲染问题。
片段标记允许布局文件动态包含不同的文件 运行时的布局。在布局编辑时要具体布局 使用不知道。您可以选择所需的布局 编辑布局时预览。 -
(选择布局......)
请勿在此会话中警告标签
add_masjid.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText1"
android:layout_alignBottom="@+id/editText1"
android:layout_alignParentStart="true"
android:text="@string/xyz"
android:textColor="#FFFFFF"
android:textSize="20sp"
/>
<Button
android:id="@+id/generalId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:background="@drawable/rect1"
android:onClick="geoLocate"
android:text="@string/abc"
android:textColor="#FFFFFF"
android:textSize="20sp" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/generalId"
android:layout_alignBottom="@+id/generalId"
android:layout_toEndOf="@+id/textView1"
android:layout_toStartOf="@+id/generalId"
android:ems="10"
android:inputType="text"
android:labelFor="@+id/editText1" />
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="@+id/generalId" />
</RelativeLayout>
它也在Logcat中创建错误
FATAL EXCEPTION: main
Process: com.example.saroosh.masjidnow, PID: 9220
android.view.InflateException: Binary XML file line #44: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:770)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
at com.example.saroosh.masjidnow.Tabs.AddMasjid.onCreateView(AddMasjid.java:26)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2080)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108)
AddMasjid.java
package com.example.saroosh.masjidnow.Tabs;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.saroosh.masjidnow.R;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
public class AddMasjid extends Fragment {
MapView gMapView;
GoogleMap gMap = null;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v =inflater.inflate(R.layout.add_masjid,container,false);
// gMapView = (MapView) v.findViewById(R.id.map);
// gMapView.getMapAsync(this);
// gMapView.onCreate(savedInstanceState);
// gMapView.onResume(); // needed to get the map to display immediately
// try {
// MapsInitializer.initialize(getActivity().getApplicationContext());
// } catch (Exception e) {
// e.printStackTrace();
// }
return v;
}
// @Override
// public void onMapReady(GoogleMap map) {
// gMap = map;
// gMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
// gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new
// LatLng(49.39,-124.83), 20));
// }
// @Override
// public void onResume() {
// super.onResume();
// gMapView.onResume();
// }
//
// @Override
// public void onPause() {
// super.onPause();
// gMapView.onPause();
// }
//
// @Override
// public void onDestroy() {
// super.onDestroy();
// gMapView.onDestroy();
// }
//
// @Override
// public void onLowMemory() {
// super.onLowMemory();
// gMapView.onLowMemory();
// }
}
请告诉我如何解决这个问题。
答案 0 :(得分:0)
使用 com.google.android.gms.maps.SupportMapFragment 代替 com.google.android.gms.maps.MapView
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="@+id/generalId" />
答案 1 :(得分:0)
我通过在xml中添加以下代码而不是片段来解决我的问题。
add_masjid.xml
<com.google.android.gms.maps.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/generalId">
</com.google.android.gms.maps.MapView>
而不是
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="@+id/generalId" />
或java中的这段代码。
package com.example.saroosh.masjidnow.Tabs;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.saroosh.masjidnow.R;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
public class AddMasjid extends Fragment implements OnMapReadyCallback{
MapView gMapView;
GoogleMap gMap = null;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v =inflater.inflate(R.layout.add_masjid,container,false);
gMapView = (MapView) v.findViewById(R.id.map);
gMapView.getMapAsync(this);
gMapView.onCreate(savedInstanceState);
gMapView.onResume(); // needed to get the map to display immediately
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
return v;
}
@Override
public void onMapReady(GoogleMap map) {
gMap = map;
gMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new
LatLng(0,0), 0));
}
@Override
public void onResume() {
super.onResume();
gMapView.onResume();
}
@Override
public void onPause() {
super.onPause();
gMapView.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
gMapView.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
gMapView.onLowMemory();
}
}