【问题标题】:error running google maps api v2运行谷歌地图 api v2 时出错
【发布时间】:2014-01-23 07:05:35
【问题描述】:

我正在使用 google play services r14,但无法在任何设备上运行该应用。

01-23 12:07:37.309: E/AndroidRuntime(7056): FATAL EXCEPTION: main
01-23 12:07:37.309: E/AndroidRuntime(7056): java.lang.RuntimeException: Unable to start     activity ComponentInfo{com.example.mapstest/com.example.mapstest.MainActivity}: java.lang.NullPointerException
01-23 12:07:37.309: E/AndroidRuntime(7056):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2114)
01-23 12:07:37.309: E/AndroidRuntime(7056):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2139)
01-23 12:07:37.309: E/AndroidRuntime(7056):     at android.app.ActivityThread.access$700(ActivityThread.java:143)
01-23 12:07:37.309: E/AndroidRuntime(7056):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
01-23 12:07:37.309: E/AndroidRuntime(7056):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-23 12:07:37.309: E/AndroidRuntime(7056):     at android.os.Looper.loop(Looper.java:137)
01-23 12:07:37.309: E/AndroidRuntime(7056):     at android.app.ActivityThread.main(ActivityThread.java:4963)
01-23 12:07:37.309: E/AndroidRuntime(7056):     at java.lang.reflect.Method.invokeNative(Native Method)
01-23 12:07:37.309: E/AndroidRuntime(7056):     at java.lang.reflect.Method.invoke(Method.java:511)
01-23 12:07:37.309: E/AndroidRuntime(7056):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
01-23 12:07:37.309: E/AndroidRuntime(7056):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
01-23 12:07:37.309: E/AndroidRuntime(7056):     at dalvik.system.NativeStart.main(Native Method)
01-23 12:07:37.309: E/AndroidRuntime(7056): Caused by: java.lang.NullPointerException
01-23 12:07:37.309: E/AndroidRuntime(7056):     at com.example.mapstest.MainActivity.onCreate(MainActivity.java:25)
01-23 12:07:37.309: E/AndroidRuntime(7056):     at android.app.Activity.performCreate(Activity.java:5184)
01-23 12:07:37.309: E/AndroidRuntime(7056):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
01-23 12:07:37.309: E/AndroidRuntime(7056):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2078)
01-23 12:07:37.309: E/AndroidRuntime(7056):     ... 11 more

我的 Mainfest.xml 代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mapstest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
    android:minSdkVersion="16"
    android:targetSdkVersion="18" />    
 <permission
    android:name="com.example.mapstest.permission.MAPS_RECEIVE"
    android:protectionLevel="signature"></permission>
<uses-permission
    android:name="com.example.mapstest.permission.MAPS_RECEIVE"/>
<uses-permission        android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<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"/>

<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"
    android:debuggable="true" >
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="api_key"/>
    <meta-data android:name="com.google.android.gms.version"
       android:value="4132500" />
    <activity
        android:name="com.example.mapstest.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>

MainActivity.java:

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

import android.os.Bundle; 
import android.app.Activity; 
import android.support.v4.app.FragmentActivity;
import android.view.Menu;

public class MainActivity extends FragmentActivity {

private static GoogleMap mMap; 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);            
    mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();       
    mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);         
    final LatLng CIU = new LatLng(35.21843892856462, 33.41662287712097);         
    Marker ciu = mMap.addMarker(new MarkerOptions()                                   
    .position(CIU).title("My Office"));     
}

@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;
}

activity_main.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"    
android:paddingBottom="@dimen/activity_vertical_margin"    
android:paddingLeft="@dimen/activity_horizontal_margin"    
android:paddingRight="@dimen/activity_horizontal_margin"    
android:paddingTop="@dimen/activity_vertical_margin"    
tools:context=".MainActivity" >
<TextView        
android:layout_width="wrap_content"        
android:layout_height="wrap_content"        
android:text="@string/hello_world" />
<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 服务版本而发生的吗?如果是这个原因,如何将 google play service revision 14 更改为 10? 有什么解决办法吗?

【问题讨论】:

  • 发布 manifest.xml
  • 发布您的 MainActivity
  • google play service 第 14 版有什么原因吗?
  • 我不认为。您的 MainActivity.java:25 中有空引用
  • mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);这段代码在第 25 行

标签: java android eclipse google-play-services google-maps-api-2


【解决方案1】:

使用这样的东西:

   1. Update the google play services in the SDK. 

  2.  Manually uninstall the App from device and restart the device. i have tried it and   its going perfectly bro   

Also do one thing get the new api key to edited the new sh1 code from https://code.google.com/apis/console/    
you can get your sh1 code from window- preference-android-buid    

【讨论】:

    【解决方案2】:

    您的 mMap 为空。尝试在布局中使用 SupportMapFragment 并在代码中使用 getSupportFragmentManager(),如下所示:

        <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="com.google.android.gms.maps.SupportMapFragment"/>
    

    还有这段代码:

        mMap = ((SupportMapFragment)getSupportFragmentManager()
        .findFragmentById(R.id.map)).getMap();
    

    【讨论】:

    【解决方案3】:

    在您的 Android 清单中您没有指定

    <uses-library
        android:name="com.google.android.maps" />
    

    使用 android:name 代替 android:class。从而改变布局中的片段,如下所示

    <fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.google.android.gms.maps.SupportMapFragment"/>
    

    并在你的java文件中使用它来定义mMap

     mMap = ((SupportMapFragment)getSupportFragmentManager()
    .findFragmentById(R.id.map)).getMap();
    

    【讨论】:

      猜你喜欢
      • 2013-02-15
      • 2013-04-16
      • 1970-01-01
      • 2014-01-15
      • 1970-01-01
      • 2011-03-25
      • 1970-01-01
      相关资源
      最近更新 更多