我已经创建了一个基本的Android应用程序,该应用程序的目的是拥有一个带有汉堡包抽屉的地图。每个汉堡包菜单选项都将在GoogleMap对象上运行对象方法。
问题在于,当汉堡包抽屉被创建并运行时,地图的片段被放置在正确的位置,主要活动中的onCreate和随后的onMapReady函数不会运行,这意味着永远不会创建GoogleMap对象。我在这些函数中添加了一些打印语句,以确保它们永远不会打印出来。
我有两个类,HamburgerDrawer.java和MainActivity.java。
HamburgerDrawer.java:
package com.vanleusen.brighthelp;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import com.google.android.gms.common.api.GoogleApiClient;
public class HamburgerDrawer extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
//testing
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hamburger_drawer);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
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);
/*FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.frame_container, new MyFragment());
transaction.commit();*/
//MainActivity map = new MainActivity();
}
@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.hamburger_drawer, 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.nav_roadmap) {
System.out.println("Roadmap");
//mapData.mapTypeInterface(1);
} else if (id == R.id.nav_satellite) {
System.out.println("Satellite");
//mapData.mapTypeInterface(2);
} else if (id == R.id.nav_hybrid) {
System.out.println("Hybrid");
//mapData.mapTypeInterface(3);
} else if (id == R.id.nav_terrain) {
System.out.println("Terrain");
//mapData.mapTypeInterface(4);
} else if (id == R.id.nav_problems) {
System.out.println("Report Problems");
} else if (id == R.id.nav_contact) {
System.out.println("Contact Us");
} else if (id == R.id.nav_about) {
System.out.println("About");
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
MainActivity.java:
package com.vanleusen.brighthelp;
/**
* Created by Oscar on 22/10/2016.
*/
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MainActivity extends Activity implements OnMapReadyCallback {
private GoogleMap mMap;
public static MapFragment mapFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_hamburger_drawer);
mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
System.out.println("Ran onCreate method");
}
@Override
public void onMapReady(GoogleMap googleMap) {
System.out.println("Ran onMapReady method");
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
我有多个布局xml文件来实现汉堡包菜单,虽然包含抽屉内容的相关文件在这里:
content_hamburger_drawer.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/content_hamburger_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.vanleusen.brighthelp.HamburgerDrawer"
tools:showIn="@layout/app_bar_hamburger_drawer">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
<view
android:id="@+id/myview"
android:layout_width="match_parent"
android:layout_height="fill_parent"
class="android.view.View" />
</FrameLayout>
</RelativeLayout>
最后我听说这可能与我的AndroidManifest有关,虽然我尝试了一些修正似乎对我没什么用,我把它包含在下面:
的AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.vanleusen.brighthelp">
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but you must specify either coarse or fine
location permissions for the 'MyLocation' functionality.
-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".HamburgerDrawer"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
launchMode ensures intents don't create multiple instances of it
<activity
android:name=".MainActivity"
android:label="Map"
android:theme="@style/AppTheme.NoActionBar"
android:launchMode="singleTask" >
</activity>
<!--
The API key for Google Maps-based APIs is defined as a string resource.
(See the file "res/values/google_maps_api.xml").
Note that the API key is linked to the encryption key used to sign the APK.
You need a different API key for each encryption key, including the release key that is used to
sign the APK for publishing.
You can define the keys for the debug and release targets in src/debug/ and src/release/.
-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
</manifest>
如果有人可以帮我解决为什么我的地图的onCreate和onMapReady函数没有被执行,我会非常感激。
答案 0 :(得分:0)
我认为你不会在任何地方调用MainActivity类,这就是为什么会出现这个问题。请在onCreate方法或任何clicklistener中从HamburgerDrawer类中调用它。
Intent a=new Intent(getApplicationContext,MainActivity.class);
startActivity(a);