I tried to create a simple color change application. In my application I have created a fragment which has linear layout and inside which text view is present. I am trying to change the color of textview in my application in a regular interval. In order to change color in regular interval, I have used random() function to create a random number between 0 and 255 and use those numbers as a parameter to create random colors. I am again using those random colors to set the background of my textview which is present in linear layout.
Below are my codes:
MainActivity.java
package com.example.abcd.color_change;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FragmentManager fragment = getSupportFragmentManager();
        Fragment existingFragment = fragment.findFragmentById(R.id.container);
        fragment.beginTransaction().replace(R.id.linear1, existingFragment).commit();
    }
}
ColorChangeFragment.java
package com.example.abcd.color_change;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
/**
 * Created by abcd on 10/7/17.
 */
public class ColorChangeFragment {
    private TextView mTextView;
    private Random rand = new Random();
    private int randNum = 0;
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState){
        View v = inflater.inflate(R.layout.fragment,container, false);
        mTextView = (TextView) v.findViewById(R.id.textview);
        Timer timer = new Timer();
        //MyTimer mt = new MyTimer();
        /*timer.schedule(mt, 1000, 1000);*/
        timer.schedule(new TimerTask(){
            @Override
            public void run() {
                int r = rand.nextInt(255);
                int g = rand.nextInt(255);
                int b = rand.nextInt(255);
                int randomColor = Color.rgb(r,g,b);
                mTextView.setBackgroundColor(randomColor);
            }
        },5000,5000);
        return v;
    }
   /* class MyTimer extends TimerTask{
        @Override
        public void run() {
            runOnUiThread(new Runnable(){
                @Override
                public void run() {
                    Random rand = new Random();
                    mTextView.setBackgroundColor(Color.argb(255, rand.nextInt(256), rand.nextInt(256) ));
                }
            });
        }
    }*/
}
ActivityMain.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</FrameLayout>
fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id ="@+id/linear1"
    android:background="@android:color/white">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id = "@+id/textview"/>
</LinearLayout>
Below is the error message I receive upon building the code.
10-30 12:56:21.393 21574-21574/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                   Process: com.example.abcd.color_change, PID: 21574
                                                   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.abcd.color_change/com.example.abcd.color_change.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2762)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2848)
                                                       at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1552)
                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                       at android.os.Looper.loop(Looper.java:154)
                                                       at android.app.ActivityThread.main(ActivityThread.java:6334)
                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
                                                       at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:394)
                                                       at android.support.v4.app.BackStackRecord.replace(BackStackRecord.java:441)
                                                       at android.support.v4.app.BackStackRecord.replace(BackStackRecord.java:432)
                                                       at com.example.abcd.color_change.MainActivity.onCreate(MainActivity.java:17)
                                                       at android.app.Activity.performCreate(Activity.java:6743)
                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1134)
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2715)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2848) 
                                                       at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1552) 
                                                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                       at android.os.Looper.loop(Looper.java:154) 
                                                       at android.app.ActivityThread.main(ActivityThread.java:6334) 
                                                       at java.lang.reflect.Method.invoke(Native Method) 
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
10-30 12:56:21.393 21574-21574/? D/AppTracker: App Event: crash
 
     
    