所以我最近開始使用片段,並試圖瞭解如何工作。我把它放在一起,但我的應用程序不斷崩潰。任何關於正在發生的事情的提示都將不勝感激,同時也解釋了爲什麼會很好。我正試圖加載一個片段,加載在一個由ListView適配器交給我的視圖中的YouTube視頻。這裏是我的設置:片段替換/添加導致IllegalStateException
我已成立了一個YouTube視頻片段:
public class PlayerYouTubeFrag extends YouTubePlayerSupportFragment {
private String currentVideoID = "Adp1TUDRNtc";
private YouTubePlayer activePlayer;
public static PlayerYouTubeFrag newInstance(String url) {
PlayerYouTubeFrag playerYouTubeFrag = new PlayerYouTubeFrag();
Bundle bundle = new Bundle();
bundle.putString("url", url);
playerYouTubeFrag.setArguments(bundle);
return playerYouTubeFrag;
}
private void init() {
initialize("AIzaSyCQxLWF1j0XFwKmros9w4Y5XSk1hqpEg6Q", new OnInitializedListener() {
@Override
public void onInitializationFailure(Provider arg0, YouTubeInitializationResult arg1) { }
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
activePlayer = player;
activePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
if (!wasRestored) {
activePlayer.loadVideo(getArguments().getString("url"), 0);
}
}
});
}
}
現在我也有一個類,樹立片段和交換:
public class Youtube extends FragmentActivity{
public static final String API_KEY = "MyKey";
public static final String VIDEO_ID = "videoID";
public YouTubePlayerView youTubePlayerView;
public Youtube(View view) {
//issue here!!!
PlayerYouTubeFrag myFragment = PlayerYouTubeFrag.newInstance("Adp1TUDRNtc");
getSupportFragmentManager().beginTransaction().replace(view.findViewById(R.id.placeholder).getId(), myFragment).commit();
}
末但並非最不重要我的xml:
<FrameLayout
android:id="@+id/placeholder"
android:layout_height="match_parent"
android:layout_width="match_parent">
</FrameLayout>
的logcat的:
05-01 15:40:42.439: E/AndroidRuntime(24550): FATAL EXCEPTION: main
05-01 15:40:42.439: E/AndroidRuntime(24550): Process: com.elmehalawi.hamra, PID: 24550
05-01 15:40:42.439: E/AndroidRuntime(24550): java.lang.IllegalStateException: Activity has been destroyed
05-01 15:40:42.439: E/AndroidRuntime(24550): at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1365)
05-01 15:40:42.439: E/AndroidRuntime(24550): at and roid.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
05-01 15:40:42.439: E/AndroidRuntime(24550): at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
05-01 15:40:42.439: E/AndroidRuntime(24550): at com.elmehalawi.hamra.cards.Youtube.<init>(Youtube.java:31)
05-01 15:40:42.439: E/AndroidRuntime(24550): at com.elmehalawi.hamra.cards.YoutubeCard.setupInnerViewElements(YoutubeCard.java:23)
05-01 15:40:42.439: E/AndroidRuntime(24550): at it.gmariotti.cardslib.library.internal.Card.getInnerView(Card.java:250)
05-01 15:40:42.439: E/AndroidRuntime(24550): at it.gmariotti.cardslib.library.view.CardView.setupMainView(CardView.java:364)
05-01 15:40:42.439: E/AndroidRuntime(24550): at it.gmariotti.cardslib.library.view.CardView.buildUI(CardView.java:270)
05-01 15:40:42.439: E/AndroidRuntime(24550): at it.gmariotti.cardslib.library.view.CardView.setCard(CardView.java:230)
05-01 15:40:42.439: E/AndroidRuntime(24550): at com.elmehalawi.hamra.FeedAdapter.getView(FeedAdapter.java:96)
05-01 15:40:42.439: E/AndroidRuntime(24550): at android.widget.AbsListView.obtainView(AbsListView.java:2263)
05-01 15:40:42.439: E/AndroidRuntime(24550): at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
05-01 15:40:42.439: E/AndroidRuntime(24550): at android.widget.ListView.onMeasure(ListView.java:1175)
05-01 15:40:42.439: E/AndroidRuntime(24550): at android.view.View.measure(View.java:16497)
05-01 15:40:42.439: E/AndroidRuntime(24550): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
讓我們看看logcat。 – KingBryan
那裏,讓我知道如果你需要更多;) – Osman