2013-05-04 183 views
0

嗨我跟随此https://developers.google.com/maps/documentation/android/start#installing_the_google_maps_android_v2_api学习使用谷歌地图API。SOLVED_Android:使用API​​谷歌地图崩溃启动应用程序

这是我的清单:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.dude.uniplaces" 
    android:versionCode="1" 
    android:versionName="1.0" > 
    <uses-feature 
     android:glEsVersion="0x00020000" 
     android:required="true"/> 


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

    <permission 
     android:name="com.dude.uniplaces.permission.C2D_MESSAGE" 
     android:protectionLevel="signature" /> 

    <uses-permission android:name="com.dude.uniplaces.permission.C2D_MESSAGE" /> 
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> 
    <uses-permission android:name="android.permission.GET_ACCOUNTS" /> 
    <uses-permission android:name="android.permission.WAKE_LOCK" /> 
    <uses-permission android:name="android.permission.VIBRATE" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 

<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/> 

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

    <permission 
     android:name="com.dude.uniplaces.permission.MAPS_RECEIVE" 
     android:protectionLevel="signature"/> 
<uses-permission android:name="com.dude.uniplaces.permission.MAPS_RECEIVE"/> 
    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <uses-library android:name="com.google.android.maps" />   
     <receiver 
      android:name="com.google.android.gcm.GCMBroadcastReceiver" 
      android:permission="com.google.android.c2dm.permission.SEND" > 
      <intent-filter> 
       <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
       <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> 

       <category android:name="com.dude.uniplaces" /> 
      </intent-filter> 
     </receiver> 

     <service android:name="com.dude.uniplaces.GCMIntentService" /> 

     <activity 
      android:name="com.dude.uniplaces.Index" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name="com.dude.uniplaces.Dude" 
      android:label="dude" > 
     </activity> 
     <activity 
      android:name="com.dude.uniplaces.SendMess" 
      android:label="sendmess" > 
     </activity> 
     <meta-data 
    android:name="com.google.android.maps.v2.API_KEY" 
    android:value="AIzaSyBUO0v3_pHsRXfnGyJ68AeZkCUtHINw6OA"/> 

    </application> 

</manifest> 

这是主要的活动我的XML文件:

<?xml version="1.0" encoding="utf-8"?> 

<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=".Index" > 


    <Button 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:text="Invia Messaggio" 
    android:onClick="sendMex" /> 
    <fragment 
      android:id="@+id/map" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:name="com.google.android.gms.maps.MapFragment"/> 



</RelativeLayout> 

这是我的主要活动:

package com.dude.uniplaces; 
import static com.dude.uniplaces.CommonUtilities.DISPLAY_MESSAGE_ACTION; 
import static com.dude.uniplaces.CommonUtilities.EXTRA_MESSAGE; 
import static com.dude.uniplaces.CommonUtilities.SENDER_ID; 

import java.util.HashMap; 
import java.util.Map; 

import android.app.Activity; 
import android.app.ProgressDialog; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.location.Criteria; 
import android.location.Location; 
import android.location.LocationManager; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.provider.Settings; 
import android.view.View; 
import android.widget.Toast; 

import com.google.android.gcm.GCMRegistrar; 


public class Index extends Activity { 
    ProgressDialog progressBar; 
    AsyncTask<Void, Void, Void> mRegisterTask; 
    @Override 
    public void onCreate(Bundle savedInstanceState){ 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.index); 
     // Phone is ready 
     GCMRegistrar.checkDevice(this); 
     // Checking Manifest 
     GCMRegistrar.checkManifest(this); 
     registerReceiver(mHandleMessageReceiver, new IntentFilter(DISPLAY_MESSAGE_ACTION)); 
     // Ottieni il Registration ID 
     final String regId = GCMRegistrar.getRegistrationId(this); 
     // Controllo se sono registrato 
     if (regId.equals("")) { 
      // Mi registro 
      GCMRegistrar.register(this, SENDER_ID); 
     } else { 
      // Sono registrato 
      if (!GCMRegistrar.isRegisteredOnServer(this)) { 
       // Provo a registrarmi ancora 

       final Context context = this; 
       mRegisterTask = new AsyncTask<Void, Void, Void>() { 
        @Override 
        protected Void doInBackground(Void... params) { 
         ServerUtilities.register(context, regId); 
         return null; 
        } 
        @Override 
        protected void onPostExecute(Void result) { 
         mRegisterTask = null; 
        } 
       }; 
       mRegisterTask.execute(null, null, null); 
      } 
     } 
     /* Here I should be registred */ 
     /* Now is time to take gps coordinates */ 
     LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE); 
     boolean enabled = service.isProviderEnabled(LocationManager.GPS_PROVIDER); 

     // Check if enabled and if not send user to the GSP settings 
     // Better solution would be to display a dialog and suggesting to 
     // go to the settings 

     if (!enabled) { 
      Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
      startActivity(intent); 
     } 


     LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     Map<String, String> params = new HashMap<String, String>(); 
     Criteria criteria = new Criteria(); 
     String provider = locationManager.getBestProvider(criteria, false); 
     Location location = locationManager.getLastKnownLocation(provider); 
     String message="x"; 
     String Id = GCMRegistrar.getRegistrationId(this); 
     if(location != null) 
      message = String.format("%1$s \n%2$s \n%3$s",Id, 
             location.getLongitude(), location.getLatitude() 
           ); 
     if(location == null) 
      params.put("one", "BONAA"); 
     else 
     params.put("one", message); 
     ServerUtilities su = new ServerUtilities(); 
     su.go("http://unipiapp.altervista.org/cord.php",params); 




    } 
    /** 
    * Ricevo notifica push 
    * */ 
    private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      String newMessage = intent.getExtras().getString(EXTRA_MESSAGE); 
      // Sveglia il telefono se è in stand-by 
      WakeLocker.acquire(getApplicationContext()); 
      // Visualizza il messaggio 
      Toast.makeText(getApplicationContext(), "New Message: " + newMessage, Toast.LENGTH_LONG).show(); 
      // Rilascia il wavelocker 
      WakeLocker.release(); 
     } 
    }; 
    @Override 
    protected void onDestroy() { 
     if (mRegisterTask != null) { 
      mRegisterTask.cancel(true); 
     } 
     try { 
      unregisterReceiver(mHandleMessageReceiver); 
      GCMRegistrar.onDestroy(this); 
     } catch (Exception e) { 
     } 
     super.onDestroy(); 
    } 

/* Function call clickin on button */ 
public void sendMex(View w) 
{ 
    Intent intent = new Intent(Index.this, SendMess.class); 
    startActivity(intent); 

} 

} /* END CLASS */ 

我下载谷歌播放服务并将其添加到我的工作区,但是当我尝试在手机中启动应用程序时,它崩溃了:

05-04 20:11:24.441:E/AndroidRuntime(11190):致命异常:主 05-04 20:11:24.441:E/AndroidRuntime(11190): 了java.lang.RuntimeException:无法启动活动 ComponentInfo {com.dude.uniplaces/com.dude.uniplaces.Index}: android.view.InflateException:二进制XML文件行#19:错误 充气类片段

你能帮忙我,请吗?

问候


编辑: 我修改INDEX.XML,你看到了我,然后我说(以为)在正确的方式谷歌库,你可以在武官图像看,但我仍然需要崩溃!

http://img442.imageshack.us/img442/3539/73174342.png


最后编辑时间:

我解决了增加进口android.support.v4.app.FragmentActivity; 和chancing延伸活动与FragmentActivity

感谢所有

回答

0

你分钟SDK在8

注:如果您希望在API 11上运行的应用程序,并降低你需要添加支持库作为好。

http://developer.android.com/tools/extras/support-library.html

使用此

android:name="com.google.android.gms.maps.SupportMapFragment" 

另外,还要确保你遵循了以下链接

https://developers.google.com/maps/documentation/android/start

编辑

所有步骤

MapFragment的API 12和更高的支持片段的API 11,下

另外,还要确保你映射项目引用谷歌播放服务库项目。

请参阅您的谷歌地图中的项目

要检查如果库项目发挥服务。右键点击你的项目转到属性。选择android。检查是否勾选了库。

enter image description here

要引用地图中的项目库项目。

右键单击您的项目转到属性。选择android。点击添加,浏览图书馆项目点击添加并确定。注意下面的绿色勾号。

enter image description here

欲了解更多信息,请查看以下链接

https://developer.android.com/tools/projects/projects-eclipse.html

您还需要添加支持库,因为您正在使用SupportFragment。检查下面的链接

https://developer.android.com/tools/extras/support-library.html

+0

你好感谢你的回答我改变了我的INDEX.XML: 机器人:name =“com.google.android.gms.maps.SupportMapFragment” 但我的应用程序仍然崩溃! 关于谷歌播放服务库项目,我将它添加到我的工作区中,然后将其添加到库(项目,Android,库的属性)中,并在libreries中的'Java Build Path' – Edge7 2013-05-04 19:50:26

+0

@ user2350420中添加google_play_services.jar,您不应将其添加到你的构建路径,你应该只参考库项目。 http://developer.android.com/tools/projects/projects-eclipse.html。你也应该添加支持库http://developer.android.com/tools/extras/support-library.html – Raghunandan 2013-05-05 01:28:24

+0

@ Edge7你尝试了以上吗? – Raghunandan 2013-05-06 13:22:59

0

右键单击该项目并选择Android的工具 - >添加支持库...

和改变这种

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

这个

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

只是为了确认你已经包含谷歌正确播放服务,右键单击项目并选择属性。在左侧标签中选择Android。在右窗格的底部,您应该看到带有标签库的框架。如果谷歌播放正确添加它将显示为绿色复选标记。同时确保谷歌播放项目在工作区中打开。

0

它看起来像你参考google-play-services图书馆的方式有一些问题。看看前三个步骤中,该博客文章中,我就写了Google Maps API V2来获得如何正确地做到这一点的想法:

Google Maps API V2

相关问题