gpt4 book ai didi

java - 错误 java.lang.IllegalStateException : Activity has been destroyed

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:37:13 25 4
gpt4 key购买 nike

当我遇到这个问题时,我一直在尝试使用 Facebook SDK,每次运行下面的代码时,我似乎都会收到错误“java.lang.IllegalStateException:Activity 已被破坏”,我认为它与 fragment 有关,有什么想法吗?

 11-02 15:24:29.212: E/AndroidRuntime(17034): FATAL EXCEPTION: main
11-02 15:24:29.212: E/AndroidRuntime(17034): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.facebooktest/com.example.facebooktest.FacebookTutorial}: java.lang.IllegalStateException: Activity has been destroyed

上面是错误,下面是代码

package com.example.facebooktest;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.Facebook;

public class FacebookTutorial extends FragmentActivity {
// --------- Facebook References ---------//
private Fragment mainFragment;

// application id from facebook.com/developers
public static final String APP_ID = "434865303240706";
// log tag for any log.x statements
public static final String TAG = "FACEBOOK CONNECT";
// permissions array
private static final String[] PERMS = new String[] { "user_events" };
// facebook vars
private Facebook mFacebook;
private AsyncFacebookRunner mAsyncRunner;
// id text view
private TextView mText;

public static final int LOGIN = Menu.FIRST;
public static final int GET_EVENTS = Menu.FIRST + 1;
public static final int GET_ID = Menu.FIRST + 2;

protected void initLayout() {
LinearLayout rootView = new LinearLayout(this.getApplicationContext());
rootView.setOrientation(LinearLayout.VERTICAL);

this.mText = new TextView(this.getApplicationContext());
this.mText.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
rootView.addView(this.mText);

this.setContentView(rootView);

}

// --------- Method called on create. ---------//

public void onCreate(Bundle savedInstanceState) {

if (savedInstanceState == null) {
// Add the fragment on initial activity setup
mainFragment = new Fragment();
getSupportFragmentManager().beginTransaction()
.add(android.R.id.content, mainFragment).commit();
} else {
// Or set the fragment from restored state info
mainFragment = (Fragment) getSupportFragmentManager()
.findFragmentById(android.R.id.content);
}

}

}

最佳答案

您的 onCreate 缺少 super.onCreate(savedInstanceState);,将其放在 if 语句之前,应该可以解决问题。

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

if (savedInstanceState == null) {
// Add the fragment on initial activity setup
mainFragment = new Fragment();
getSupportFragmentManager().beginTransaction()
.add(android.R.id.content, mainFragment).commit();
} else {
// Or set the fragment from restored state info
mainFragment = (Fragment) getSupportFragmentManager()
.findFragmentById(android.R.id.content);
}

}

关于java - 错误 java.lang.IllegalStateException : Activity has been destroyed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13198702/

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