引起:android.view.InflateException:二进制XML文件行#18:错误膨胀类片段

时间:2014-01-02 12:32:17

标签: android android-fragments google-maps-android-api-2 android-fragmentactivity

我即将创建一个片段Mobile,目前正在尝试使用Maps API V2。我到目前为止所以地图显示在应用程序中,但我无法使用代码中包含的其他功能(例如缩放)。

如果我选择评论,代码就会很好用:

  

map =((SupportMapFragment)getFragmentManager()。findFragmentById(R.id.maps))。getMap();

    //Bestämmer närhet på kameran
    CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LOCATION_GOOGLE, 14);
    //Uppdaterar "kameran"
    map.animateCamera(update);
    //Sätter maptyp (Satellite-view)
    map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
    //Sätter markering med en liten titel 
    map.addMarker(new MarkerOptions().position(LOCATION_GOOGLE).title("Google Inc"));

MapsFragment

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

public class MapsFragment extends Fragment{

    //Skapar en variabel för position
    private final LatLng LOCATION_GOOGLE = new LatLng(40.7406578, -74.0020894);
    //Skapar objekt
    private GoogleMap map;


    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = inflater.inflate(R.layout.maps, container, false); 

        map = ((SupportMapFragment)getFragmentManager().findFragmentById(R.id.maps)).getMap();

        //Bestämmer närhet på kameran
        CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LOCATION_GOOGLE, 14);
        //Uppdaterar "kameran"
        map.animateCamera(update);
        //Sätter maptyp (Satellite-view)
        map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
        //Sätter markering med en liten titel 
        map.addMarker(new MarkerOptions().position(LOCATION_GOOGLE).title("Google Inc"));

        return view;
    }
}

maps.xml

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:name="com.google.android.gms.maps.MapFragment"/>

主要活动

 import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends FragmentActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        buildList();
    }

    public void buildList(){
        String[] menuItems = {"Om Google", "Om Android", "Hitta oss", "Kontakta oss"};

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.menu_items, menuItems);

        ListView list = (ListView)findViewById(R.id.menuList);
        list.setAdapter(adapter);
    }


}

活动主体

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/menuList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2" >
    </ListView>

       <fragment
           android:id="@+id/maps"
           android:name="com.raj.fragment.MapsFragment"
           android:layout_width="wrap_content"
           android:layout_height="match_parent"
           android:layout_weight="2" />

</LinearLayout>

错误

01-02 13:28:25.657: E/AndroidRuntime(31071): FATAL EXCEPTION: main
01-02 13:28:25.657: E/AndroidRuntime(31071): Process: com.raj.fragment, PID: 31071
01-02 13:28:25.657: E/AndroidRuntime(31071): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.raj.fragment/com.raj.fragment.MainActivity}: android.view.InflateException: Binary XML file line #18: Error inflating class fragment
01-02 13:28:25.657: E/AndroidRuntime(31071):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
01-02 13:28:25.657: E/AndroidRuntime(31071):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2248)
01-02 13:28:25.657: E/AndroidRuntime(31071):    at android.app.ActivityThread.access$800(ActivityThread.java:138)
01-02 13:28:25.657: E/AndroidRuntime(31071):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199)
01-02 13:28:25.657: E/AndroidRuntime(31071):    at android.os.Handler.dispatchMessage(Handler.java:102)
01-02 13:28:25.657: E/AndroidRuntime(31071):    at android.os.Looper.loop(Looper.java:136)
01-02 13:28:25.657: E/AndroidRuntime(31071):    at android.app.ActivityThread.main(ActivityThread.java:5050)
01-02 13:28:25.657: E/AndroidRuntime(31071):    at java.lang.reflect.Method.invokeNative(Native Method)
01-02 13:28:25.657: E/AndroidRuntime(31071):    at java.lang.reflect.Method.invoke(Method.java:515)
01-02 13:28:25.657: E/AndroidRuntime(31071):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1264)
01-02 13:28:25.657: E/AndroidRuntime(31071):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1080)
01-02 13:28:25.657: E/AndroidRuntime(31071):    at dalvik.system.NativeStart.main(Native Method)
01-02 13:28:25.657: E/AndroidRuntime(31071): Caused by: android.view.InflateException: Binary XML file line #18: Error inflating class fragment
01-02 13:28:25.657: E/AndroidRuntime(31071):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
01-02 13:28:25.657: E/AndroidRuntime(31071):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
01-02 13:28:25.657: E/AndroidRuntime(31071):    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
01-02 13:28:25.657: E/AndroidRuntime(31071):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
01-02 13:28:25.657: E/AndroidRuntime(31071):    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
01-02 13:28:25.657: E/AndroidRuntime(31071):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
01-02 13:28:25.657: E/AndroidRuntime(31071):    at android.app.Activity.setContentView(Activity.java:1929)
01-02 13:28:25.657: E/AndroidRuntime(31071):    at com.raj.fragment.MainActivity.onCreate(MainActivity.java:13)
01-02 13:28:25.657: E/AndroidRuntime(31071):    at android.app.Activity.performCreate(Activity.java:5231)
01-02 13:28:25.657: E/AndroidRuntime(31071):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-02 13:28:25.657: E/AndroidRuntime(31071):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
01-02 13:28:25.657: E/AndroidRuntime(31071):    ... 11 more
01-02 13:28:25.657: E/AndroidRuntime(31071): Caused by: java.lang.ClassCastException: com.raj.fragment.MapsFragment cannot be cast to com.google.android.gms.maps.SupportMapFragment
01-02 13:28:25.657: E/AndroidRuntime(31071):    at com.raj.fragment.MapsFragment.onCreateView(MapsFragment.java:29)
01-02 13:28:25.657: E/AndroidRuntime(31071):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
01-02 13:28:25.657: E/AndroidRuntime(31071):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:900)
01-02 13:28:25.657: E/AndroidRuntime(31071):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082)
01-02 13:28:25.657: E/AndroidRuntime(31071):    at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1184)
01-02 13:28:25.657: E/AndroidRuntime(31071):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:291)
01-02 13:28:25.657: E/AndroidRuntime(31071):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
01-02 13:28:25.657: E/AndroidRuntime(31071):    ... 21 more

2 个答案:

答案 0 :(得分:2)

您的课程延伸FragmentActivity

你有

map = ((SupportMapFragment)getFragmentManager().findFragmentById(R.id.maps)).getMap();

CHange this

 android:name="com.google.android.gms.maps.MapFragment"/>

android:name="com.google.android.gms.maps.SupportMapFragment"/>

答案 1 :(得分:-3)

尝试向MapsFragment添加一些构造函数,它将起作用。

public MapsFragment(Context ctx, AttributeSet attrs, int def) {
    super(ctx, attrs, def);
}

public MapsFragment(Context ctx, AttributeSet attrs) {
    this(ctx, attrs, 0);
}

public MapsFragment(Context ctx) {
    this(ctx, null, 0);
}