2

我在 stackoverflow 中发现了类似的主题,但对我没有帮助。我正在使用片段显示谷歌地图,它在获得另一个片段并返回后崩溃。换句话说,谷歌地图只显示一次并崩溃。这是代码。

public class MapTabMainFragment extends BaseFragment {
    private AdView adView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view = null;
        try {
            view = inflater.inflate(R.layout.fragment_map_main, container,
                    false);
        } catch (Exception e) {
            e.printStackTrace();
        }

        initComponents(view);
        initValues();
        initListeners();

        return view;
    }

    public void initComponents(View view) {
        adView = (AdView) view.findViewById(R.id.adView1);
    }

    public void initValues() {
        AdRequest re = new AdRequest();
        adView.loadAd(re);
    }

    public void initListeners() {

    }
}

 public class BaseFragment extends Fragment {

    public BottomTabActivity mActivity;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mActivity = (BottomTabActivity) this.getActivity();
    }

    public boolean onBackPressed() {
        return false;
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {

    }
}

当我试图捕捉异常时,它是

android.view.InflateException:二进制 XML 文件第 20 行:膨胀类片段时出错。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/lib/com.google.ads"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/WhiteColor"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/header" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="@dimen/data_listview_margin_top" >

        <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:layout_marginBottom="@dimen/data_listview_margin_bottom"
            class="com.google.android.gms.maps.SupportMapFragment" />
        <!--class="com.cyrilmottier.polaris2.maps.SupportMapFragment"  -->

        <com.google.ads.AdView
            android:id="@+id/adView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            app:adSize="SMART_BANNER"
            app:adUnitId="ca-app-pub-9766031373541061/3761995838"
            app:loadAdOnCreate="true"
            app:testDevices="TEST_EMULATOR, TEST_DEVICE_ID" >
        </com.google.ads.AdView>
    </RelativeLayout>

</LinearLayout>
4

6 回答 6

3

您不能在 XML 中嵌套片段。你应该在代码中做到这一点。最好的方法是创建一个 FrameLayout,并在运行时将其替换为您的片段。

假设 XML 如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/inquiryLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".InquiryFragment" >

<FrameLayout
    android:id="@+id/contentFrame"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true" >

</FrameLayout>
 </RelativeLayout>

现在代码很简单:

public class InquiryFragment extends Fragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.inquiry_fragment, container, false);

        Plan1Fragment frag = new Plan1Fragment();
        getChildFragmentManager().beginTransaction()
            .replace(R.id.contentFrame, frag).commit();

        return v;
    }

}

就是这样。

于 2014-01-05T18:44:31.933 回答
1

在对stackoverflow进行研究后,我自己解决了这个问题。这与嵌套片段问题有关。我在 xml 中删除了地图片段并为其放置了框架,然后以编程方式添加片段。它就像一个魅力。感谢您的所有努力。

于 2013-10-01T15:50:39.123 回答
0
// try this way

public class MapTabMainFragment extends FragmentActivity {

    private AdView adView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_map_main);
        adView = (AdView) findViewById(R.id.adView1);
        AdRequest re = new AdRequest();
        adView.loadAd(re);

    }

}
于 2013-10-01T04:35:53.433 回答
0

您正在尝试在 BaseFragment 类中实际创建 Activity 之前访问它...

mActivity = (BottomTabActivity) this.getActivity();

相反,覆盖“onActivityCreated”方法并在那里执行...

@Override
public void onActivityCreated(Bundle savedInstanceState)
{
    mActivity = (BottomTabActivity) this.getActivity();
}
于 2013-11-14T05:52:09.070 回答
0

编辑应用程序的 AndroidManifest.xml 文件,并在元素中添加以下声明。这嵌入了应用程序编译时使用的 Google Play 服务版本。

<meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />

->这解决了我的问题。

于 2013-11-27T15:11:56.237 回答
0

与其放入xmlns:android="http://schemas.android.com/apk/res/android"Linear Layout 标签,不如将其放入 <com.google.android.gms.ads.AdView标签中。如下所示:

 <com.google.android.gms.ads.AdView 
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:adSize="SMART_BANNER"
        app:adUnitId="ca-app-pub-9766031373541061/3761995838"
        app:loadAdOnCreate="true"
        app:testDevices="TEST_EMULATOR, TEST_DEVICE_ID" />

如需进一步帮助,请访问此链接

于 2015-05-23T19:02:17.080 回答