Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Android Activity Lifecycle The Activity Lifecycle Retrieving Instance State

Royce Foo Wai Kit
Royce Foo Wai Kit
2,551 Points

I keep getting bugs/errors relating to the save instance and restore instance.

No matter what i change it'll still pop up different kind of errors or bug even if i follow the video step by step. Sorry for the complications i tried following the instructions given by the markdown cheatsheet but still don't even know how to post a proper question.

public class Funfactactivity extends AppCompatActivity {
    public static final String TAG = Funfactactivity.class.getSimpleName();
    private static final String KEY_FACT = "KEY_FACT";
    private static final String KEY_COLOR = "KEY_COLOR";
    //Declare our view variables
    private FactBook mFactBook = new FactBook();
    private ColorWheel mColorWheel = new ColorWheel();
    private TextView mfunfacttext;
    private Button mfunfactbutton;
    private RelativeLayout mRelativeLayout;
    private String mFact = mFactBook.mFacts[0];
    private int mColor = Color.parseColor(mColorWheel.mColors[8]);


    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        outState.putString(KEY_FACT, mFact);
        outState.putInt(KEY_COLOR, mColor);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);

        mFact=savedInstanceState.getString(KEY_FACT);
        mfunfacttext.setText(mFact);
        mColor=savedInstanceState.getInt(KEY_COLOR);
        mRelativeLayout.setBackgroundColor(mColor);
        mfunfactbutton.setTextColor(mColor);

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_funfactactivity);

        //Assign views from the layout files to the corresponding variables
        mfunfacttext = (TextView) findViewById(R.id.funcfacttext);
        mfunfactbutton = (Button) findViewById(R.id.funfactbutton);
        mRelativeLayout = (RelativeLayout) findViewById(R.id.RelativeLayout);

        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mFact = mFactBook.getFact();
                mfunfacttext.setText(mFact);
                mColor = mColorWheel.getColor();

                //update screen with fact

                mRelativeLayout.setBackgroundColor(mColor);
                mfunfactbutton.setTextColor(mColor);
            }

        };

        mfunfactbutton.setOnClickListener(listener);
         //toast is like invinsible notifcation like android app killer says app killed how many
        Toast.makeText(Funfactactivity.this, "Application started", Toast.LENGTH_SHORT).show();



    }

}

The error

 [ 08-18 07:10:05.630  4108: 4145 D/]
                HostConnection::get() New Host Connection established 0xaa9a4ae0, tid 4145
08-18 07:10:05.650 4108-4145/fyp.funfacts I/OpenGLRenderer: Initialized EGL, version 1.4
08-18 07:10:08.873 4108-4145/fyp.funfacts E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb4093b20
08-18 07:10:08.897 4108-4108/fyp.funfacts D/AndroidRuntime: Shutting down VM
08-18 07:10:08.898 4108-4108/fyp.funfacts E/AndroidRuntime: FATAL EXCEPTION: main
                                                            Process: fyp.funfacts, PID: 4108
                                               java.lang.RuntimeException: Unable to start activity ComponentInfo{fyp.funfacts/fyp.funfacts.Funfactactivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.setBackgroundColor(int)' on a null object reference
                                                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4077)
                                                                at android.app.ActivityThread.-wrap15(ActivityThread.java)
                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1350)
                                                                at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                at android.os.Looper.loop(Looper.java:148)
                                                                at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                             Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.setBackgroundColor(int)' on a null object reference
                                                                at fyp.funfacts.Funfactactivity.onRestoreInstanceState(Funfactactivity.java:45)
                                                                at android.app.Activity.performRestoreInstanceState(Activity.java:959)
                                                                at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1163)
                                                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2389)
                                                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4077) 
                                                                at android.app.ActivityThread.-wrap15(ActivityThread.java) 
                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1350) 
                                                                at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                at android.os.Looper.loop(Looper.java:148) 
                                                                at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                at java.lang.reflect.Method.invoke(Native Method) 
                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Royce Foo Wai Kit
Royce Foo Wai Kit
2,551 Points

Edit : Solved.. For people having crashes with their new android studio in Marshmellow android please add the following code: android:configChanges="orientation|screenSize"

in your android manifest files.