我试图在另一个片段中显示一个谷歌地图片段,但是我不能操纵地图,我已经把我看到的所有步骤都留档了,但是我不能操纵我的地图。
XML代码:
<FrameLayout 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=".FragmentoPrincipalUsuario">
<!-- TODO: Update blank fragment layout -->
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|top">
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="391dp"
class="com.google.android.gms.maps.MapFragment"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginTop="60dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="149dp"
android:layout_gravity="center_horizontal|bottom">
<Button
android:layout_width="117dp"
android:layout_height="wrap_content"
android:text="pedir taxi"
android:id="@+id/pedirTaxi"
android:layout_gravity="center_horizontal" />
</LinearLayout>
类片段代码:
public class FragmentoPrincipalUsuario extends Fragment implements OnMapReadyCallback {
private OnFragmentInteractionListener mListener;
private List<ParseUser> mChoferes;
private ParseUser mUsuarioActual;
private Button mPedido;
private GoogleMap map;
public FragmentoPrincipalUsuario() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//conseguir usuario actual
// mUsuarioActual = ParseUser.getCurrentUser();
View x = inflater.inflate(R.layout.fragmento_principal_usuario, container, false);
//evento boton pedir taxi
mPedido.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo("tipoUsuario", "chofer");
query.findInBackground(new FindCallback<ParseUser>() {
public void done(List<ParseUser> choferes, com.parse.ParseException e) {
if (e == null) {
mChoferes = choferes;
String nombreChofer;
for (ParseUser chofer : mChoferes) {
nombreChofer = chofer.getUsername();
ParseObject gameScore = new ParseObject("Viaje");
gameScore.put("cliente", "hola");
gameScore.put("chofer", nombreChofer);
gameScore.put("tomado", false);
gameScore.saveInBackground();
}
} else {
// Something went wrong.
}
}
});
}
});
// Inflate the layout for this fragment
return x;
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
@Override
public void onMapReady(GoogleMap map) {
LatLng sydney = new LatLng(-27.7933121, -64.2574857);
map.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
map.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p/>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
主要活动类别代码:
public class DrawerPrincipal extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener,FragmentoPrincipalUsuario.OnFragmentInteractionListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawer_principal);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//cargar fragment principal usuario
Fragment fragmento= null;
fragmento = new FragmentoPrincipalUsuario();
getSupportFragmentManager().beginTransaction()
.replace(R.id.content_main, fragmento)
.commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.pantalla_principal_usuario, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.cuenta) {
// Handle the camera action
} else if (id == R.id.historial_viajes) {
} else if (id == R.id.contacto) {
} else if (id == R.id.compartir) {
} else if (id == R.id.version) {
} else if (id == R.id.salir) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
@Override
public void onFragmentInteraction(Uri uri) {
}
}
在调用getMapAsync()后,编辑以下堆栈跟踪:
空指针错误:
12-18 19:51:50.056 11265-11265/ar.com.taxiexpress.taxiexpressE/AndroidRuntime: FATAL EXCEPTION: mainjava.lang.RuntimeExcture:无法启动活动ComponentInfo{ar.com.taxiexpress.taxiexpress/ar.com.taxiexpress.taxiexpress.DrawerPrincipal}:java.lang.在android.app.ActivityThread.perform启动活动(活动Thread.java:2180)在android.app.活动Thread.handle启动活动(活动Thread.java:2230)在android.app.活动Thread.access600美元(活动Thread.java:141)在android.app.活动线程$H. handleMessage(ActivityThread.java:1234)在android.os.Handler.dispatchMessage(Handler.java:99)在android.os.Looper.loop(Loop. java: 137)在android. app.ActivityThread. main(ActivityThread. java: 5041)在java. lang.反射。方法。方法调用(方法. java: 511)在com. android. interal. os。ZygoteInit$method odAndArgsCaller. run(ZygoteInit. java: 793)在com. android. interal. os。在dalvik. system上的ZygoteInit. main(ZygoteInit. java: 560)。NativeStart. main(本地方法)引起:java. lang.在ar. com. taxiExpress. taxiExpress.在android. support. v4. app.上创建视图(FragmentoPrindalStuario. java: 61)。Fragment. performCreateView(Fragment. java: 1962)在android. support. v4. app.FragmentManagerImp. moveToState(FragmentManager. java: 1067)at android. support. v4. app.FragmentManagerImp. moveToState(FragmentManager. java: 1248)at android. support. v4. app.在android. support. v4. app上运行(BackStackRecord. java: 738)。在android. support. v4. app上的FragmentManagerImp. expendingActions(FragmentManager. java: 1613)。在android. support. v4. app上的FragmentController. expendingActions(FragmentController. java: 330)。在android. app. onStart(FragmentActive. java: 547)。在android. app.在android. app.在android. app上的ActivityThread. perform解雇活动(ActivityThread. java: 2153)。在android. app.在android. app访问600美元(ActivityThread. java: 141)。ActivityThread$H. handleMessage(ActivityThread. java: 1234)at android. os.在android. os.在android. app.ActivityThread. main(ActivityThread. java: 5041)在java. lang.反射。方法。方法调用(方法. java: 511)在com. android. interal. os。ZygoteInit$method odAndArgsCaller. run(ZygoteInit. java: 793)在com. android. interal. os。在dalvik. system上的ZygoteInit. main(ZygoteInit. java: 560)。NativeStart. main(本机方法)
看起来主要的问题是您没有调用getMapAsync()。
由于您使用的是片段内部的嵌套支持MapFra402,因此您需要使用getChildFragmentManager()
来获取Map碎片,然后调用getMapAsync()
。
SupportMapFragment mapFragment = ((SupportMapFragment) getChildFragmentManager()
.findFragmentById(R.id.map));
mapFragment.getMapAsync(this);
但是,虽然这样做可行,但最好让片段扩展SupportMapFragment,这样就不需要嵌套片段:
public class MyMapFragment extends SupportMapFragment
implements OnMapReadyCallback {
private GoogleMap mMap;
private Marker marker;
public MyMapFragment () {
}
@Override
public void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
if (mMap == null) {
getMapAsync(this);
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
setUpMap();
}
private void setUpMap() {
mMap.setMyLocationEnabled(true);
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
mMap.getUiSettings().setMapToolbarEnabled(false);
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng point) {
//remove previously placed Marker
if (marker != null) {
marker.remove();
}
//place marker where user just clicked
marker = mMap.addMarker(new MarkerOptions().position(point).title("Marker")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)));
}
});
}
}
我是xamarin的新手,我需要在应用程序中放置谷歌地图。 我已经从xamarin下载了示例: https://github.com/xamarin/monodroid-samples/tree/master/MapsAndLocationDemo_v2 经过一些改变,它工作了,这个项目显示地图。 以下是工作的项目的代码: /MainActivity.cs/ /BasicDemo.axml/ /A
情况是我使用Fragments在main活动中显示2个视图,这意味着我有两个选项卡,而在第二个选项卡中,我使用以下代码显示谷歌地图 这是第二个选项卡,我想自定义地图,为此我在上面的函数中使用了这段代码。 imap=SupportMapFragment)getSupportFragmentManager()。findFragmentById(R.id.map))。getMap(); imap是在创建
我在我的应用程序中实现了一个谷歌地图片段,用户可以通过点击右上角的“指南针”按钮看到他们当前的位置。
我对Android开发真的是一个新手,有几天我会尝试在我的应用程序中添加一个地图。我尝试了一些事情,但没有成功。如果有人能帮我,我很感激。 我做过的事: 将google-play-services_lib添加到我的包资源管理器中。 包含了android-support-v4.jar和google-play-services.jar作为我的项目的依赖项。 下面是main.xml文件 这里是清单文件
我的主要活动是片段,其中一个片段是Map Fragment。现在,当我点击那里时,每次它都会慢慢打开。我正在使用fragment的单例实例,但它仍然不能像预期的那样工作。下面是这门课的一个好例子:
我有一个片段,它是Viewpager的一部分,我想在片段中使用Google Map V2。这就是我到目前为止所尝试的, 在我的片段中, 在我的布局子片段信息中。xml,我有, 我现在能看到地图了。但是标记没有显示出来。我认为我的谷歌地图mMapView是空的。请帮我解决这个问题。提前感谢。