我尝试使用Map v2。我使用sha1代码获得了api密钥。但每次我去参加我的活动时,我的应用都会崩溃。
这是我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mehdi"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<permission
android:name="com.example.mehdi.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="com.example.mehdi.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data
android:name="com.google.android.gms.v2.API_KEY"
android:value="my api key is here"/>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name=".Splash"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这是我的Splash活动:
package com.example.mehdi;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
public class Splash extends Activity {
static final LatLng HAMBURG = new LatLng(53.558, 9.927);
static final LatLng KIEL = new LatLng(53.551, 9.993);
private GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
.title("Hamburg"));
Marker kiel = map.addMarker(new MarkerOptions()
.position(KIEL)
.title("Kiel")
.snippet("Kiel is cool")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.ic_launcher)));
// Move the camera instantly to hamburg with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));
// Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
}
}
这是我的splash.xml
<RelativeLayout 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"
tools:context=".Splash" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
</RelativeLayout>
我确实将google-play-services_lib导入我的工作区并将其设置为库并在我的包中使用它。
这是我在模拟器中运行应用程序后的logCat:
09-11 03:38:11.501: I/ActivityManager(1256): START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.example.mehdi/.Splash} from pid 1406
09-11 03:38:11.521: D/dalvikvm(1256): GC_FOR_ALLOC freed 1061K, 24% free 7962K/10396K, paused 1ms, total 12ms
09-11 03:38:11.521: E/SoundPool(1256): error loading /system/media/audio/ui/Effect_Tick.ogg
09-11 03:38:11.531: W/AudioService(1256): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
09-11 03:38:11.531: E/SoundPool(1256): error loading /system/media/audio/ui/Effect_Tick.ogg
09-11 03:38:11.531: W/AudioService(1256): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
09-11 03:38:11.541: E/SoundPool(1256): error loading /system/media/audio/ui/Effect_Tick.ogg
09-11 03:38:11.541: W/AudioService(1256): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
09-11 03:38:11.551: E/SoundPool(1256): error loading /system/media/audio/ui/Effect_Tick.ogg
09-11 03:38:11.551: W/AudioService(1256): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
09-11 03:38:11.551: E/SoundPool(1256): error loading /system/media/audio/ui/Effect_Tick.ogg
09-11 03:38:11.551: W/AudioService(1256): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
09-11 03:38:11.551: E/SoundPool(1256): error loading /system/media/audio/ui/KeypressStandard.ogg
09-11 03:38:11.551: W/AudioService(1256): Soundpool could not load file: /system/media/audio/ui/KeypressStandard.ogg
09-11 03:38:11.551: E/SoundPool(1256): error loading /system/media/audio/ui/KeypressSpacebar.ogg
09-11 03:38:11.551: W/AudioService(1256): Soundpool could not load file: /system/media/audio/ui/KeypressSpacebar.ogg
09-11 03:38:11.551: E/SoundPool(1256): error loading /system/media/audio/ui/KeypressDelete.ogg
09-11 03:38:11.551: W/AudioService(1256): Soundpool could not load file: /system/media/audio/ui/KeypressDelete.ogg
09-11 03:38:11.551: E/SoundPool(1256): error loading /system/media/audio/ui/KeypressReturn.ogg
09-11 03:38:11.551: W/AudioService(1256): Soundpool could not load file: /system/media/audio/ui/KeypressReturn.ogg
09-11 03:38:11.551: E/SoundPool(1256): error loading /system/media/audio/ui/KeypressInvalid.ogg
09-11 03:38:11.551: W/AudioService(1256): Soundpool could not load file: /system/media/audio/ui/KeypressInvalid.ogg
09-11 03:38:11.551: W/AudioService(1256): onLoadSoundEffects(), Error -1 while loading samples
09-11 03:38:11.861: D/dalvikvm(2586): Not late-enabling CheckJNI (already on)
09-11 03:38:11.861: I/ActivityManager(1256): Start proc com.example.mehdi for activity com.example.mehdi/.Splash: pid=2586 uid=10059 gids={50059, 3003, 1028, 1015}
09-11 03:38:11.981: D/AndroidRuntime(2586): Shutting down VM
09-11 03:38:11.981: W/dalvikvm(2586): threadid=1: thread exiting with uncaught exception (group=0xb2d21b20)
09-11 03:38:11.981: E/AndroidRuntime(2586): FATAL EXCEPTION: main
09-11 03:38:11.981: E/AndroidRuntime(2586): Process: com.example.mehdi, PID: 2586
09-11 03:38:11.981: E/AndroidRuntime(2586): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mehdi/com.example.mehdi.Splash}: java.lang.NullPointerException
09-11 03:38:11.981: E/AndroidRuntime(2586): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
09-11 03:38:11.981: E/AndroidRuntime(2586): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
09-11 03:38:11.981: E/AndroidRuntime(2586): at android.app.ActivityThread.access$800(ActivityThread.java:135)
09-11 03:38:11.981: E/AndroidRuntime(2586): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
09-11 03:38:11.981: E/AndroidRuntime(2586): at android.os.Handler.dispatchMessage(Handler.java:102)
09-11 03:38:11.981: E/AndroidRuntime(2586): at android.os.Looper.loop(Looper.java:136)
09-11 03:38:11.981: E/AndroidRuntime(2586): at android.app.ActivityThread.main(ActivityThread.java:5017)
09-11 03:38:11.981: E/AndroidRuntime(2586): at java.lang.reflect.Method.invokeNative(Native Method)
09-11 03:38:11.981: E/AndroidRuntime(2586): at java.lang.reflect.Method.invoke(Method.java:515)
09-11 03:38:11.981: E/AndroidRuntime(2586): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-11 03:38:11.981: E/AndroidRuntime(2586): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-11 03:38:11.981: E/AndroidRuntime(2586): at dalvik.system.NativeStart.main(Native Method)
09-11 03:38:11.981: E/AndroidRuntime(2586): Caused by: java.lang.NullPointerException
09-11 03:38:11.981: E/AndroidRuntime(2586): at com.example.mehdi.Splash.onCreate(Splash.java:25)
09-11 03:38:11.981: E/AndroidRuntime(2586): at android.app.Activity.performCreate(Activity.java:5231)
09-11 03:38:11.981: E/AndroidRuntime(2586): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-11 03:38:11.981: E/AndroidRuntime(2586): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
09-11 03:38:11.981: E/AndroidRuntime(2586): ... 11 more
09-11 03:38:12.091: E/BufferQueue(918): [com.android.launcher/com.android.launcher2.Launcher] cancelBuffer: slot 0 is not owned by the client (state=3)
09-11 03:38:12.511: W/ActivityManager(1256): Force finishing activity com.example.mehdi/.Splash
09-11 03:38:12.841: I/WindowManager(1256): Screenshot max retries 4 of Token{b3355e18 ActivityRecord{b34e9d78 u0 com.example.mehdi/.Splash t7 f}} appWin=Window{b3374de0 u0 Starting com.example.mehdi} drawState=4
09-11 03:38:12.841: W/WindowManager(1256): Screenshot failure taking screenshot for (800x1280) to layer 21010
09-11 03:38:13.021: D/dalvikvm(1375): GC_FOR_ALLOC freed 399K, 26% free 4631K/6176K, paused 46ms, total 46ms
09-11 03:38:13.081: D/DropBoxEntryAddedChimeraService(1508): User is not opted-in to Usage & Diagnostics or Lockbox.
09-11 03:38:13.361: W/ActivityManager(1256): Activity pause timeout for ActivityRecord{b34e9d78 u0 com.example.mehdi/.Splash t7 f}
我也尝试在多个Android物理设备上启动应用程序,但所有这些都崩溃了。
我解决了这个问题。感谢@ gaurav4sarma。溶液
我用api&gt; 20这个问题。较低的api是问题所在。 我需要添加支持v7来扩展AppCompatActivity。
答案 0 :(得分:1)
您获得的错误是地图片段的NPE错误。主要原因是你没有在onCreate里面的setContentView方法中设置正确的内容布局。
setContentView(R.layout.activity_main); //Your Layout is splash.xml
将其更改为
setContentView(R.layout.splash);
即使这将解决NPE问题。我仍然建议使用支持库版本的map,然后在地图的async回调中进行地图操作,如下所示
<RelativeLayout 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"
tools:context=".MainActivity" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>
在OnCreate内部实例化地图如下
public class Splash extends AppCompatActivity implements OnMapReadyCallback {
static final LatLng HAMBURG = new LatLng(53.558, 9.927);
static final LatLng KIEL = new LatLng(53.551, 9.993);
SupportMapFragment mapFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap map) {
Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
.title("Hamburg"));
Marker kiel = map.addMarker(new MarkerOptions()
.position(KIEL)
.title("Kiel")
.snippet("Kiel is cool")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.ic_launcher)));
// Move the camera instantly to hamburg with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));
// Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
}
}
答案 1 :(得分:0)
用FragmentActivity替换扩展活动
public class MainActivity extends FragmentActivity {
也以这种方式使用地图:
SupportMapFragment mapFragment=(SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map);
map = mapFragment .getMap();
将您的xml更改为:
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
android:name
属性很重要。没有它,map将返回null。
注意:如果android:name="com.google.android.gms.maps.MapFragment"
不起作用,请尝试android:name="com.google.android.gms.maps.SupportMapFragment"