我试图实现地图视图,但我在log cat中收到此错误
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.****.***/com.****.***.MapView}: java.lang.NullPointerException
我的代码
public class MapView extends FragmentActivity implements LocationListener {
private LinkedHashMap<Integer, BaseElement> info;
private GoogleMap googleMap;
HotelInfo hotel;
private ProgressDialog dialog;
private String lan, lat, name, email;
private LatLng cameraPosition;
private Marker[] marker;
com.google.android.gms.maps.MapView mapView;
private Activity activity;
private View view;
private ViewGroup container;
private LayoutInflater inflater;
private LinkedHashMap<Integer, BaseElement> hotels;
@Override
protected void onCreate(Bundle savedInstanceState) {
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.activity_activity_map, container,
false);
mapView = (com.google.android.gms.maps.MapView) view
.findViewById(R.id.map);
Bundle savedInstanceState2 = null;
mapView.onCreate(savedInstanceState2);
// Gets to GoogleMap from the MapView and does initialization stuff
googleMap = mapView.getMap();
googleMap.getUiSettings().setMyLocationButtonEnabled(false);
googleMap.setMyLocationEnabled(true);
MapsInitializer.initialize(activity);
mapView.onResume();
/*
}
}
我的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"
android:id="@+id/filmhall"
android:background="@color/common_signin_btn_text_dark" >
<com.google.android.gms.maps.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="4dp"
android:layout_weight="1"/>
</LinearLayout>
似乎有什么问题?当试图去这个类时应用程序崩溃。有没有办法解决这个问题?提前完成。
修改
logcat的
FATAL EXCEPTION: main
Process: com.****.***, PID: 25529
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.****.***/com.****.***.MapView}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2202)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2252)
at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1200)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.fortuna.amaya.MapView.onCreate(MapView.java:56)
at android.app.Activity.performCreate(Activity.java:5275)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2166)
答案 0 :(得分:0)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
用
替换它inflater = (LayoutInflater) getActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
答案 1 :(得分:0)
在此处添加NullPointer检查:
googleMap = mapView.getMap();
if( googleMap != null ) {
googleMap.getUiSettings().setMyLocationButtonEnabled(false);
googleMap.setMyLocationEnabled(true);
}
检查您的申请中的上述空检查,这将阻止Exception
如果您多次犯错误,则可以切换为SupportMapFragment
而不是MapView
更改:
my.xml
<fragment
class="com.google.android.gms.maps.SupportMapFragment"
android:id="@+id/mapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
java文件:
SupportMapFragment m = ((SupportMapFragment) getActivity()
.getSupportFragmentManager().findFragmentById(R.id.mapFragment));
答案 2 :(得分:0)
更改
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
到
inflater = (LayoutInflater) getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.activity_activity_map, container,
false);
setContentView(view);
您要充气的视图必须将其设置为 setContentView 并检查googleMap - &gt; if(googleMap!= null){//代码}