错误java.lang.IllegalStateException:活动已被破坏 [英] Error java.lang.IllegalStateException: Activity has been destroyed
问题描述
下面,我一直在努力,而我又找到这个问题,每次运行下面的$ C $词似乎得到了错误的时间与Facebook的SDK工作java.lang.IllegalStateException:活动已被破坏,我认为这事做的片段,有什么想法?
11-02 15:24:29.212:E / AndroidRuntime(17034):致命异常:主要
11-02 15:24:29.212:E / AndroidRuntime(17034):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.facebooktest / com.example.facebooktest.FacebookTutorial}:java.lang.IllegalStateException:活动已被破坏
以上是错误,以下是code。
包com.example.facebooktest;
进口android.os.Bundle;
进口android.support.v4.app.Fragment;
进口android.support.v4.app.FragmentActivity;
进口android.view.Menu;
进口android.view.ViewGroup.LayoutParams;
进口android.widget.LinearLayout;
进口android.widget.TextView;
进口com.facebook.android.AsyncFacebookRunner;
进口com.facebook.android.Facebook;公共类FacebookTutorial扩展FragmentActivity {
//实--------- ---------引用//
私人片段mainFragment; 从facebook.com/developers //应用程序ID
公共静态最后弦乐APP_ID =434865303240706;
//任何log.x陈述日志标签
公共静态最后的字符串标记=连接Facebook;
//权限阵列
私有静态最后的String [] = PERMS新的String [] {user_events};
// Facebook的瓦尔
私人facebook mFacebook;
私人AsyncFacebookRunner mAsyncRunner;
// ID文本视图
私人TextView的多行文字; 公共静态最终诠释LOGIN = Menu.FIRST;
公共静态最终诠释GET_EVENTS = Menu.FIRST + 1;
公共静态最终诠释GET_ID = Menu.FIRST + 2; 保护无效InitLayout在(){
的LinearLayout rootView =新的LinearLayout(this.getApplicationContext());
rootView.setOrientation(LinearLayout.VERTICAL); this.mText =新的TextView(this.getApplicationContext());
this.mText.setLayoutParams(新的LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
rootView.addView(this.mText); this.setContentView(rootView); } // ---------方法叫上创建。 --------- // 公共无效的onCreate(捆绑savedInstanceState){ 如果(savedInstanceState == NULL){
//添加片段初始设置活动
mainFragment =新片段();
getSupportFragmentManager()调用BeginTransaction()
。新增(android.R.id.content,mainFragment).commit();
}其他{
//或者设置为从恢复状态信息的片段
mainFragment =(片段)getSupportFragmentManager()
.findFragmentById(android.R.id.content);
} }}
您的onCreate
缺少 super.onCreate(savedInstanceState);
,把它放在如果
语句之前,这应该解决这个问题。
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState); 如果(savedInstanceState == NULL){
//添加片段初始设置活动
mainFragment =新片段();
getSupportFragmentManager()调用BeginTransaction()
。新增(android.R.id.content,mainFragment).commit();
}其他{
//或者设置为从恢复状态信息的片段
mainFragment =(片段)getSupportFragmentManager()
.findFragmentById(android.R.id.content);
}}
Below I have been trying to work with the Facebook SDK while i came upon this problem, every time that a run the below code i seem to get the error "java.lang.IllegalStateException: Activity has been destroyed", I think it has something to do with the fragments, any thoughts?
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
Above is the error, and below is the code.
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);
}
}
}
Your onCreate
is missing super.onCreate(savedInstanceState);
, put it before the if
statement and that should resolve the issue.
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.lang.IllegalStateException:活动已被破坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!