所有活动的导航抽屉 - 我的应用程序不会将地图显示为抽屉中的活动



我正在尝试创建一个使用谷歌地图的安卓应用程序,并试图将其添加到所有活动应用程序的导航抽屉中,但地图没有显示。我正在尝试开发一个使用抽屉在活动之间导航的应用程序,其中一个是使用 Google 地图创建的。地图活动开始,出现一条带有"地图已准备就绪"的 Toast 消息,当我单击灰色屏幕(地图应该出现的位置(时,我收到 Toast 消息告诉我经度和纬度,这意味着地图正在以某种方式工作,在布局的背面。我相信这种情况是由一些错误编写的布局文本引起的,但我无法找到原因,所以我将在此处添加 xml 的文件,也许有人会发现我没有看到的错误。我还尝试将包含地图的片段放在 xml 上,因为我读到这可能是不显示地图的原因,但仍然没有变化。在地图活动中.java我还有一个linearlayout.setVisibility(View.GONE),如果我更改为VISIBLE,它出现在屏幕上,在不存在的地图上。谢谢!

编辑:我尝试了一种简化的方法来显示地图,没有任何其他功能,但情况相同,地图不会加载。清单具有所需的所有权限以及依赖项。我使用了这些文件 : 第一活动.java

package com.example.navigationdrawer2;
import android.content.res.TypedArray;
import android.os.Bundle;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
public class FirstActivity extends BaseActivity implements OnMapReadyCallback {
private String[] navMenuTitles;
private TypedArray navMenuIcons;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync( this);
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
navMenuIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons);
set(navMenuTitles, navMenuIcons);
}
@Override
public void onMapReady(GoogleMap googleMap) {
}
}

Activity_first.xml:

<androidx.drawerlayout.widget.DrawerLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
tools:context="com.example.navigationdrawer2.SecondActivity"
class="com.google.android.gms.maps.SupportMapFragment"/>
</LinearLayout>
</FrameLayout>
<ListView android:id="@+id/left_drawer"
android:layout_width="260dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#e5e5e5"
android:dividerHeight="1dp"
android:background="#d6d6d6"/>
</androidx.drawerlayout.widget.DrawerLayout>

随着FirstActivity扩展BaseActivity,我也将添加该.java。

基本活动.java:

public class BaseActivity extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
protected RelativeLayout _completeLayout, _activityLayout;
private CharSequence mDrawerTitle;
private Menu menuObject;
private CharSequence mTitle;
Toolbar toolbar;
private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drawer);
}
public void set(String[] navMenuTitles, TypedArray navMenuIcons) {
mTitle = mDrawerTitle = getTitle();
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
navDrawerItems = new ArrayList<NavDrawerItem>();
if (navMenuIcons == null) {
for (int i = 0; i < navMenuTitles.length; i++) {
navDrawerItems.add(new NavDrawerItem(navMenuTitles[i]));
}
} else {
for (int i = 0; i < navMenuTitles.length; i++) {
navDrawerItems.add(new NavDrawerItem(navMenuTitles[i],
navMenuIcons.getResourceId(i, -1)));
}
}
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
adapter = new NavDrawerListAdapter(getApplicationContext(),
navDrawerItems);
mDrawerList.setAdapter(adapter);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
setupDrawerToggle();
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.string.menu, 
R.string.app_name 
) {
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(mTitle);
supportInvalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(mDrawerTitle);
supportInvalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
private class SlideMenuClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
displayView(position);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
mDrawerLayout.closeDrawer(mDrawerList);
} else {
mDrawerLayout.openDrawer(mDrawerList);
}
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
return super.onPrepareOptionsMenu(menu);
}
private void displayView(int position) {
switch (position) {
case 0:
Intent intent = new Intent(this, FirstActivity.class);
startActivity(intent);
finish();
break;
case 1:
Intent intent1 = new Intent(this, SecondActivity.class);
startActivity(intent1);
finish();
break;
default:
break;
}
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
mDrawerLayout.closeDrawer(mDrawerList);
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
void setupDrawerToggle(){
mDrawerToggle = new androidx.appcompat.app.ActionBarDrawerToggle(this,mDrawerLayout, R.string.app_name, R.string.app_name);
mDrawerToggle.syncState();
}

}

@Override
protected void onCreate(Bundle 
savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
LayoutInflater inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View contentView = inflater.inflate(R.layout.help, null, false);
mDrawer.addView(contentView, 0); 
}

在创建您的活动时,不要调用 setContentView,而是执行上面的代码片段:

FrameLayout convertView = inflator.inflate(R.layout.child_rows, parent, false);

您必须更改"父"="framLayout",您需要在抽屉活动中添加framLayout并给出参考。

Activity的情况下,您必须使用setContentView而不是Inflater。尝试如下:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
....
}

相关内容

  • 没有找到相关文章

最新更新