【问题标题】:Show google map with drawer用抽屉显示谷歌地图
【发布时间】:2016-12-22 19:07:06
【问题描述】:

我们有不同的谷歌地图活动(一些带有标签,一些简单,还有一些其他活动),所以我们正在尝试在我们的应用程序中实现导航抽屉,为此我们创建了一个扩展 ActionBarActivity 并具有抽屉的类,并且我们在导航抽屉类中扩展了这个主要活动,但是当我们打开我们的应用程序时,它的显示地图为空。 请帮忙。

这是我们的文件

抽屉列表.xml

 <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <android.support.v7.widget.Toolbar
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#FA0"
            android:id="@+id/toolbar"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            />
        <FrameLayout
            android:id="@+id/content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
        <android.support.design.widget.NavigationView
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:id="@+id/shitstuff"
            app:itemTextColor="#000"
            app:menu="@layout/drawermenu"
            android:layout_marginTop="-24dp"
            />
    </android.support.v4.widget.DrawerLayout>

mapload.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#ffc17540"
        android:id="@+id/maptab"

        >
            <fragment xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/map"
                android:layout_width="fill_parent"

                android:gravity="bottom"
                android:layout_height="fill_parent"
               />


            <RelativeLayout
                android:id="@+id/slidingContainer"
                android:layout_width="fill_parent"
                android:gravity="bottom"

                android:layout_height="fill_parent">
                <LinearLayout
                    android:id="@+id/t"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:background="#ffff773c"

                    android:orientation="horizontal">
                <RadioGroup
                    android:id="@+id/rg_views"
                    android:orientation="horizontal"
                    android:gravity="bottom"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentEnd="true">

                    <RadioButton
                        android:id="@+id/rb_normal"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/str_rb_normal"
                        android:checked="true" />

                    <RadioButton
                        android:id="@+id/rb_satellite"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/str_rb_satellite" />

                    <RadioButton
                        android:id="@+id/rb_terrain"
                        android:layout_width="161dp"
                        android:layout_height="wrap_content"
                        android:text="@string/str_rb_terrain" />

                </RadioGroup>
                <HorizontalScrollView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                    <LinearLayout
                        android:id="@+id/inhorizontalscrollview"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal">

                        <ImageButton
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:id="@+id/imageButton"
                            android:src="@android:drawable/ic_input_add" />

                    </LinearLayout>
                </HorizontalScrollView>
                </LinearLayout>
                <View
                    android:id="@+id/transparentView"
                    android:visibility="gone"
                    android:layout_width="fill_parent"
                    android:layout_height="@dimen/map_height"
                    android:layout_alignParentTop="true"/>

            </RelativeLayout>

    </RelativeLayout>

BaseActivity.java

public class BaseActivity extends ActionBarActivity
    {
        public DrawerLayout mDrawerLayout;
        private ActionBarDrawerToggle mDrawerToggle;

        @Override
        protected void onCreate(Bundle savedInstanceState)
        {

            super.onCreate(savedInstanceState);

            setContentView(R.layout.drawer_list);
         //   Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
           // setSupportActionBar(toolbar);
            // You were missing this setHomeAsUpIndicator
            getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_navigation_drawer);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);

            mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
            NavigationView n = (NavigationView) findViewById(R.id.shitstuff);

            mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout , R.string.drawer_open, R.string.drawer_close)
            {
                /*
                 * Called when a drawer has settled in a completely closed state
                 */
                public void onDrawerClosed(View view)
                {
                    Log.d("drawerToggle", "Drawer closed");
                    super.onDrawerClosed(view);
                 //   getActionBar().setTitle(R.string.app_name);
                    invalidateOptionsMenu(); //Creates call to onPrepareOptionsMenu()
                }

                /*
                 * Called when a drawer has settled in a completely open state
                 */
                public void onDrawerOpened(View drawerView)
                {
                    Log.d("drawerToggle", "Drawer opened");
                    super.onDrawerOpened(drawerView);
    //                getActionBar().setTitle("NavigationDrawer");
                    invalidateOptionsMenu();
                }
            };
            mDrawerLayout.setDrawerListener(mDrawerToggle);



            n.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(MenuItem menuItem) {
                    switch (menuItem.getItemId()) {
                        ////.......

                    }
                    mDrawerLayout.closeDrawers();  // CLOSE DRAWER
                    return true;
                }
            });

        }


        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
                // THIS IS YOUR DRAWER/HAMBURGER BUTTON
                case android.R.id.home:
                    mDrawerLayout.openDrawer(GravityCompat.START);  // OPEN DRAWER
                    return true;

            }
            return super.onOptionsItemSelected(item);
        }
    }

Mylocation.java

public class MyLocation  extends BaseActivity implements View.OnClickListener,OnMapReadyCallback {
      GoogleMap googleMap;
 @Override
public void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);

    super.onCreate(savedInstanceState);
     LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService
            (Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.mapload, null, false); // line no-74
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

}

但每次我运行应用程序时都会收到此错误

       java.lang.RuntimeException: Unable to start activity ComponentInfo{MyLocation}: android.view.InflateException: Binary XML file line #24: Binary XML file line #24: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3254)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350)
at android.app.ActivityThread.access$1100(ActivityThread.java:222)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7237)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: android.view.InflateException: Binary XML file line #24: Binary XML file line #24: Error inflating class fragment
at android.view.LayoutInflater.inflate(LayoutInflater.java:551)
at android.view.LayoutInflater.inflate(LayoutInflater.java:429)
at com.myapp.gps.MyLocation.onCreate(MyLocation.java:74)                   //error here
at android.app.Activity.performCreate(Activity.java:6876)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3207)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350) 
at android.app.ActivityThread.access$1100(ActivityThread.java:222) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:158) 
at android.app.ActivityThread.main(ActivityThread.java:7237) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
Caused by: android.view.InflateException: Binary XML file line #24: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:788)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:847)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)
at android.view.LayoutInflater.inflate(LayoutInflater.java:527)
 at android.view.LayoutInflater.inflate(LayoutInflater.java:429) 
at com.myapp.gps.MyLocation.onCreate(MyLocation.java:74)                   //error here
at android.app.Activity.performCreate(Activity.java:6876) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135) 

我尝试了什么:-

  inflater.from(getApplicationContext()).inflate(R.layout.mapload, pageHolder, true);
  GoogleMap mapFragment =((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();   //getting null pointer exception here

我尝试过的另一件事

  getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, mapFragment).commit(); //same null pointer exception

【问题讨论】:

    标签: java android google-maps


    【解决方案1】:

    inflater.inflate 的行在该类中没有任何用处。 XML 布局未加载,findFragmentById(R.id.map) 返回您的空值。

    我想你想要类似的东西

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
    
        setContentView(R.layout.mapload);
    
        // Find fragment here
    }
    

    或者您可能只是想从父类获得setContentView

    基本上,错误是说您的&lt;fragment&gt; 标签缺少一个类。

    <fragment    
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/map"
        android:layout_width="fill_parent"
        <!-- Needs a class or name attribute -->
        android:gravity="bottom"
        android:layout_height="fill_parent" />
    

    例如,from the documentation

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

    (还值得一提的是,xmlns:android 仅适用于根 xml 元素)


    不过,我认为您不需要一个完全独立的 Activity 或布局。

    将地图片段加载到这个 FrameLayout 中

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    

    就像任何常规片段一样......

    SupportMapFragment mapFragment = new SupportMapFragment();
    
    getSupportFragmentManager()
        .beginTransaction()
        .replace(R.id.content, mapFragment)
        .commit();
    
    mapFragment.getMapAsync(this);
    

    【讨论】:

    • 感谢您的回复。我在片段中添加了类,还使用了 setcontentview。所以我的新代码是pastebin.com/aSw9DXww 但现在我收到此错误 java.lang.IllegalArgumentException: No view found for id 0x7f0d0093 (com.keepAeye.gps:id/content) for fragment SupportMapFragment{b182300 #1 id=0x7f0d0093}
    • 你不需要setContentView(R.layout.mapload),或者如果你需要,那么你就缺少@id/content,是的。只需查看您的 XML 文件。 drawerlist.xml 是带有 ID 的 FrameLayout 所在的位置,而不是在 mapload.xml
    • 换句话说,如果你在MyLocation 上使用setContentView,你会丢失整个NavigationView。
    • 太棒了,我试图从过去 2 天开始解决这个问题。谢谢,它正在工作。但我还有一个查询,如您所见,我在 mapload.xml 上有单选按钮和其他属性,所以如果我不能使用 setContentView,那么我如何访问这些属性?
    • 意思是,我们如何访问布局特定的属性?
    【解决方案2】:

    将此行添加到您的 xml: android:name="com.google.android.gms.maps.SupportMapFragment" 像这样:

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:layout_width="fill_parent"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:gravity="bottom"
    android:layout_height="fill_parent"/>
    

    使用抽屉实现谷歌地图的最佳方法是使用片段,这意味着将您的类“MyLocation”更改为从片段扩展

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-18
      • 2014-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多