gpt4 book ai didi

java - fragment 替换/添加导致 IllegalStateException

转载 作者:行者123 更新时间:2023-12-01 18:32:42 25 4
gpt4 key购买 nike

所以我最近开始使用 fragment 并尝试了解一切是如何工作的。我已经把它们放在一起了,但我的应用程序总是崩溃。任何关于正在发生的事情的提示将不胜感激,并且解释为什么也很棒。我正在尝试在 fragment 中加载 YouTube 视频,该 fragment 加载到 ListView 适配器传递给我的 View 中。这是我的设置:

我为 YouTube 视频设置了一个 fragment :

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);

}
}
});
}
}

现在我还有一个设置 fragment 和交换的类:

 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>

日志猫:

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)

最佳答案

您不应该在 Activity 的构造函数中进行任何初始化(尤其是 Fragment 事务)。初始化应该在 onCreate

中进行
public class Youtube extends FragmentActivity {
public static final String API_KEY = "MyKey";
public static final String VIDEO_ID = "videoID";
public YouTubePlayerView youTubePlayerView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PlayerYouTubeFrag myFragment = PlayerYouTubeFrag.newInstance("Adp1TUDRNtc");
getSupportFragmentManager().beginTransaction().replace(view.findViewById(R.id.placeholder).getId(), myFragment).commit();
}
}

关于java - fragment 替换/添加导致 IllegalStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23415462/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com