title: Countdown Timer Error in Android
body: <p>I Searched it but any Solution Couldn't solve my problem.
  I'm implementing a timer in a fragment but my app stops working showing an error.</p>
  
  <p>This is the working of my app.
  when user clicks the start button it takes the user to the next activity where fragments are shown in a container(RelativeLayout). I've implemented the Count CountDownTimer function in the fragment. Fragment was working perfectly before the CountDownTimer function.</p>
  
  <p>Any Help?
  Here is the code.</p>
  
  <p>Start.java</p>
  
  <pre><code>package com.example.sheikhspc.timerexample;
  
  import android.app.Fragment;
  import android.app.FragmentManager;
  import android.app.FragmentTransaction;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  
  public class Start extends AppCompatActivity {
  
  Fragment frag = null;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_start);
      FragmentManager fm = getFragmentManager();
      FragmentTransaction fragmentTransaction = fm.beginTransaction();
  
      frag = new BlankFragment1();
      fragmentTransaction.replace(R.id.container,frag);
  
      fragmentTransaction.commit();
  
  
  
  
  
      }
  }
  </code></pre>
  
  <p>MainActivity.java</p>
  
  <pre><code>package com.example.sheikhspc.timerexample;
  
  import android.content.Intent;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Button;
  
  public class MainActivity extends AppCompatActivity {
  
  Button btn;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      btn = (Button)findViewById(R.id.startbtn);
  }
  
  public void start(View view)
  {
      Intent intent = new Intent(this,Start.class);
      startActivity(intent);
  }
  }
  </code></pre>
  
  <p>BlankFragment1.java</p>
  
  <pre><code>package com.example.sheikhspc.timerexample;
  
  
  import android.os.Bundle;
  import android.app.Fragment;
  import android.os.CountDownTimer;
  import android.provider.DocumentsContract;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.TextView;
  import android.widget.Toast;
  
  
  public class BlankFragment1 extends Fragment {
  
  
  public BlankFragment1() {
      // Required empty public constructor
  }
  
  
  Fragment frag = null;
  TextView tv;
  View root;
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
      // Inflate the layout for this fragment
      tv = (TextView)root.findViewById(R.id.timer1);
  
      new CountDownTimer(30000, 1000) {
  
          public void onTick(long millisUntilFinished) {
              tv.setText("seconds remaining: " + millisUntilFinished / 1000);
              //here you can have your logic to set text to edittext
          }
  
          public void onFinish() {
              tv.setText("done!");
          }
  
      }.start();
  
  
  
      return inflater.inflate(R.layout.fragment_blank_fragment1, container,    false);
  }
  
  }
  </code></pre>
  
  <p>Error Log</p>
  
  <pre><code>     E/AndroidRuntime: FATAL EXCEPTION: main
       Process: com.example.sheikhspc.timerexample, PID: 28970
       java.lang.RuntimeException: Unable to start activity    ComponentInfo{com.example.sheikhspc.timerexample/com.example.sheikhspc.timerexamp le.Start}: 
       java.lang.NullPointerException: Attempt to invoke virtual method  'android.view.View android.view.View.findViewById(int)' on a null object  reference
  
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
                    at android.app.ActivityThread.access$800(ActivityThread.java:155)
                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
                    at android.os.Handler.dispatchMessage(Handler.java:102)
                    at android.os.Looper.loop(Looper.java:135)
                    at android.app.ActivityThread.main(ActivityThread.java:5343)
                    at java.lang.reflect.Method.invoke(Native Method)
                    at java.lang.reflect.Method.invoke(Method.java:372)
                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
                 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
                    at com.example.sheikhspc.timerexample.BlankFragment1.onCreateView(BlankFragment1.java:46)
                    at android.app.Fragment.performCreateView(Fragment.java:2053)
                    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:894)
                    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
                    at android.app.BackStackRecord.run(BackStackRecord.java:834)
                    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1454)
                    at android.app.Activity.performStart(Activity.java:6025)
                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413) 
                    at android.app.ActivityThread.access$800(ActivityThread.java:155) 
                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317) 
                    at android.os.Handler.dispatchMessage(Handler.java:102) 
                    at android.os.Looper.loop(Looper.java:135) 
                    at android.app.ActivityThread.main(ActivityThread.java:5343) 
                    at java.lang.reflect.Method.invoke(Native Method) 
                    at java.lang.reflect.Method.invoke(Method.java:372) 
                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905) 
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700) 
  Application terminated.
  </code></pre>