Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error java.lang.IllegalStateException: Activity has been destroyed

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

    }

}
like image 492
8BitSensei Avatar asked Dec 01 '22 21:12

8BitSensei


1 Answers

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

}
like image 167
jnthnjns Avatar answered Dec 10 '22 13:12

jnthnjns