我无法在平板电脑上运行google maps api v2。我似乎无法找到错误的位置。应用程序崩溃,因为它总是在选项卡中意外停止。有人能帮帮我吗?我正在使用FragmentActivity。
此外,我已经清理了项目,并确保我导入了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 :(得分: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();
}
}