我正在寻找这个问题的答案(有很多问题与此错误有关),但它们都不适合我。所以我想打电话两个片段。如果我只称youtube碎片,一切都可以。关于其他活动炎(仅带有adsfragment)一切也很好。
这里是:
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() {
}
}
Caused by: java.lang.ClassCastException: pl.krasiniak.krachapp_beta.AdsFragment cannot be cast to android.app.Fragment
显然,您的AdsFragment
不是片段,或者扩展了Android支持软件包的片段片段(android.support.v4.app.Fragment
)。
我遇到了这个错误,这是由Android:class上的错误路径引起的。请确保您的代码使用正确的路径。