2016-12-22 3 views
0

Wir haben verschiedene differnet Aktivitäten von Google Map (Einige mit Tabs, Einige einfache, und einige andere Aktivitäten), So sind wir versuchen, Navigationsschublade in unserer App zu implementieren, Dafür haben wir eine Klasse erstellt, die erweitern ActionBarActivity und hat Schublade, und wir erweitern diese Hauptaktivität in unseren Klassen für die Navigationsschublade, aber wenn wir unsere Anwendung öffnen, ist die angezeigte Karte null. Bitte helfen.Google Karte mit Schublade zeigen

Hier ist unsere Dateien

drawerlist.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); 

} 

Aber jedes Mal wenn ich laufen Anwendung ich erhalte diesen Fehler

 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)  

Was ich versucht: -

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

eine andere Sache, die ich versuchte,

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

Antwort

0

Die Linie mit inflater.inflate tut nichts nützliches dort in dieser Klasse. Das XML-Layout ist nicht geladen und findFragmentById(R.id.map) gibt Ihre Null zurück.

Ich glaube, Sie so etwas wie

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY); 

    setContentView(R.layout.mapload); 

    // Find fragment here 
} 

Oder vielleicht haben Sie vielleicht haben Sie wollte nur setContentView von der übergeordneten Klasse wollte?

Im Grunde sagt der Fehler, dass Ihr <fragment> Tag eine Klasse fehlt.

<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" /> 

Zum Beispiel 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"/> 

(auch erwähnenswert, dass xmlns:android nur an der Wurzel XML-Element angewendet werden soll)


Obwohl, ich glaube nicht, Sie brauchen eine ganze separate Aktivität oder Layout.

Legen Sie das Fragment der Karte in diese FrameLayout

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

wie jede normale Fragment ...

SupportMapFragment mapFragment = new SupportMapFragment(); 

getSupportFragmentManager() 
    .beginTransaction() 
    .replace(R.id.content, mapFragment) 
    .commit(); 

mapFragment.getMapAsync(this); 
+0

Vielen Dank für Ihre Antwort, die ich. Klasse in Fragment hinzugefügt und setcontentview verwenden Also mein neuer Code ist http://pastebin.com/aSw9DXww Aber jetzt bekomme ich diesen Fehler java.lang.IllegalArgumentException: Keine Ansicht für ID 0x7f0d0093 (com.keepAeye.gps: id/content) für das Fragment SupportMapFragment {b182300 # 1 id = 0x7f0d0093} –

+2

Sie brauchen 'setContentView (R.layout.mapload)' nicht oder wenn Sie das tun, dann sind Sie mi ssing '@ id/content', ja. Schauen Sie sich einfach Ihre XML-Dateien an. 'drawlistlist.xml' ist wo das' FrameLayout' mit der ID liegt, nicht in 'mapload.xml' –

+1

Mit anderen Worten, wenn Sie' SetContentView' auf 'MyLocation' setzen, verlieren Sie die gesamte NavigationView. –

0

diese Zeile in die xml hinzu: android: name = "com.google .android.gms.Maps.SupportMapFragment“ wie folgt aus:

<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"/> 

und der beste Weg, Google-Map zu implementieren mit Schublade Fragment zu verwenden ist, ist das bedeutet, dass Ihre Klasse ändern‚MyLocation‘, um sich von Fragmente

Verwandte Themen