我想在静态cardview中放置一个谷歌地图,其他元素如按钮,textview等。
所以我创建了这个xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/prenotation"
>
<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal|bottom">
<com.google.android.gms.maps.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="160dp"
android:enabled="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="LIMB"
android:textColor="#000"
android:textSize="30dp"
android:id="@+id/and"
android:layout_marginTop="40dp"
android:layout_below="@+id/map"
android:layout_alignLeft="@+id/view1"
android:layout_alignStart="@+id/view1"
android:layout_marginLeft="30dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="LIPU"
android:textColor="#000"
android:textSize="30dp"
android:id="@+id/rit"
android:layout_alignTop="@+id/and"
android:layout_alignRight="@+id/view1"
android:layout_alignEnd="@+id/view1"
android:layout_marginRight="30dp"
android:layout_marginEnd="30dp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
这在Java中:
public class PrenotationFragment extends Fragment
{
String objID;
MapView mMapView;
GoogleMap mgoogleMap;
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_prenotation, container, false);
Bundle args = getArguments();
Button prenota = (Button)rootView.findViewById(R.id.prenotate);
if(args != null)
{
objID = args.getString("Obj");
Download task = new Download(objID,getActivity(),rootView);
task.execute();
}
prenota.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
String url = "http://www.aviosharing.eu/?flight=("+objID+")";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
mMapView = (MapView) rootView.findViewById(R.id.map);
mMapView.onCreate(savedInstanceState);
mMapView.onResume();// needed to get the map to display immediately
try
{
MapsInitializer.initialize(getActivity().getApplicationContext());
}
catch (Exception e)
{
e.printStackTrace();
}
mMapView.getMapAsync(new OnMapReadyCallback()
{
public void onMapReady(GoogleMap googleMap)
{
mgoogleMap = googleMap;
}
});
return rootView;
}
public void onResume() {
super.onResume();
mMapView.onResume();
}
public void onPause() {
super.onPause();
mMapView.onPause();
}
public void onLowMemory() {
super.onLowMemory();
mMapView.onLowMemory();
}
public void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
}
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mMapView.onSaveInstanceState(outState);
}
但是当我加载它时,我收到此错误消息:
java.lang.NullPointerException:尝试调用虚方法&#39; void com.google.android.gms.maps.MapView.onCreate(android.os.Bundle)&#39;在...上 null对象引用 在 com.example.carsharing.PrenotationFragment.onCreateView(PrenotationFragment.java:79) 在 android.support.v4.app.Fragment.performCreateView(Fragment.java:1974) 在 android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) 在 android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252) 在 android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742) 在 android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617) 在 android.support.v4.app.FragmentManagerImpl $ 1.run(FragmentManager.java:517) 在android.os.Handler.handleCallback(Handler.java:739) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:135) 在android.app.ActivityThread.main(ActivityThread.java:5253) at java.lang.reflect.Method.invoke(Native Method) 在java.lang.reflect.Method.invoke(Method.java:372) 在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:899) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
为什么我收到此错误?我想也是,我需要使用:
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
在这种情况下,而不是MapView?
由于