2016-02-19 63 views
2

我嘗試在片段選項卡中添加映射。我的谷歌地圖通常有效。但是當我使用的地圖選項卡片段的孩子裏面,然後拿到例外:嘗試在fragement內添加映射時出錯TAB

android.view.InflateException: Binary XML file line #6: Error inflating class fragment 

我想在TAB2添加地圖

enter image description here

MainActivity.java

import android.os.Bundle; 
import android.support.design.widget.TabLayout; 
import android.support.v4.view.ViewPager; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.widget.TextView; 

public class MainActivity extends AppCompatActivity { 
    private Toolbar toolbar; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

    toolbar = (Toolbar) findViewById(R.id.app_bar); 
    toolbar.setTitle(""); 
    TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title); 
    mTitle.setText("Chili's"); 
    setSupportActionBar(toolbar); 

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout); 
    tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_restaurant_white).setText("Menu")); 
    tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_place_white).setText("Map"));; 
    tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_thumb_up_white).setText("Facebook")); 
    tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_info_white).setText("About")); 
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL); 

    final ViewPager viewPager = (ViewPager) findViewById(R.id.pager); 
    final PagerAdapter adapter = new PagerAdapter 
      (getSupportFragmentManager(), tabLayout.getTabCount()); 
    viewPager.setAdapter(adapter); 
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); 
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { 
     @Override 
     public void onTabSelected(TabLayout.Tab tab) { 
      viewPager.setCurrentItem(tab.getPosition()); 
     } 

     @Override 
     public void onTabUnselected(TabLayout.Tab tab) { 

     } 

     @Override 
     public void onTabReselected(TabLayout.Tab tab) { 

     } 
    }); 
} 

}

activity_maps.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 

android:id="@+id/home_root" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical"> 

<RelativeLayout 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:layout_gravity="center" 
    android:gravity="center"> 

    <fragment 
     android:id="@+id/map" 
     android:name="com.google.android.gms.maps.MapFragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     tools:name="com.chilis.chilis.Maps" /> 

    </RelativeLayout> 
</LinearLayout> 

Map.class

import android.app.Activity; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentActivity; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.OnMapReadyCallback; 
import com.google.android.gms.maps.SupportMapFragment; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.MarkerOptions; 

public class Maps extends Fragment implements OnMapReadyCallback { 

    private GoogleMap mMap; 
    private FragmentActivity myContext; 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

     View rootView = inflater.inflate(R.layout.activity_maps, container, false); 

     SupportMapFragment mapFragment = (SupportMapFragment) myContext.getSupportFragmentManager()  //error 
       .findFragmentById(R.id.map); 
     mapFragment.getMapAsync(this); //error 

     return rootView; 
    } 

    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     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)); 
    } 

    @Override 
    public void onAttach(Activity activity) { 
     myContext=(FragmentActivity) activity; 
     super.onAttach(activity); 
    } 
} 

的AndroidManifest.xml

 <?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 

package="--------------"> 
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_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=".SplashActivity" 
     android:screenOrientation="portrait" 
     android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity android:name=".MainActivity" /> 

    <meta-data 
     android:name="com.google.android.geo.API_KEY" 
     android:value="@string/google_maps_key" /> 

</application> 

+0

一些變化你能請張貼滿logcat的錯誤輸出? – FlyingPumba

回答

1

檢查您activity_maps.xml文件,你不會錯過的LinearLayout的結束標記:</ LinearLayout>

+0

沒有我結束TAG,抱歉是複製粘貼錯誤。還是一樣的錯誤。 –

1

我找到了解決方案。問題是沒有找到正確的碎片上下文。

有上Map.java類

public class MapsActivity extends Fragment implements OnMapReadyCallback { 
    private GoogleMap mMap; 
    public static MapsActivity newInstance() { 
     MapsActivity fragment = new MapsActivity(); 
     return fragment; 
    } 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.activity_maps, null, false); 

     SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager() 
       .findFragmentById(R.id.map); 
     mapFragment.getMapAsync(this); 

     return view; 
    } 


    /** 
    * Manipulates the map once available. 
    * This callback is triggered when the map is ready to be used. 
    * This is where we can add markers or lines, add listeners or move the camera. In this case, 
    * we just add a marker near Sydney, Australia. 
    * If Google Play services is not installed on the device, the user will be prompted to install 
    * it inside the SupportMapFragment. This method will only be triggered once the user has 
    * installed Google Play services and returned to the app. 
    */ 
    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     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)); 
    } 
} 
相關問題