地图片段崩溃的活动

时间:2014-02-10 18:32:16

标签: android android-layout android-fragments android-activity android-fragmentactivity

我有这个活动布局,其中包含一个片段:

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

    ...

    <fragment
        android:id="@+id/HomeActivity_container_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/HomeActivity_navigation_LinearLayout" />

</RelativeLayout>

这是活动:

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home_activity); //crashes here
    init();
    changeFragment(R.id.HomeActivity_container_fragment, new MapFragment());
}

这是changeFragment函数:

protected void changeFragment(int fragmentContainer, Fragment newFragment)
{
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.replace(fragmentContainer, newFragment);
    transaction.addToBackStack(null);

    transaction.commit();
}

这是地图片段的布局:

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

    <fragment
        android:id="@+id/MapFragment_map_Fragment"
        class="com.google.android.gms.maps.MapFragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

</RelativeLayout>

这是地图片段类:

public class MapFragment extends Fragment
{
    @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
     {
         View rootView = inflater.inflate(R.layout.map_fragment, container, false);
         return rootView;
     }
}

Nore:谷歌地图验证没有问题,因为如果我将我的活动片段容器放入其正在运行的地图中。

以下是我从logcat获取的例外:

02-10 20:23:33.591: E/AndroidRuntime(2009): FATAL EXCEPTION: main
02-10 20:23:33.591: E/AndroidRuntime(2009): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.evapp.activities/com.evapp.activities.HomeActivity}: android.view.InflateException: Binary XML file line #50: Error inflating class fragment
02-10 20:23:33.591: E/AndroidRuntime(2009):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
02-10 20:23:33.591: E/AndroidRuntime(2009):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
02-10 20:23:33.591: E/AndroidRuntime(2009):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
02-10 20:23:33.591: E/AndroidRuntime(2009):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
02-10 20:23:33.591: E/AndroidRuntime(2009):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-10 20:23:33.591: E/AndroidRuntime(2009):     at android.os.Looper.loop(Looper.java:137)
02-10 20:23:33.591: E/AndroidRuntime(2009):     at android.app.ActivityThread.main(ActivityThread.java:4745)
02-10 20:23:33.591: E/AndroidRuntime(2009):     at java.lang.reflect.Method.invokeNative(Native Method)
02-10 20:23:33.591: E/AndroidRuntime(2009):     at java.lang.reflect.Method.invoke(Method.java:511)
02-10 20:23:33.591: E/AndroidRuntime(2009):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-10 20:23:33.591: E/AndroidRuntime(2009):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-10 20:23:33.591: E/AndroidRuntime(2009):     at dalvik.system.NativeStart.main(Native Method)
02-10 20:23:33.591: E/AndroidRuntime(2009): Caused by: android.view.InflateException: Binary XML file line #50: Error inflating class fragment
02-10 20:23:33.591: E/AndroidRuntime(2009):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
02-10 20:23:33.591: E/AndroidRuntime(2009):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
02-10 20:23:33.591: E/AndroidRuntime(2009):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
02-10 20:23:33.591: E/AndroidRuntime(2009):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
02-10 20:23:33.591: E/AndroidRuntime(2009):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
02-10 20:23:33.591: E/AndroidRuntime(2009):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
02-10 20:23:33.591: E/AndroidRuntime(2009):     at android.app.Activity.setContentView(Activity.java:1867)
02-10 20:23:33.591: E/AndroidRuntime(2009):     at com.evapp.activities.HomeActivity.onCreate(HomeActivity.java:21)
02-10 20:23:33.591: E/AndroidRuntime(2009):     at android.app.Activity.performCreate(Activity.java:5008)
02-10 20:23:33.591: E/AndroidRuntime(2009):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
02-10 20:23:33.591: E/AndroidRuntime(2009):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
02-10 20:23:33.591: E/AndroidRuntime(2009):     ... 11 more
02-10 20:23:33.591: E/AndroidRuntime(2009): Caused by: java.lang.NullPointerException: name == null
02-10 20:23:33.591: E/AndroidRuntime(2009):     at java.lang.VMClassLoader.findLoadedClass(Native Method)
02-10 20:23:33.591: E/AndroidRuntime(2009):     at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:354)
02-10 20:23:33.591: E/AndroidRuntime(2009):     at java.lang.ClassLoader.loadClass(ClassLoader.java:491)
02-10 20:23:33.591: E/AndroidRuntime(2009):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
02-10 20:23:33.591: E/AndroidRuntime(2009):     at android.support.v4.app.Fragment.isSupportFragmentClass(Fragment.java:436)
02-10 20:23:33.591: E/AndroidRuntime(2009):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:255)
02-10 20:23:33.591: E/AndroidRuntime(2009):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
02-10 20:23:33.591: E/AndroidRuntime(2009):     ... 21 more

1 个答案:

答案 0 :(得分:0)

可能您想使用FrameLayout(我认为您错误地使用fragment)...

试试这个......

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

    ...

    <FrameLayout <!-- here use FrameLayout, not fragment -->
        android:id="@+id/HomeActivity_container_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/HomeActivity_navigation_LinearLayout" />

</RelativeLayout>