找回密碼 或 安全提問
 註冊
|註冊|登錄

伊莉討論區

搜索
發表文章前請先閱讀相關版規尊貴會員無限觀看附件圖片你準備好成為出色的版主了嗎?
mg蘿莉mg鬼父3d中出
juq 651靈壹壹地獄之魂姑姑totte採精qos♠wif

休閒聊天興趣交流學術文化旅遊交流飲食交流家庭事務PC GAMETV GAME
熱門線上其他線上感情感性寵物交流家族門派動漫交流貼圖分享BL/GL
音樂世界影視娛樂女性頻道潮流資訊BT下載區GB下載區下載分享短片
電腦資訊數碼產品手機交流交易廣場網站事務長篇小說體育運動時事經濟
上班一族博彩娛樂

❖四月新番【簡】無名

[簡]被稱為廢物的原英

4月新番[繁體]約會大

[繁]怪人的沙拉碗 - 0

✡ 凡人修仙傳・97・

[繁]關於我轉生變成史
C & C++ 語言C# 語言Visual Basic 語言PHP 語言JAVA 語言
查看: 3285|回復: 1
打印上一主題下一主題

[問題]在android上如何按下一個按鈕然後在相同的Activity顯示google map[複製鏈接]

Rank: 1

帖子
237
積分
198 點
潛水值
26390 米
跳轉到指定樓層
樓主
發表於 2016-7-9 02:20 PM|只看該作者|倒序瀏覽
本帖最後由 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. }
複製代碼


...
瀏覽完整內容,請先 註冊登入會員
分享分享0收藏收藏0支持支持0
若新密碼無法使用,可能是數據未更新。請使用舊密碼看看。

使用道具檢舉

Rank: 6Rank: 6Rank: 6Rank: 6Rank: 6Rank: 6

帖子
178
積分
2188 點
潛水值
41518 米
頭香
發表於 2016-7-16 05:44 PM|只看該作者
Exception 貼出來比 code 貼出來還重要喔。
帶過一些人,寫程式出問題就回頭一直改 code,但卻不肯認真看 Exception。
其實 exception 看完,有六七成的機會就知道問題出在那裡了。

點評

sank910021 謝謝,我大概已經知道問題在哪裡了,我沒用SupportMapFragment去findviewbyid地圖的id,還有SupportMapFragment.getMapAsync(onMapReadyCallBack)  發表於 2016-7-16 08:58 PM
若對尊貴或贊助會員有任何疑問,歡迎向我們查詢。我們的即時通或MSN: admin@eyny.com

使用道具檢舉

您需要登錄後才可以回帖 登錄 | 註冊

Powered by Discuz!

© Comsenz Inc.

重要聲明:本討論區是以即時上載留言的方式運作,對所有留言的真實性、完整性及立場等,不負任何法律責任。而一切留言之言論只代表留言者個人意見,並非本網站之立場,用戶不應信賴內容,並應自行判斷內容之真實性。於有關情形下,用戶應尋求專業意見(如涉及醫療、法律或投資等問題)。 由於本討論區受到「即時上載留言」運作方式所規限,故不能完全監察所有留言,若讀者發現有留言出現問題,請聯絡我們。有權刪除任何留言及拒絕任何人士上載留言,同時亦有不刪除留言的權利。切勿上傳和撰寫 侵犯版權(未經授權)、粗言穢語、誹謗、渲染色情暴力或人身攻擊的言論,敬請自律。本網站保留一切法律權利。
回頂部