我严格遵循谷歌地图API规则,并根据要求导入谷歌播放服务库。在通过Android SDK意外更新谷歌存储库和谷歌播放服务后,有如下错误。我该怎么做才能纠正这个问题?我私下打开了谷歌播放服务lib项目的包。它显示com.android。 UNstubJava,因为没有其他特殊类支持我的项目。
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.larry.proto.maptest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<permission
android:name="com.larry.proto.maptest.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.larry.proto.maptest.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-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" >
<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="AIzaSyBUHtViyAqpiOofdE0526j4Q-oO9aLut0I" />
<activity
android:name="com.larry.proto.maptest.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>
</application>
</manifest>
res / Laylout / activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"/>
Logcat消息
05-21 14:31:23.636: E/AndroidRuntime(9435): Process: com.larry.proto.maptest, PID: 9435
05-21 14:31:23.636: E/AndroidRuntime(9435): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.larry.proto.maptest/com.larry.proto.maptest.MainActivity}: java.lang.NullPointerException
05-21 14:31:23.636: E/AndroidRuntime(9435): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-21 14:31:23.636: E/AndroidRuntime(9435): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-21 14:31:23.636: E/AndroidRuntime(9435): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-21 14:31:23.636: E/AndroidRuntime(9435): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-21 14:31:23.636: E/AndroidRuntime(9435): at android.os.Handler.dispatchMessage(Handler.java:102)
05-21 14:31:23.636: E/AndroidRuntime(9435): at android.os.Looper.loop(Looper.java:136)
05-21 14:31:23.636: E/AndroidRuntime(9435): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-21 14:31:23.636: E/AndroidRuntime(9435): at java.lang.reflect.Method.invokeNative(Native Method)
05-21 14:31:23.636: E/AndroidRuntime(9435): at java.lang.reflect.Method.invoke(Method.java:515)
05-21 14:31:23.636: E/AndroidRuntime(9435): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-21 14:31:23.636: E/AndroidRuntime(9435): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-21 14:31:23.636: E/AndroidRuntime(9435): at dalvik.system.NativeStart.main(Native Method)
05-21 14:31:23.636: E/AndroidRuntime(9435): Caused by: java.lang.NullPointerException
05-21 14:31:23.636: E/AndroidRuntime(9435): at com.larry.proto.maptest.MainActivity.onCreate(MainActivity.java:47)
05-21 14:31:23.636: E/AndroidRuntime(9435): at android.app.Activity.performCreate(Activity.java:5231)
05-21 14:31:23.636: E/AndroidRuntime(9435): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-21 14:31:23.636: E/AndroidRuntime(9435): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
代码
package com.larry.proto.maptest;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MainActivity extends FragmentActivity implements OnMapClickListener, OnMapLongClickListener {
final int RQS_GooglePlayServices = 1;
private GoogleMap myMap;
Location myLocation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
myMap.setMyLocationEnabled(true);
myMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
@Override
protected void onResume() {
super.onResume();
int resultCode = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(getApplicationContext());
if (resultCode == ConnectionResult.SUCCESS) {
Toast.makeText(getApplicationContext(),
"isGooglePlayServicesAvailable SUCCESS", Toast.LENGTH_LONG)
.show();
} else {
GooglePlayServicesUtil.getErrorDialog(resultCode, this,
RQS_GooglePlayServices);
}
}
@Override
public void onMapClick(LatLng point) {
myMap.animateCamera(CameraUpdateFactory.newLatLng(point));
Toast.makeText(getApplicationContext(), point.toString(),
Toast.LENGTH_LONG).show();
}
@Override
public void onMapLongClick(LatLng point) {
myMap.addMarker(new MarkerOptions()
.position(point)
.title("You are here")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
Toast.makeText(getApplicationContext(),
"New marker added@" + point.toString(), Toast.LENGTH_LONG)
.show();
}
}
答案 0 :(得分:2)
根据你的评论,你有这个
android:name="com.google.android.gms.maps.MapFragment
应该是
android:name="com.google.android.gms.maps.SupportMapFragment
因为你有
myMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
getMap()
返回null。
在检查了Google Play服务的可用性后初始化GoogleMap对象
GoogleMap只能在底层使用getMap()获取 加载了地图系统,并且片段中的基础视图存在。 该类自动初始化地图系统和视图; 但是,因为这样,你就无法保证准备就绪 取决于Google Play服务APK的可用性。如果一个 GoogleMap不可用,getMap()将返回null。
编辑:
看着你的清单
android:minSdkVersion="14"
所以改变这个
public class MainActivity extends FragmentActivity
要
public class MainActivity extends Activity
然后回到
android:name="com.google.android.gms.maps.MapFragment
同时更改此
myMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
到
myMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
您不需要以下
<permission
android:name="com.larry.proto.maptest.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.larry.proto.maptest.permission.MAPS_RECEIVE" />
在onResume
if (resultCode == ConnectionResult.SUCCESS) {
// intialize if success
}
答案 1 :(得分:2)
如果您的minsdk<12
和/或您希望Backward Compability Support
那么
你应该改变这个
<fragment xmlns:android="schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"/>
与
<fragment xmlns:android="schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SuoportMapFragment"/>