2013-06-04 282 views
0

我正在開發一個使用谷歌地圖API V2的Android應用程序,應用程序崩潰,我有這個錯誤。Android谷歌地圖API v2

06-04 12:26:31.980: E/AndroidRuntime(16726): FATAL EXCEPTION: main 06-04 12:26:31.980: E/AndroidRuntime(16726): java.lang.NoClassDefFoundError: com.google.android.gms.R$styleable 06-04 12:26:31.980: E/AndroidRuntime(16726): at com.google.android.gms.maps.GoogleMapOptions.createFromAttributes(Unknown Source) 06-04 12:26:31.980: E/AndroidRuntime(16726): at com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown Source) 06-04 12:26:31.980: E/AndroidRuntime(16726): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:279) 06-04 12:26:31.980: E/AndroidRuntime(16726): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676) 06-04 12:26:31.980: E/AndroidRuntime(16726): at android.view.LayoutInflater.inflate(LayoutInflater.java:466) 06-04 12:26:31.980: E/AndroidRuntime(16726): at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 06-04 12:26:31.980: E/AndroidRuntime(16726): at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 06-04 12:26:31.980: E/AndroidRuntime(16726): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256) 06-04 12:26:31.980: E/AndroidRuntime(16726): at android.app.Activity.setContentView(Activity.java:1867) 06-04 12:26:31.980: E/AndroidRuntime(16726): at com.its.android.MainActivity.onCreate(MainActivity.java:29) 06-04 12:26:31.980: E/AndroidRuntime(16726): at android.app.Activity.performCreate(Activity.java:5008) 06-04 12:26:31.980: E/AndroidRuntime(16726): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 06-04 12:26:31.980: E/AndroidRuntime(16726): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 06-04 12:26:31.980: E/AndroidRuntime(16726): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)

我可以用這個代碼 公共類MainActivity擴展FragmentActivity {

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    SupportMapFragment fragment = new SupportMapFragment(); 
    getSupportFragmentManager().beginTransaction() 
      .add(android.R.id.content, fragment).commit(); 

}}

,但是當我嘗試添加標記與此代碼,應用程序崩潰顯示地圖

public class MainActivity extends FragmentActivity { 

     private GoogleMap mMap; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 
      setUpMapIfNeeded(); 
     } 

     @Override 
     protected void onResume() { 
      super.onResume(); 
      setUpMapIfNeeded(); 
     } 

     private void setUpMapIfNeeded() { 

      if (mMap == null) { 

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

       if (mMap != null) { 
        setUpMap(); 
       } 
      } 
     } 

     private void setUpMap() { 
      mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker")); 
     } 

a ctivity_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.SupportMapFragment"/> 

我加入谷歌服務的librarie

AndroidManifest.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.its.android" 
    android:versionCode="1" 
    android:versionName="1.0" > 

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

    <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" /> 

    <permission 
      android:name="com.its.android.permission.MAPS_RECEIVE" 
      android:protectionLevel="signature" /> 
    <uses-permission android:name="com.its.android.permission.MAPS_RECEIVE" /> 

    <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.its.android.SplashActivity" 
         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.its.android.MainActivity" 
         android:label="@string/app_name" /> 

       <uses-library android:name="com.google.android.maps" />  
       <meta-data 
         android:name="com.google.android.maps.v2.API_KEY" 
         android:value="xxxxx"/> 
    </application> 

</manifest> 

回答

0

你是做了錯誤的方式:

從活動佈局文件,你這樣做:

<?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.SupportMapFragment"/> 

所以你定義在XML文件中的地圖片段。

從您要創建代碼映射片段的實例,並manualy加入它的另一方面:

SupportMapFragment fragment = new SupportMapFragment(); 
getSupportFragmentManager().beginTransaction() 
     .add(android.R.id.content, fragment).commit(); 

所以基本上你兩次添加地圖碎片,它們沒有連接一個另一個。

你應該做的另一件事是刪除這一行:

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

這是一個API V1許可,並沒有任何與API V2。

+0

我看到,但這是爲我工作,它崩潰時,我試圖添加一個標記(第二個代碼) – San

+0

最後的解決方案工作對我來說thanx ^^ – San

+0

@San,最後的解決方案爲你工作? –

0

加入谷歌播放服務項目作爲一個庫而不是把jar文件裏面庫夾。

+0

我試過了,但仍然有同樣的問題! – San

0

進口谷歌播放-services_lib作爲一個項目,並在導入選擇複製到工作區

你添加了所有這些權限?

<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" /> 
<!-- 
The following two permissions are not required to use 
Google Maps Android API v2, but are recommended. 
--> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

<permission 
    android:name="YOUR PACKAGE.permission.MAPS_RECEIVE" 
    android:protectionLevel="signature" /> 

<uses-permission android:name="YOUR PACKAGE.permission.MAPS_RECEIVE" /> 
+0

我已經做到了,但我仍然有同樣的錯誤! – San

+0

k你有沒有在庫中加入它?並且您需要將android-support-v4.jar添加到外部罐子中 – prvn

+0

我在庫文件夾 – San

0
import Google-play-services_lib from the android sdk 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.URL; 
import java.util.HashMap; 
import java.util.List; 

import org.json.JSONObject; 

import android.app.Dialog; 
import android.location.Criteria; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 
import android.util.Log; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.Spinner; 

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.SupportMapFragment; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.MarkerOptions; 


public class MainActivity extends FragmentActivity implements LocationListener{ 

    GoogleMap mGoogleMap; 
    Spinner mSprPlaceType; 

    String[] mPlaceType=null; 
    String[] mPlaceTypeName=null; 

    double mLatitude=0; 
    double mLongitude=0; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 


     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main);  

     // Array of place types 
     mPlaceType = getResources().getStringArray(R.array.place_type); 

     // Array of place type names 
     mPlaceTypeName = getResources().getStringArray(R.array.place_type_name); 

     // Creating an array adapter with an array of Place types 
     // to populate the spinner 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, mPlaceTypeName); 

     // Getting reference to the Spinner 
     mSprPlaceType = (Spinner) findViewById(R.id.spr_place_type); 

     // Setting adapter on Spinner to set place types 
     mSprPlaceType.setAdapter(adapter); 

     Button btnFind; 

     // Getting reference to Find Button 
     btnFind = (Button) findViewById(R.id.btn_find); 


     // Getting Google Play availability status 
     int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext()); 


     if(status!=ConnectionResult.SUCCESS){ // Google Play Services are not available 

      int requestCode = 10; 
      Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode); 
      dialog.show(); 

     }else { // Google Play Services are available 

      // Getting reference to the SupportMapFragment 
      SupportMapFragment fragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); 

      // Getting Google Map 
      mGoogleMap = fragment.getMap(); 

      // Enabling MyLocation in Google Map 
      mGoogleMap.setMyLocationEnabled(true); 



      // Getting LocationManager object from System Service LOCATION_SERVICE 
      LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 

      // Creating a criteria object to retrieve provider 
      Criteria criteria = new Criteria(); 

      // Getting the name of the best provider 
      String provider = locationManager.getBestProvider(criteria, true); 

      // Getting Current Location From GPS 
      Location location = locationManager.getLastKnownLocation(provider); 

      if(location!=null){ 
        onLocationChanged(location); 
      } 

      locationManager.requestLocationUpdates(provider, 20000, 0, this); 

      // Setting click event lister for the find button 
      btnFind.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 


        int selectedPosition = mSprPlaceType.getSelectedItemPosition(); 
        String type = mPlaceType[selectedPosition]; 


        StringBuilder sb = new StringBuilder("https://maps.googleapis.com/maps/api/place/nearbysearch/json?"); 
        sb.append("location="+mLatitude+","+mLongitude); 
        sb.append("&radius=5000"); 
        sb.append("&types="+type); 
        sb.append("&sensor=true"); 
        sb.append("&key=AIzaSyDX_1T5Cd826EVg9kIeJVdpWBadtTrBPcY"); 


        // Creating a new non-ui thread task to download Google place json data 
        PlacesTask placesTask = new PlacesTask();         

        // Invokes the "doInBackground()" method of the class PlaceTask 
        placesTask.execute(sb.toString()); 


       } 
      }); 

     }  

    } 

    /** A method to download json data from url */ 
    private String downloadUrl(String strUrl) throws IOException{ 
     Log.d("strUrl",""+strUrl); 
     String data = ""; 
     InputStream iStream = null; 
     HttpURLConnection urlConnection = null; 
     try{ 
       URL url = new URL(strUrl);     


       // Creating an http connection to communicate with url 
       urlConnection = (HttpURLConnection) url.openConnection();     

       // Connecting to url 
       urlConnection.connect();     

       // Reading data from url 
       iStream = urlConnection.getInputStream(); 

       BufferedReader br = new BufferedReader(new InputStreamReader(iStream)); 

       StringBuffer sb = new StringBuffer(); 

       String line = ""; 
       while((line = br.readLine()) != null){ 
         sb.append(line); 
       } 

       data = sb.toString(); 

       br.close(); 

     }catch(Exception e){ 
       Log.d("Exception while downloading url", e.toString()); 
     }finally{ 
       iStream.close(); 
       urlConnection.disconnect(); 
     } 

     return data; 
    }   


    /** A class, to download Google Places */ 
    private class PlacesTask extends AsyncTask<String, Integer, String>{ 

     String data = null; 

     // Invoked by execute() method of this object 
     @Override 
     protected String doInBackground(String... url) { 
      try{ 
       data = downloadUrl(url[0]); 
      }catch(Exception e){ 
       Log.d("Background Task",e.toString()); 
      } 
      return data; 
     } 

     // Executed after the complete execution of doInBackground() method 
     @Override 
     protected void onPostExecute(String result){    
      ParserTask parserTask = new ParserTask(); 

      // Start parsing the Google places in JSON format 
      // Invokes the "doInBackground()" method of the class ParseTask 
      parserTask.execute(result); 
     } 

    } 

    /** A class to parse the Google Places in JSON format */ 
    private class ParserTask extends AsyncTask<String, Integer, List<HashMap<String,String>>>{ 

     JSONObject jObject; 

     // Invoked by execute() method of this object 
     @Override 
     protected List<HashMap<String,String>> doInBackground(String... jsonData) { 

      List<HashMap<String, String>> places = null;    
      PlaceJSONParser placeJsonParser = new PlaceJSONParser(); 

      try{ 
       jObject = new JSONObject(jsonData[0]); 

       /** Getting the parsed data as a List construct */ 
       places = placeJsonParser.parse(jObject); 

      }catch(Exception e){ 
        Log.d("Exception",e.toString()); 
      } 
      return places; 
     } 

     // Executed after the complete execution of doInBackground() method 
     @Override 
     protected void onPostExecute(List<HashMap<String,String>> list){    

      // Clears all the existing markers 
      mGoogleMap.clear(); 

      for(int i=0;i<list.size();i++){ 

       // Creating a marker 
       MarkerOptions markerOptions = new MarkerOptions(); 

       // Getting a place from the places list 
       HashMap<String, String> hmPlace = list.get(i); 

       // Getting latitude of the place 
       double lat = Double.parseDouble(hmPlace.get("lat"));     

       // Getting longitude of the place 
       double lng = Double.parseDouble(hmPlace.get("lng")); 

       // Getting name 
       String name = hmPlace.get("place_name"); 

       // Getting vicinity 
       String vicinity = hmPlace.get("vicinity"); 

       LatLng latLng = new LatLng(lat, lng); 

       // Setting the position for the marker 
       markerOptions.position(latLng); 

       // Setting the title for the marker. 
       //This will be displayed on taping the marker 
       markerOptions.title(name); 
       markerOptions.snippet(vicinity); 

       // Placing a marker on the touched position 
       mGoogleMap.addMarker(markerOptions);    

      }  

     } 

    } 



    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
     mLatitude = location.getLatitude(); 
     mLongitude = location.getLongitude(); 
     Log.d("mLatitude",""+mLatitude); 
     Log.d("mLongitude",""+mLongitude); 
     LatLng latLng = new LatLng(mLatitude, mLongitude); 

     mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
     mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(12)); 

    } 

    @Override 
    public void onProviderDisabled(String provider) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onProviderEnabled(String provider) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
     // TODO Auto-generated method stub 

    } 
} 
0

如果你的活動是由發射器發射的消息? 片段需要默認初始值設定項。

嘗試按意向運行第二個活動。

試試看看這個代碼。

Intent intent = new Intent(this, [map activity].class); 
statrtActivity(intent);