在扩展另一个扩展ActionBarActivity的Activity的Activity中映射片段膨胀错误

时间:2014-05-19 16:14:39

标签: android google-maps android-fragments

我知道这个标题没有意义。 问题是我有一个扩展ActionBarActivity的主活动(它包含所有活动中共享的所有代码);我有另一个活动扩展了主活动;最后一个活动包含一个地图片段;我添加了所有必需的代码,我在设置内容视图中收到错误。

有人可以帮忙吗?我可以添加什么来帮助您理解我的问题?

修改

这是代码。我遗漏了许多我认为无关紧要的事情。

MainLayoutActivity.java:

public class MainLayoutActivity extends ActionBarActivity {

    @Override
    public void setContentView(int id) {


        LinearLayout linearLayoutContent = (LinearLayout) findViewById(R.id.linearLayoutContent);
        LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(id, linearLayoutContent); //linrstlayoutcontent is the linearlayout that will contain the content 

    }

}

activity_main_activity.xml:

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

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <LinearLayout
                android:id="@+id/linearLayoutContent"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/bgColor"
                android:orientation="vertical" >
            </LinearLayout>

        </RelativeLayout>
    </FrameLayout> 

    <ListView
        android:id="@+id/MainMenu"
        style="@style/MainMenu"
        android:background="@color/LeftMenuBackground"
        android:divider="@drawable/menu_separator" />


</android.support.v4.widget.DrawerLayout>

ClubDetailsActivity.Java

public class ClubDetailsActivity extends MainLayoutActivity {

    private GoogleMap googleMap = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

                setContentView(R.layout.activity_club_details);
         initilizeMap();

        }


   private void initilizeMap() {
        if (googleMap == null) {

            googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
            //googleMap = (My_Layout.findViewById(R.id.map)).getMap();
            // check if map is created successfully or not
            if (googleMap == null) {
                showToast("Sorry! unable to create maps");
            }
        }
    }


}

activity_club_details.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical"  >


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


 </LinearLayout>

错误:

05-19 19:30:50.961: E/AndroidRuntime(20934): FATAL EXCEPTION: main
05-19 19:30:50.961: E/AndroidRuntime(20934): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.borninteractive.fitnesstime/com.borninteractive.fitnesstime.ClubDetailsActivity}: android.view.InflateException: Binary XML file line #164: Error inflating class fragment
05-19 19:30:50.961: E/AndroidRuntime(20934): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2249)
05-19 19:30:50.961: E/AndroidRuntime(20934): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2299)
05-19 19:30:50.961: E/AndroidRuntime(20934): at android.app.ActivityThread.access$700(ActivityThread.java:154)
05-19 19:30:50.961: E/AndroidRuntime(20934): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284)
05-19 19:30:50.961: E/AndroidRuntime(20934): at android.os.Handler.dispatchMessage(Handler.java:99)
05-19 19:30:50.961: E/AndroidRuntime(20934): at android.os.Looper.loop(Looper.java:137)
05-19 19:30:50.961: E/AndroidRuntime(20934): at android.app.ActivityThread.main(ActivityThread.java:5306)
05-19 19:30:50.961: E/AndroidRuntime(20934): at java.lang.reflect.Method.invokeNative(Native Method)
05-19 19:30:50.961: E/AndroidRuntime(20934): at java.lang.reflect.Method.invoke(Method.java:511)
05-19 19:30:50.961: E/AndroidRuntime(20934): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
05-19 19:30:50.961: E/AndroidRuntime(20934): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
05-19 19:30:50.961: E/AndroidRuntime(20934): at dalvik.system.NativeStart.main(Native Method)
05-19 19:30:50.961: E/AndroidRuntime(20934): Caused by: android.view.InflateException: Binary XML file line #164: Error inflating class fragment
05-19 19:30:50.961: E/AndroidRuntime(20934): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
05-19 19:30:50.961: E/AndroidRuntime(20934): at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
05-19 19:30:50.961: E/AndroidRuntime(20934): at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
05-19 19:30:50.961: E/AndroidRuntime(20934): at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
05-19 19:30:50.961: E/AndroidRuntime(20934): at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
05-19 19:30:50.961: E/AndroidRuntime(20934): at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
05-19 19:30:50.961: E/AndroidRuntime(20934): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
05-19 19:30:50.961: E/AndroidRuntime(20934): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
05-19 19:30:50.961: E/AndroidRuntime(20934): at com.borninteractive.fitnesstime.MainLayoutActivity.setContentView(MainLayoutActivity.java:237)
05-19 19:30:50.961: E/AndroidRuntime(20934): at com.borninteractive.fitnesstime.ClubDetailsActivity.onCreate(ClubDetailsActivity.java:87)
05-19 19:30:50.961: E/AndroidRuntime(20934): at android.app.Activity.performCreate(Activity.java:5255)
05-19 19:30:50.961: E/AndroidRuntime(20934): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
05-19 19:30:50.961: E/AndroidRuntime(20934): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2213)
05-19 19:30:50.961: E/AndroidRuntime(20934): ... 11 more
05-19 19:30:50.961: E/AndroidRuntime(20934): Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.fragment" on path: /data/app/com.borninteractive.fitnesstime-2.apk
05-19 19:30:50.961: E/AndroidRuntime(20934): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
05-19 19:30:50.961: E/AndroidRuntime(20934): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
05-19 19:30:50.961: E/AndroidRuntime(20934): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
05-19 19:30:50.961: E/AndroidRuntime(20934): at android.view.LayoutInflater.createView(LayoutInflater.java:558)
05-19 19:30:50.961: E/AndroidRuntime(20934): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:649)
05-19 19:30:50.961: E/AndroidRuntime(20934): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
05-19 19:30:50.961: E/AndroidRuntime(20934): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:666)
05-19 19:30:50.961: E/AndroidRuntime(20934): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
05-19 19:30:50.961: E/AndroidRuntime(20934): ... 23 more

谢谢

3 个答案:

答案 0 :(得分:1)

我不确定为什么会遇到ClassNotFoundException。您的构建中似乎缺少支持库。如果您的构建路径设置正确,请仔细检查此页面http://developer.android.com/tools/support-library/setup.html#libs-with-res。还要检查您是否将支持库中的片段与本机片段混合。

除此之外,即使您修复ClassNotFoundException,您的应用也无效。在MainLayoutActivity setContentView {}}},您缺少致电super.setContentView(R.layout.activity_main_activity.xml);

目前linearLayoutContent将始终为null,因为未设置布局。

答案 1 :(得分:1)

从您的sdk经理更新您的Google Play图书馆。

private void checkForMap() {
    try {
        // Getting Google Play availability status
        int status = GooglePlayServicesUtil
                .isGooglePlayServicesAvailable(mContext);

        // Showing status
        if (status != ConnectionResult.SUCCESS) {
            int requestCode = 10;
            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status,
                    this, requestCode);
            dialog.show();

        } else { // Google Play Services are available

            // Getting reference to the SupportMapFragment of
            // activity_main.xml
            SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map);
            // map = ((MapFragment)
            // getFragmentManager().findFragmentById(R.id.map)).getMap();

            // Getting GoogleMap object from the fragment
            googleMap = fm.getMap();
        }

        googleMap.setOnInfoWindowClickListener(this);
    } catch (Exception exception) {
        DebugHelper.printException(exception);
    }
}

答案 2 :(得分:1)

Google地图v2始终通过Fragment活动进行扩展,因为在xml布局文件中,地图部分是按片段显示的。所以你应该使用扩展片段活动谷歌地图。请查看以下链接,了解google map v2的说明:     https://developers.google.com/maps/documentation/android/start