Google Maps V2异常:在Fragment中填充类片段时出错

时间:2014-01-11 05:53:24

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

问候研究员Stackoverflow社区,

我正在开发在Fragment和ViewPager中显示Google Maps V2的Android应用程序。 我面临一个关于膨胀包含谷歌地图V2的片段布局的问题。我搜索并查看此链接Android, google maps fragment and viewpager - Error inflating class fragment以获取答案,因为该方法与我所寻找的方法相同,在Fragment中膨胀Google Maps V2但仍然无法正常工作。 我也在这个链接Android Google Maps in Fragment中搜索,但结果是一样的。

这是我的fragment_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true" 
    >
    <LinearLayout 
        android:id="@+id/layout_detail_fragment_secondlist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="#ffffff" 
        >
        <ImageView
            android:id="@+id/detail_item_imv_secondlist"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:contentDescription="@null"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true" 
            />
        <TextView
            android:id="@+id/detail_item_tv_title_secondlist"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:background="#cae0e0e0"
            android:gravity="left|center_vertical"
            android:textSize="15sp"
            android:textStyle="bold" 
            />
        <TextView
            android:id="@+id/detail_item_tv_content_secondlist"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="fill"
            android:padding="10dp"
            android:textSize="12sp"
            android:lineSpacingMultiplier="1.5" 
            />
        <fragment
            android:id="@+id/detail_item_map_content_secondlist"
            android:layout_width="match_parent"
            android:layout_height="150dp" 
            class="com.google.android.gms.maps.SupportMapFragment"
            />
    </LinearLayout>
</ScrollView>

这是我的fragment_class:

package com.gbu.app.template.fragments;

import java.util.ArrayList;

import com.gbu.app.template.DetailSecondListActivity;
import com.gbu.app.template.R;
import com.gbu.app.template.data.StoreObject;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class DetailSecondListViewPagerFragment extends Fragment {
    private ImageView detail_item_imv_secondlist;
    private LinearLayout layout_detail_fragment_secondlist;
    private TextView detail_item_tv_title_secondlist, detail_item_tv_content_secondlist;
    private DisplayMetrics detail_metrics_secondlist;
    private ArrayList<StoreObject> detail_store;

    @Override
    public View onCreateView(LayoutInflater inflator, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflator, container, savedInstanceState);
        View view_detail_secondlist = inflator.inflate(R.layout.item_viewpager_detail_secondlist, container, false);
        savedInstanceState = getArguments();
        detail_metrics_secondlist = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(detail_metrics_secondlist);
        layout_detail_fragment_secondlist = (LinearLayout) view_detail_secondlist.findViewById(R.id.layout_detail_fragment_secondlist);
        detail_item_imv_secondlist = (ImageView) layout_detail_fragment_secondlist.findViewById(R.id.detail_item_imv_secondlist);
        detail_item_tv_title_secondlist = (TextView) layout_detail_fragment_secondlist.findViewById(R.id.detail_item_tv_title_secondlist);
        detail_item_tv_content_secondlist = (TextView) layout_detail_fragment_secondlist.findViewById(R.id.detail_item_tv_content_secondlist);
        detail_store = new ArrayList<StoreObject>();
        detail_store = DetailSecondListActivity.detail_store;
        startApp(savedInstanceState);
        return view_detail_secondlist;
    }

    private void startApp(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        detail_item_imv_secondlist.getLayoutParams().height = (int) (detail_metrics_secondlist.heightPixels * 0.38);
        detail_item_imv_secondlist.setImageResource(R.drawable.default_images);
        detail_item_tv_title_secondlist.setText(detail_store.get(savedInstanceState.getInt("current_item")).getTitle());
        detail_item_tv_content_secondlist.setText(detail_store.get(savedInstanceState.getInt("current_item")).getAddress());
    }

    public void onDestroyView() {
        super.onDestroyView();
        Log.d("message", "onDestroyView");
    }
}

这是pager_adapter:

package com.gbu.app.template.adapters;

import com.gbu.app.template.DetailSecondListActivity;
import com.gbu.app.template.fragments.DetailSecondListViewPagerFragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class DetailSecondListPagerAdapter extends FragmentPagerAdapter {
    DetailSecondListViewPagerFragment dslvpf;
    Bundle args;

    public DetailSecondListPagerAdapter(FragmentManager fm) {
        super(fm);
        // TODO Auto-generated constructor stub
    }

    @Override
    public Fragment getItem(int arg0) {
        // TODO Auto-generated method stub
        dslvpf = new DetailSecondListViewPagerFragment();
        args = new Bundle();
        args.putInt("current_item", arg0);
        dslvpf.setArguments(args);
        return dslvpf;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return DetailSecondListActivity.detail_store.size();
    }

}

这是布局Main_Activity.xml:

<?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"
    >
    <android.support.v4.view.ViewPager
        android:id="@+id/detail_view_pager_second_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        />
</RelativeLayout>

这是我的Main_Activity的代码:

package com.gbu.app.template;

import java.util.ArrayList;

import com.gbu.app.template.adapters.DetailSecondListPagerAdapter;
import com.gbu.app.template.data.StoreObject;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;

public class DetailSecondListActivity extends FragmentActivity {
    private Intent i;
    public static ArrayList<StoreObject> detail_store;
    private ViewPager detail_viewpager_secondlist;
    private DetailSecondListPagerAdapter detail_secondlist_pager_adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail_secondlist);
        detail_viewpager_secondlist = (ViewPager) findViewById(R.id.detail_view_pager_second_list);
        detail_store = new ArrayList<StoreObject>();
        detail_store = SecondListActivity.item_store;
    }

    @Override
    protected void onStart() {
        super.onStart();
        detail_secondlist_pager_adapter = new DetailSecondListPagerAdapter(getSupportFragmentManager());
        detail_viewpager_secondlist.setAdapter(detail_secondlist_pager_adapter);
    }

    @Override
    protected void onStop() {
        super.onStop();
        TabGroupActivity parentActivity = (TabGroupActivity) getParent();
        parentActivity.onActivityResult(3, RESULT_OK, i);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 4 && resultCode == RESULT_OK) {
            i = data;
            startApp(data);
        }
    }

    private void startApp(Intent data) {
        // TODO Auto-generated method stub
        detail_viewpager_secondlist.setCurrentItem(data.getIntExtra("position", 0));
    }
}

这是AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.gbu.app.template"
    android:versionCode="1"
    android:versionName="1.0" xmlns:tools="http://schemas.android.com/tools">

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18"/>

    <permission
        android:name="com.gbu.app.template.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <uses-permission android:name="com.gbu.app.template.permission.MAPS_RECEIVE"/>
    <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="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.gbu.app.template.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.gbu.app.template.TemplateTabActivity"
            android:label="@string/app_name" >
        </activity>
        <activity
            android:name="com.gbu.app.template.FirstListActivity"
            android:label="@string/app_name" >
        </activity>
        <activity
            android:name="com.gbu.app.template.DetailFirstListActivity"
            android:label="@string/app_name" >
        </activity>
        <activity
            android:name="com.gbu.app.template.SecondTabActivity"
            android:label="@string/app_name" >
        </activity>
        <activity
            android:name="com.gbu.app.template.SecondListActivity"
            android:label="@string/app_name" >
        </activity>
        <activity
            android:name="com.gbu.app.template.DetailSecondListActivity"
            android:label="@string/app_name" >
        </activity>
        <activity
            android:name="com.gbu.app.template.MapviewActivity"
            android:label="@string/app_name" >
        </activity>
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyCYbyZhouA71LgSsWbZxnFEyQrt_Q_xdHQ"/>   
    </application>

</manifest>

我的问题是,如果我将Google Maps V2放在Main_Activity中,那么地图就可以正常工作并显示正常。但是如果我放入Fragments,结果将触发异常:错误膨胀类片段。这是显示异常的logcat,我不知道是什么导致了它,因为它没有显示类似ClassNotFoundException或NullException的原因或导致它的原因:

01-11 12:05:44.879: E/AndroidRuntime(7711): android.view.InflateException: Binary XML file line #40: Error inflating class fragment
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at com.gbu.app.template.fragments.DetailSecondListViewPagerFragment.onCreateView(DetailSecondListViewPagerFragment.java:32)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.view.View.measure(View.java:16420)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.view.View.measure(View.java:16420)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5055)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.view.View.measure(View.java:16420)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5055)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.view.View.measure(View.java:16420)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5055)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2549)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.view.View.measure(View.java:16420)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5055)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.view.View.measure(View.java:16420)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5055)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.view.View.measure(View.java:16420)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5055)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2549)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.view.View.measure(View.java:16420)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5055)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.view.View.measure(View.java:16420)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.view.View.measure(View.java:16420)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5055)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.view.View.measure(View.java:16420)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.view.View.measure(View.java:16420)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5055)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.widget.FrameLayout.onM

它只是在我的fragment_class中的代码中说,错误仅指向在布局上膨胀:

View view_detail_secondlist = inflator.inflate(R.layout.item_viewpager_detail_secondlist, container, false);

那么,我在这里缺少什么?这是一些我没有添加的代码?或许我还没有真正理解开发片段的概念,谷歌地图V2? 任何帮助都会得到赞赏,谢谢。

3 个答案:

答案 0 :(得分:1)

在充气时尝试使用try catch block
全球声明View view_detail_secondlist;

       @Override
    public View onCreateView(LayoutInflater inflator, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflator, container, savedInstanceState);

    if (container == null) {
        return null;
    }

    if (view_detail_secondlist != null) {
        ViewGroup parent = (ViewGroup) view_detail_secondlist.getParent();
        if (parent != null)
            parent.removeView(view_detail_secondlist);
    }

    try {
        view_detail_secondlist = inflator.inflate(R.layout.item_viewpager_detail_secondlist, container, false);
    } catch (InflateException e) {
        // Log.wtf("S*****", e.getMessage());
    }
    savedInstanceState = getArguments();
    detail_metrics_secondlist = new DisplayMetrics();
    getActivity().getWindowManager().getDefaultDisplay().getMetrics(detail_metrics_secondlist);
    layout_detail_fragment_secondlist = (LinearLayout) view_detail_secondlist.findViewById(R.id.layout_detail_fragment_secondlist);
    detail_item_imv_secondlist = (ImageView) layout_detail_fragment_secondlist.findViewById(R.id.detail_item_imv_secondlist);
    detail_item_tv_title_secondlist = (TextView) layout_detail_fragment_secondlist.findViewById(R.id.detail_item_tv_title_secondlist);
    detail_item_tv_content_secondlist = (TextView) layout_detail_fragment_secondlist.findViewById(R.id.detail_item_tv_content_secondlist);
    detail_store = new ArrayList<StoreObject>();
    detail_store = DetailSecondListActivity.detail_store;
    startApp(savedInstanceState);
    return view_detail_secondlist;
}

答案 1 :(得分:0)

删除 来自super.onCreateView(inflator, container, savedInstanceState);

OnCreate的{​​{1}}

答案 2 :(得分:0)

您正在使用支持片段类,如果您没有将旧Sdk定位为将其更改为MapFragment