2015-04-30 84 views
1

我試圖實現一個地圖視圖,但我在日誌貓Android的MapView類lava.lang.nullpointerexception

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> 

什麼似乎是問題?應用程序崩潰時嘗試去這個class.is有一種方法來解決這個問題?提前Thanx。

編輯

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

提供完整的logcat標籤'AndroidRuntime' – Kushal

+0

現在你可以看看 – CraZyDroiD

回答

0
inflater = (LayoutInflater) activity 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

inflater = (LayoutInflater) getActivity() 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
0

替換此添加空指針點擊這裏:

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)); 
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的支票 - >如果(GoogleMap的!= null){// you code}