我对Android编程很新,我创建了一个包含许多片段的应用程序。我已按照Google的说明设置了如何设置Google Ad Mobs。当你点击一个特定的片段时,我第一次使用它。但是,如果我返回到该片段,应用程序崩溃并且没有点击Ad Fragment
类......
我的Java课程:
public class ContactsListFragment extends BaseFragment {
ArrayList<Contact> allContacts = new ArrayList<Contact>();
...
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_ad, container, false);
return rootView;
}
}
public static class AdFragment extends Fragment {
private AdView mAdView;
public AdFragment() {
}
@Override
public void onActivityCreated(Bundle bundle) {
super.onActivityCreated(bundle);
// Gets the ad view defined in layout/ad_fragment.xml with ad unit ID set in
// values/strings.xml.
mAdView = (AdView) getView().findViewById(R.id.adView);
// Create an ad request. Check logcat output for the hashed device ID to
// get test ads on a physical device. e.g.
// "Use AdRequest.Builder.addTestDevice("ABCDEF012345") to get test ads on this device."
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
// Start loading the ad in the background.
mAdView.loadAd(adRequest);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_ad, container, false);
}
/** Called when leaving the activity */
@Override
public void onPause() {
if (mAdView != null) {
mAdView.pause();
}
super.onPause();
}
/** Called when returning to the activity */
@Override
public void onResume() {
super.onResume();
if (mAdView != null) {
mAdView.resume();
}
}
/** Called before the activity is destroyed */
@Override
public void onDestroy() {
if (mAdView != null) {
mAdView.destroy();
}
super.onDestroy();
}
}
我的这个类的xml文件:
<fragment
android:id="@+id/adFragment"
android:name="com.oozzat.app.ContactsListFragment$AdFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
我的Logcat看起来像这样:
12-19 09:35:13.474: E/AndroidRuntime(22409): FATAL EXCEPTION: main
12-19 09:35:13.474: E/AndroidRuntime(22409): android.view.InflateException: Binary XML file line #80: Error inflating class fragment
12-19 09:35:13.474: E/AndroidRuntime(22409): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719)
12-19 09:35:13.474: E/AndroidRuntime(22409): at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
12-19 09:35:13.474: E/AndroidRuntime(22409): at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
12-19 09:35:13.474: E/AndroidRuntime(22409): at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
12-19 09:35:13.474: E/AndroidRuntime(22409): at com.oozzat.app.ContactsListFragment.onCreateView(ContactsListFragment.java:52)
12-19 09:35:13.474: E/AndroidRuntime(22409): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
12-19 09:35:13.474: E/AndroidRuntime(22409): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938)
12-19 09:35:13.474: E/AndroidRuntime(22409): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
12-19 09:35:13.474: E/AndroidRuntime(22409): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
12-19 09:35:13.474: E/AndroidRuntime(22409): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
12-19 09:35:13.474: E/AndroidRuntime(22409): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:446)
12-19 09:35:13.474: E/AndroidRuntime(22409): at android.os.Handler.handleCallback(Handler.java:730)
12-19 09:35:13.474: E/AndroidRuntime(22409): at android.os.Handler.dispatchMessage(Handler.java:92)
12-19 09:35:13.474: E/AndroidRuntime(22409): at android.os.Looper.loop(Looper.java:137)
12-19 09:35:13.474: E/AndroidRuntime(22409): at android.app.ActivityThread.main(ActivityThread.java:5419)
12-19 09:35:13.474: E/AndroidRuntime(22409): at java.lang.reflect.Method.invokeNative(Native Method)
12-19 09:35:13.474: E/AndroidRuntime(22409): at java.lang.reflect.Method.invoke(Method.java:525)
12-19 09:35:13.474: E/AndroidRuntime(22409): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
12-19 09:35:13.474: E/AndroidRuntime(22409): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
12-19 09:35:13.474: E/AndroidRuntime(22409): at dalvik.system.NativeStart.main(Native Method)
12-19 09:35:13.474: E/AndroidRuntime(22409): Caused by: java.lang.IllegalArgumentException: Binary XML file line #80: Duplicate id 0xffffffff, tag adFragmentPlaceholder, or parent id 0xffffffff with another fragment for com.oozzat.app.ContactsListFragment$PlaceholderFragment
12-19 09:35:13.474: E/AndroidRuntime(22409): at android.app.Activity.onCreateView(Activity.java:4971)
12-19 09:35:13.474: E/AndroidRuntime(22409): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:695)
答案 0 :(得分:0)
问题是
12-19 09:35:13.474: E/AndroidRuntime(22409): Caused by: java.lang.IllegalArgumentException: Binary XML file line #80: Duplicate id 0xffffffff, tag adFragmentPlaceholder, or parent id 0xffffffff with another fragment for com.oozzat.app.ContactsListFragment$PlaceholderFragment
基本上,您在同一View层次结构中多次使用相同的资源ID。
查看您正在加载的Activity和Fragments的布局XML。寻找重复的身份。