我一直在尋找這個問題的答案(這個錯誤有很多問題),但沒有一個適合我。所以我想調用兩個片段。如果我只打電話給YouTube片段,一切正常。在其他活動(只有AdsFragment)一切正常工作也。android.view.InflateException:二進制XML文件行#17:錯誤膨脹類片段
這裏是:
studio.java
public class Studio extends YouTubeFailureRecoveryActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_studio);
YouTubePlayerFragment youTubePlayerFragment =
(YouTubePlayerFragment) getFragmentManager().findFragmentById(R.id.youtube_fragment);
youTubePlayerFragment.initialize("myapikey", this);
}
@Override
public void onStart() {
super.onStart();
EasyTracker.getInstance(this).activityStart(this); // Add this method.
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player,
boolean wasRestored) {
if (!wasRestored) {
player.cueVideo("film");
}
}
@Override
protected YouTubePlayer.Provider getYouTubePlayerProvider() {
return (YouTubePlayerFragment) getFragmentManager().findFragmentById(R.id.youtube_fragment);
}
public void showPlaylist(View view){
Intent intent = YouTubeIntents.createUserIntent(this, "User");
startActivity(intent);
}
@Override
public void onStop() {
super.onStop();
// The rest of your onStop() code.
EasyTracker.getInstance(this).activityStop(this); // Add this method.
}
}
這裏是activity_studio.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#cccccc"
tools:context="pl.krasiniak.krachapp_beta.Studio"
tools:ignore="MergeRootFrame">
<fragment
android:id="@+id/youtube_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
class = "com.google.android.youtube.player.YouTubePlayerFragment" />
<fragment
android:gravity="center"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:id="@+id/fragmentAds"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
class="pl.krasiniak.krachapp_beta.AdsFragment"
android:layout_gravity="center_horizontal|bottom" />
<ImageView
android:layout_margin="15dp"
android:src="@drawable/logo_studio"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_below="@+id/youtube_fragment"
android:onClick="showPlaylist"
android:id="@+id/imageView" />
<TextView
android:scaleType="fitXY"
android:layout_marginRight="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/StudioKrasiniakText"
android:layout_alignTop="@+id/imageView"
android:layout_toRightOf="@+id/imageView" />
</RelativeLayout>
和logcat的
01-24 22:00:38.394 14282-14282/pl.krasiniak.krachapp_beta E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{pl.krasiniak.krachapp_beta/pl.krasiniak.krachapp_beta.Studio}: android.view.InflateException: Binary XML file line #17: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2079)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
at android.app.ActivityThread.access$600(ActivityThread.java:132)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1157)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4575)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
at android.app.Activity.setContentView(Activity.java:1835)
at pl.krasiniak.krachapp_beta.Studio.onCreate(Studio.java:44)
at android.app.Activity.performCreate(Activity.java:4465)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2033)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
at android.app.ActivityThread.access$600(ActivityThread.java:132)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1157)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4575)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: pl.krasiniak.krachapp_beta.AdsFragment cannot be cast to android.app.Fragment
at android.app.Fragment.instantiate(Fragment.java:574)
at android.app.Fragment.instantiate(Fragment.java:549)
at android.app.Activity.onCreateView(Activity.java:4235)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:673)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
at android.app.Activity.setContentView(Activity.java:1835)
at pl.krasiniak.krachapp_beta.Studio.onCreate(Studio.java:44)
at android.app.Activity.performCreate(Activity.java:4465)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2033)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
at android.app.ActivityThread.access$600(ActivityThread.java:132)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1157)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4575)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
at dalvik.system.NativeStart.main(Native Method)
AdsFragment.java
public class AdsFragment extends Fragment implements AdListener {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_ads, container, false);
assert rootView != null;
RelativeLayout layout = (RelativeLayout) rootView.findViewById(R.id.fragment_adsLayout);
AdManager mManager = new AdManager(getActivity(), "url", "code", true);
mManager.setListener(this);
AdView mAdView = new AdView(getActivity(), "url",
"code", true, true);
mAdView.setAdListener(this);
layout.addView(mAdView);
return rootView;
}
@Override
public void adClicked() {
}
@Override
public void adClosed(Ad ad, boolean b) {
}
@Override
public void adLoadSucceeded(Ad ad) {
}
@Override
public void adShown(Ad ad, boolean b) {
}
@Override
public void noAdFound() {
}
}
它很奇怪,因爲它工作的其他活動非常正常......我將AdsFragment.java添加到我的問題 – wzieba
@tuksiarz:請檢查您的'Fragment'的import語句,看看您的'Fragment'實現進口。 – CommonsWare
我已經導入了這個'import android.support.v4.app.Fragment' – wzieba