2014-01-08 69 views
1

我無法在我的平板電腦上運行Google地圖API v2。我似乎無法找到錯誤的地方。該應用程序崩潰,因爲它總是意外停止在標籤中。有人能幫助我嗎?我正在使用FragmentActivity。無法運行Google地圖apiv2

此外我已經清理了項目並確保我導入了google-play-service-lib。我不明白我是如何得到錯誤的。

logcat的:

01-08 12:54:05.450: E/AndroidRuntime(25497): FATAL EXCEPTION: main 
01-08 12:54:05.450: E/AndroidRuntime(25497): java.lang.RuntimeException: Unable to start  
activity ComponentInfo{com.example.googlemaps/com.example.googlemaps.MainActivity}: 
java.lang.NullPointerException 
01-08 12:54:05.450: E/AndroidRuntime(25497):at 
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211) 
01-08 12:54:05.450: E/AndroidRuntime(25497): at 
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 
01-08 12:54:05.450: E/AndroidRuntime(25497): at 
android.app.ActivityThread.access$600(ActivityThread.java:141) 
01-08 12:54:05.450: E/AndroidRuntime(25497):at 
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 
01-08 12:54:05.450: E/AndroidRuntime(25497):at 
android.os.Handler.dispatchMessage(Handler.java:99) 
01-08 12:54:05.450: E/AndroidRuntime(25497):at android.os.Looper.loop(Looper.java:137) 
01-08 12:54:05.450: E/AndroidRuntime(25497):at 
android.app.ActivityThread.main(ActivityThread.java:5103) 
01-08 12:54:05.450: E/AndroidRuntime(25497): at 
java.lang.reflect.Method.invokeNative(Native Method) 
01-08 12:54:05.450: E/AndroidRuntime(25497):at 
java.lang.reflect.Method.invoke(Method.java:525) 
01-08 12:54:05.450: E/AndroidRuntime(25497): at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
01-08 12:54:05.450: E/AndroidRuntime(25497): at 
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
01-08 12:54:05.450: E/AndroidRuntime(25497): at 
dalvik.system.NativeStart.main(Native Method) 
01-08 12:54:05.450: E/AndroidRuntime(25497): Caused by: java.lang.NullPointerException 
01-08 12:54:05.450: E/AndroidRuntime(25497): at 
com.example.googlemaps.MainActivity.setUpMap(MainActivity.java:47) 
01-08 12:54:05.450: E/AndroidRuntime(25497): at 
com.example.googlemaps.MainActivity.setUpMapIfNeeded(MainActivity.java:33) 
01-08 12:54:05.450: E/AndroidRuntime(25497):at 
com.example.googlemaps.MainActivity.onCreate(MainActivity.java:25) 
01-08 12:54:05.450: E/AndroidRuntime(25497): at 
android.app.Activity.performCreate(Activity.java:5133) 
01-08 12:54:05.450: E/AndroidRuntime(25497): at 
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
01-08 12:54:05.450: E/AndroidRuntime(25497): at 
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175) 
01-08 12:54:05.450: E/AndroidRuntime(25497): ... 11 more 
01-08 12:56:09.040: E/MPL-storeload(440): calData from inv_save_mpl_states, size=2 

主要:

package com.example.googlemaps; 

import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.SupportMapFragment; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.MarkerOptions; 

import android.location.Criteria; 
import android.location.Location; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.view.Menu; 
import android.support.v4.app.FragmentActivity; 


public class MainActivity extends FragmentActivity { 

private GoogleMap googleMap; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    setUpMapIfNeeded(); 
} 

private void setUpMapIfNeeded() { 
    // TODO Auto-generated method stub 
    if (googleMap == null) { 
     googleMap = ((SupportMapFragment) 
     getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); 
     if (googleMap != null) { 
      setUpMap(); 
     } 
    } 
} 

private void setUpMap() { 
    // TODO Auto-generated method stub 
    googleMap.setMyLocationEnabled(true); 

    LocationManager locationManager = (LocationManager) 
      getSystemService(LOCATION_SERVICE); 
    Criteria criteria = new Criteria(); 
    String provider = locationManager.getBestProvider(criteria, true); 
    Location myLocation = locationManager.getLastKnownLocation(provider); 
    googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID); 
    double latitude = myLocation.getLatitude(); 
    double longitude = myLocation.getLongitude(); 

    LatLng latLng = new LatLng(latitude, longitude); 
    googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
    googleMap.animateCamera(CameraUpdateFactory.zoomTo(10)); 
    googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude, 
      longitude)).title("You are here!")); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 
} 

清單:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.googlemaps" 
android:versionCode="1" 
android:versionName="1.0" > 

<permission 
    android:name="com.example.googlemaps.permission.MAPS_RECEIVE" 
    android:protectionLevel="signature" /> 

<uses-permission android:name="com.example.googlemaps.permission.MAPS_RECEIVE" /> 
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission 
android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

<uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="16" /> 

<uses-feature 
    android:glEsVersion="0x00020000" 
    android:required="true" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 

    <activity 
     android:name="com.example.googlemaps.MainActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <meta-data 
     android:name="com.google.android.gms.version" 
     android:value="@integer/google_play_services_version" /> 

    <meta-data 
     android:name="com.google.android.maps.v2.API_KEY" 
     android:value="AIzaSyC5RVOfXZGfsFHoMosdF_mOOcfaqUZEJJU" /> 
</application> 

</manifest> 

佈局:

<?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"> 

<fragment 
    android:name="com.google.android.gms.maps.SupportMapFragment" 
    xmlns:map="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/map" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    /> 

</LinearLayout> 
+0

[R U ON設備或模擬器測試?模擬器中的 – Raghunandan

+0

他將如何測試Google Map V2,它總是要求更新庫........在模擬器中運行Map v2過於忙碌。 –

+0

@SatyakiMukherjee你可以在4.4。看到第二點這裏http://developer.android.com/google/play-services/setup.html – Raghunandan

回答

0
LocationManager locationManager = (LocationManager)getSystemService(LOCATION_SERVICE); 
Criteria criteria = new Criteria(); 
String provider = locationManager.getBestProvider(criteria, true); 
Location myLocation = locationManager.getLastKnownLocation(provider); 

在上述代碼中,

您已爲條件創建了新對象,但未指定標準。這可能產生問題的

嘗試下面的代碼,

LocationManager locationManager = (LocationManager)getSystemService(LOCATION_SERVICE); 
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
if (locationManager != null) 
{ 
    Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
    if (location != null) 
    { 
     latitude = location.getLatitude(); 
     longitude = location.getLongitude(); 
    } 
} 
+0

'01-08 16:03:03.390:E/AndroidRuntime(29726):java.lang.RuntimeException:無法啓動活動ComponentInfo {com.example.googlemaps/com.example.googlemaps。 MainActivity}:java.lang.ClassCastException:com.example.googlemaps.MainActivity無法轉換爲android.location.LocationListener' – user3164712

+0

發生了另一個錯誤:( – user3164712

+0

將'implements LocationListener'添加到Activity中。 – VIGNESH