gpt4 book ai didi

java - 错误 : class MapPane is public, 应在名为 MapPane.java 的文件中声明

转载 作者:行者123 更新时间:2023-12-01 16:49:58 25 4
gpt4 key购买 nike

我使用此代码创建 map 和标记,但当我运行它时,它显示空白 map 和错误

我的代码来自 - https://developers.google.com/maps/documentation/android-api/

ContactFragment.java

public class ContactFragment extends Fragment implements OnMapReadyCallback {

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

MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.mapView);
mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap map) {
LatLng sydney = new LatLng(-33.867, 151.206);

map.setMyLocationEnabled(true);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));

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

fragment_contact.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="layout.ContactFragment"
android:background="@color/colorWhite"
android:id="@+id/contact_wrapper">

<!-- TODO: Update blank fragment layout -->

<com.google.android.gms.maps.MapView
android:layout_width="match_parent"
android:id="@+id/mapView"
android:layout_height="200dp" />

<TextView
android:text="Address - 1526 Petchaburi Rd. Makkasan Rajdhevee Bangkok 10400 Thailand"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView6"
android:layout_below="@+id/mapView"
android:layout_alignParentStart="true"
android:layout_marginTop="18dp"
android:padding="10dp"
android:textSize="18sp"
android:textStyle="normal|bold" />

<TextView
android:text="Tel - +66-2652-7477-80, Fax +66-2652-7777"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView7"
android:padding="10dp"
android:layout_below="@+id/textView3"
android:layout_alignParentStart="true"
android:textSize="18sp"
android:textStyle="normal|bold"
android:fontFamily="sans-serif" />

<TextView
android:text=" EMail - sdschool@sd.ac.th"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView3"
android:padding="10dp"
android:textSize="18sp"
android:layout_below="@+id/textView6"
android:layout_alignParentStart="true"
android:textStyle="normal|bold" />

</RelativeLayout>

这是我遇到的错误 enter image description here

最佳答案

使用下面的代码。

public class ContactFragment extends Fragment implements OnMapReadyCallback {

View rootView

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

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


rootView =inflater.inflate(R.layout.content_main, container, false);

MapFragment mapFragment = (MapFragment) getFragmentManager()
rootView.findFragmentById(R.id.mapView);
mapFragment.getMapAsync(this);

return rootView;

}
}

关于java - 错误 : class MapPane is public, 应在名为 MapPane.java 的文件中声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42483006/

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