伊莉討論區

標題: 在android上如何按下一個按鈕然後在相同的Activity顯示google map [打印本頁]

作者: sank910021    時間: 2016-7-9 02:20 PM     標題: 在android上如何按下一個按鈕然後在相同的Activity顯示google map

本帖最後由 sank910021 於 2016-7-9 02:21 PM 編輯

如題,我希望按下一個button後在同個頁面顯示google map,但是我不管怎麼按我手機都會閃退...
有使用到ViewStub、Google Map API,這兩個以前都沒用過,試著照網路上的教學弄出來也不確定對不對....
還請各位幫我解答,謝謝!

map.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <fragment android:layout_width="wrap_content"
  3.     android:layout_height="wrap_content"
  4.     android:name="com.google.android.gms.maps.MapFragment"
  5.     android:id="@+id/map_fragment"
  6.     xmlns:android="http://schemas.android.com/apk/res/android" />
複製代碼


activity_main.xml:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4.     android:orientation="vertical"
  5.     android:layout_width="match_parent"
  6.     android:layout_height="match_parent"
  7.     android:paddingBottom="@dimen/activity_vertical_margin"
  8.     android:paddingLeft="@dimen/activity_horizontal_margin"
  9.     android:paddingRight="@dimen/activity_horizontal_margin"
  10.     android:paddingTop="@dimen/activity_vertical_margin"
  11.     tools:context="com.example.lincy.googlemaptest.MainActivity">

  12.     <LinearLayout
  13.         android:orientation="vertical"
  14.         android:layout_width="wrap_content"
  15.         android:layout_height="wrap_content">

  16.         <ViewStub
  17.             android:id="@+id/vs_map"
  18.             android:layout_width="wrap_content"
  19.             android:layout_height="wrap_content"
  20.             android:layout="@layout/map"/>

  21.     </LinearLayout>

  22.     <Button
  23.         android:layout_width="wrap_content"
  24.         android:layout_height="wrap_content"
  25.         android:text="@string/map_button"
  26.         android:id="@+id/map_button" />
複製代碼
MainActivity.java:
  1. import android.content.pm.PackageManager;
  2. import android.support.v4.app.ActivityCompat;
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.support.v4.app.FragmentActivity;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.view.ViewStub;
  8. import android.widget.Button;
  9. import android.app.Activity;

  10. import com.google.android.gms.maps.CameraUpdateFactory;
  11. import com.google.android.gms.maps.GoogleMap;
  12. import com.google.android.gms.maps.MapFragment;
  13. import com.google.android.gms.maps.OnMapReadyCallback;
  14. import com.google.android.gms.maps.SupportMapFragment;
  15. import com.google.android.gms.maps.model.LatLng;
  16. import com.google.android.gms.maps.model.MarkerOptions;

  17. public class MainActivity extends Activity implements OnMapReadyCallback {

  18.     GoogleMap map;
  19.     Button map_button;

  20.     @Override
  21.     protected void onCreate(Bundle savedInstanceState) {
  22.         super.onCreate(savedInstanceState);
  23.         setContentView(R.layout.activity_main);

  24.         this.map_button = (Button) findViewById(R.id.map_button);
  25.         this.map_button.setOnClickListener(new View.OnClickListener() {
  26.             @Override
  27.             public void onClick(View view) {
  28.                ViewStub vs = (ViewStub) findViewById(R.id.vs_map);
  29.                vs.inflate();
  30.             }
  31.        });
  32.     }

  33. @Override
  34. public void onMapReady(GoogleMap googleMap) {
  35.     LatLng sydney = new LatLng(-33.867, 151.206);

  36.     if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  37.         // TODO: Consider calling
  38.         //    ActivityCompat#requestPermissions
  39.         // here to request the missing permissions, and then overriding
  40.         //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
  41.         //                                          int[] grantResults)
  42.         // to handle the case where the user grants the permission. See the documentation
  43.         // for ActivityCompat#requestPermissions for more details.
  44.         return;
  45.     }
  46.     map.setMyLocationEnabled(true);
  47.     map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));

  48.     map.addMarker(new MarkerOptions()
  49.             .title("Sydney")
  50.             .snippet("The most populous city in Australia.")
  51.             .position(sydney));
  52.     }


  53. }
複製代碼



作者: Lak4CYUT    時間: 2016-7-16 05:44 PM

Exception 貼出來比 code 貼出來還重要喔。
帶過一些人,寫程式出問題就回頭一直改 code,但卻不肯認真看 Exception。
其實 exception 看完,有六七成的機會就知道問題出在那裡了。




歡迎光臨 伊莉討論區 (http://www86.eyny.com/) Powered by Discuz!