我在 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>