gpt4 book ai didi

android - 我想在 map 上添加标记并缩放到该位置

转载 作者:太空狗 更新时间:2023-10-29 14:18:30 25 4
gpt4 key购买 nike

我正在开发应用程序,我可以在其中查看 map 并在上面放置一些标记并缩放到该位置。

我成功地显示了我的 map View 。但在添加标记和缩放代码后,它被强制关闭...请指导我,我在下面给出了我的代码以及我的日志猫帖子。

package com.appeal.my_map;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;



public class MainActivity extends FragmentActivity {
private GoogleMap googlemap;

double latitude = 17.385044;
double longitude = 78.486671;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

marker();

CameraPosition cameraPosition = new CameraPosition.Builder().target(
new LatLng(17.385044, 78.486671)).zoom(12).build();

googlemap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

}




private void marker() {
// TODO Auto-generated method stub
MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Hello Maps ");
googlemap.addMarker(marker);
googlemap.setMyLocationEnabled(true);
}





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


}

我的 xml 文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

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

</LinearLayout>

日志猫

7561    com.appeal.my_map   AndroidRuntime  FATAL EXCEPTION: main
7561 com.appeal.my_map AndroidRuntime java.lang.RuntimeException: Unable to start activity ComponentInfo{com.appeal.my_map/com.appeal.my_map.MainActivity}: java.lang.NullPointerException
7561 com.appeal.my_map AndroidRuntime at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
7561 com.appeal.my_map AndroidRuntime at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
7561 com.appeal.my_map AndroidRuntime at android.app.ActivityThread.access$600(ActivityThread.java:162)
7561 com.appeal.my_map AndroidRuntime at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
7561 com.appeal.my_map AndroidRuntime at android.os.Handler.dispatchMessage(Handler.java:107)
7561 com.appeal.my_map AndroidRuntime at android.os.Looper.loop(Looper.java:194)
7561 com.appeal.my_map AndroidRuntime at android.app.ActivityThread.main(ActivityThread.java:5371)
7561 com.appeal.my_map AndroidRuntime at java.lang.reflect.Method.invokeNative(Native Method)
7561 com.appeal.my_map AndroidRuntime at java.lang.reflect.Method.invoke(Method.java:525)
7561 com.appeal.my_map AndroidRuntime at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
7561 com.appeal.my_map AndroidRuntime at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
7561 com.appeal.my_map AndroidRuntime at dalvik.system.NativeStart.main(Native Method)
7561 com.appeal.my_map AndroidRuntime Caused by: java.lang.NullPointerException
7561 com.appeal.my_map AndroidRuntime at com.appeal.my_map.MainActivity.marker(MainActivity.java:41)
7561 com.appeal.my_map AndroidRuntime at com.appeal.my_map.MainActivity.onCreate(MainActivity.java:25)
7561 com.appeal.my_map AndroidRuntime at android.app.Activity.performCreate(Activity.java:5122)
7561 com.appeal.my_map AndroidRuntime at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
7561 com.appeal.my_map AndroidRuntime at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
7561 com.appeal.my_map AndroidRuntime ... 11 more

哪里不对请指正...谢谢...!

最佳答案

您还没有初始化 googlemap。您需要初始化相同的

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

关于android - 我想在 map 上添加标记并缩放到该位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19225643/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com