2016-08-21 151 views
0

我正在嘗試使搜索位置功能和地圖類型更改功能在我的android google maps api應用上工作,但是當我點擊與搜索和地圖相關的按鈕時更改應用程序停止運行。 按鈕和功能的佈局很好地實現,我看不到錯誤在哪裏。有誰知道缺少什麼使這兩個功能正常工作?搜索位置和地圖類型功能不起作用

Manifest file: 

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.dborahramos.myapplication"> 

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

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 

     <meta-data 
      android:name="com.google.android.geo.API_KEY" 
      android:value="AIzaSyBVCTEWR6ifov2W_9itWuUfBvptd8MoA7U" /> 

     <activity 
      android:name=".MapsActivity" 
      android:label="@string/title_activity_maps"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

Here is activity maps: 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical"> 

     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"> 

      <EditText 
       android:layout_width="130dp" 
       android:layout_height="wrap_content" 
       android:id="@+id/TFaddress" /> 
      <Button 
       style="?android:attr/buttonStyleSmall" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Search" 
       android:id="@+id/Bsearch" 
       android:layout_gravity="right" 
       android:onClick="onSearch" /> 
      <Button 
       style="?android:attr/buttonStyleSmall" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Map Type" 
       android:id="@+id/Btype" 
       android:layout_gravity="right" 
       android:nestedScrollingEnabled="false" 
       android:onClick="changeType" /> 
     </LinearLayout> 
     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"> 

      <fragment xmlns:android="http://schemas.android.com/apk/res/android" 
       xmlns:tools="http://schemas.android.com/tools" android:layout_width="320dp" 
       android:layout_height="450dp" android:id="@+id/map" tools:context=".MapsActivity" 
       android:name="com.google.android.gms.maps.SupportMapFragment" /> 

      <LinearLayout 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:orientation="vertical"> 
       <Button 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="^" 
        android:id="@+id/Bzoomin" 
        android:onClick="onZoom" /> 

       <Button 
        style="?android:attr/buttonStyleSmall" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="v" 
        android:id="@+id/Bzoomout" 
        android:layout_gravity="center_vertical" 
        android:onClick="onZoom" /> 

      </LinearLayout> 
     </LinearLayout> 
    </LinearLayout> 

And here are search location and map type functions on maps activity.java. 

    public void onSearch(View view) 
     { 
      EditText location_tf = (EditText)findViewById(R.id.TFaddress); 
      String location = location_tf.getText().toString(); 
      List<Address> addressList = null; 
      if(location != null || !location.equals("")) 
      { 
       Geocoder geocoder = new Geocoder(this); 
       try { 
        addressList = geocoder.getFromLocationName(location , 1); 


       } catch (IOException e) { 
        e.printStackTrace(); 
       } 

       Address address = addressList.get(0); 
       LatLng latLng = new LatLng(address.getLatitude() , address.getLongitude()); 
       mMap.addMarker(new MarkerOptions().position(latLng).title("Marker")); 
       mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng)); 

      } 
     } 

    public void changeType(View view) 
    { 
     if(mMap.getMapType() == GoogleMap.MAP_TYPE_NORMAL) 
     { 
      mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE); 
     } 
     else 
      mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
    } 

回答

0

請嘗試檢查您的意圖請求。

正如documentation提到:

爲了推出谷歌地圖與您必須首先創建一個Intent對象的意圖,指定其行動,URI和包裝。

  • 行動:所有的谷歌地圖的意圖被稱爲視圖行動 - ACTION_VIEW
  • URI:Google Maps意圖使用URI編碼的字符串來指定所需的操作以及執行操作的一些數據。
  • :調用setPackage("com.google.android.apps.maps")將確保Android版Google地圖應用處理該意圖。

此外,

要驗證的應用程序可用於接收的意圖,你Intent對象調用resolveActivity()。如果結果非空,至少有一個應用程序可以處理該意圖,並且可以安全地調用startActivity()。如果結果爲空,則不應使用意圖,如果可能,應禁用調用該意圖的功能。

詳細信息和示例請見Google Maps Intents