title: How to go second activity from first activity on button click and Third activity from Second on button click in android
body: <p>Hi guys i am using 3 activities in my android app which are</p>
  <p>1.main_activity</p>
  <p>2.Secondactivity</p>
  <p>3.Third Activity</p>
  <p>I am facing problem when i try to open  the <em>Second activity</em> from main activity via a button click on main activity. The app crashes and dose not work</p>
  <p>I also need to open my <em>Third Activity</em> via a Button click on <em>Second Activity</em> When it is opened</p>
  <p>As i am new to android coding am not able to understand where i am doing wrong in my coding. I would like to seek info about and also need any one who can help me out with this</p>
  <p>Thanks in advance</p>
  <p>Below is the complete code and the Error am getting is this</p>
  <pre><code>07-27 12:13:51.887 23039-23039/com.example.mohammadzakriya.tech_bgkapp E/AndroidRuntime: FATAL EXCEPTION: main
       Process: com.example.mohammadzakriya.tech_bgkapp, PID: 23039
       java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mohammadzakriya.tech_bgkapp/com.example.mohammadzakriya.tech_bgkapp.Secondactivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' 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.-wrap11(ActivityThread.java)
           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
           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.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
           at com.example.mohammadzakriya.tech_bgkapp.Secondactivity.OnclickButtonListener(Secondactivity.java:24)
           at com.example.mohammadzakriya.tech_bgkapp.Secondactivity.onCreate(Secondactivity.java:19)
           at android.app.Activity.performCreate(Activity.java:6237)
           at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
           at android.app.ActivityThread.-wrap11(ActivityThread.java) 
           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
           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) &quot;**
                                                                                               at java.lang.reflect.Method.invoke(Native Method) 
  </code></pre>
  <p>Here is Android main Activity.java file :</p>
  <pre><code>package com.example.mohammadzakriya.tech_bgkapp;
  
  import android.content.Intent;
  import android.support.v7.app.ActionBarActivity;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Button;
  
  public class MainActivity extends AppCompatActivity{
  
      public static Button button_smb;
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          OnclickButtonListener();
      }
  
      public void OnclickButtonListener(){
          button_smb =(Button)findViewById(R.id.button);
          button_smb.setOnClickListener(
                  new View.OnClickListener() {
                      @Override
                      public void onClick(View view) {
                          Intent intent = new Intent(&quot;com.example.mohammadzakriya.tech_bgkapp.Secondactivity&quot;);
                          startActivity(intent);
                      }
                  }
          );
      }
  }
  </code></pre>
  <p>Here is Second Activity.java file :</p>
  <pre><code>package com.example.mohammadzakriya.tech_bgkapp;
  
  import android.app.Activity;
  import android.content.Intent;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Button;
  
  public class Secondactivity extends Activity{
  
      public static Button button_smb1;
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_secondactivity);
          OnclickButtonListener();
      }
  
      public void OnclickButtonListener(){
          button_smb1 =(Button)findViewById(R.id.button);
          button_smb1.setOnClickListener(
                  new View.OnClickListener() {
                      @Override
                      public void onClick(View view) {
                          Intent myintent = new Intent(&quot;com.example.mohammadzakriya.tech_bgkapp.ThirdActivity&quot;);
                          startActivity(myintent);
                      }
                  }
          );
      }
  }
  </code></pre>
  <p>Main Activity xml file :</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      tools:context=&quot;com.example.mohammadzakriya.tech_bgkapp.MainActivity&quot;
      android:background=&quot;@drawable/frontpic&quot;&gt;
  
      &lt;Button
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:text=&quot;Click to proceed&quot;
          android:id=&quot;@+id/button&quot;
          android:layout_centerVertical=&quot;true&quot;
          android:layout_centerHorizontal=&quot;true&quot;
          android:textStyle=&quot;bold|italic|normal&quot;
          android:textSize=&quot;19dp&quot;
          android:background=&quot;#d55b26f8&quot;
          android:textColor=&quot;#e2e0e0&quot; /&gt;
  
      &lt;TextView
          android:layout_width=&quot;fill_parent&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:textAppearance=&quot;?android:attr/textAppearanceLarge&quot;
          android:text=&quot;     WELCOME TO BAGALKOT APP&quot;
          android:id=&quot;@+id/textView&quot;
          android:layout_alignParentTop=&quot;true&quot;
          android:layout_centerHorizontal=&quot;true&quot;
          android:textStyle=&quot;bold|italic&quot;
          android:layout_marginTop=&quot;50dp&quot;
          android:textSize=&quot;25dp&quot;
          android:background=&quot;#fbe47d0e&quot;
          android:textColor=&quot;#e2e0e0&quot; /&gt;
  &lt;/RelativeLayout&gt;
          
  </code></pre>
  <p>Second Activity xml file :</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      tools:context=&quot;com.example.mohammadzakriya.tech_bgkapp.Secondactivity&quot;
      android:background=&quot;#eb5d16ea&quot;&gt;
  
      &lt;Button
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:text=&quot;ELECTRONICS&quot;
          android:id=&quot;@+id/button2&quot;
          android:layout_marginTop=&quot;42dp&quot;
          android:textStyle=&quot;bold|italic|normal&quot;
          android:background=&quot;#eec16d0d&quot;
          android:layout_below=&quot;@+id/textView2&quot;
          android:layout_alignParentStart=&quot;true&quot;
          android:textSize=&quot;18dp&quot;
          android:textColor=&quot;#e2e0e0&quot;/&gt;
  
      &lt;TextView
          android:layout_width=&quot;fill_parent&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:textAppearance=&quot;?android:attr/textAppearanceLarge&quot;
          android:text=&quot;              SELECT THE CATEGORY&quot;
          android:id=&quot;@+id/textView2&quot;
          android:layout_alignParentTop=&quot;true&quot;
          android:layout_alignStart=&quot;@+id/button2&quot;
          android:layout_marginTop=&quot;15dp&quot;
          android:textStyle=&quot;bold|italic|&quot;
          android:background=&quot;#fbe47d0e&quot;
          android:textColor=&quot;#e2e0e0&quot;/&gt;
  &lt;/RelativeLayout&gt;
          
  </code></pre>
  <p>Android manifest file :</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      package=&quot;com.example.mohammadzakriya.tech_bgkapp&quot;&gt;
  
      &lt;application
          android:allowBackup=&quot;true&quot;
          android:icon=&quot;@mipmap/ic_launcher&quot;
          android:label=&quot;@string/app_name&quot;
          android:supportsRtl=&quot;true&quot;
          android:theme=&quot;@style/AppTheme&quot;&gt;
          &lt;activity
              android:name=&quot;.MainActivity&quot;
              android:label=&quot;@string/app_name&quot;&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
  
                  &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
          &lt;activity
              android:name=&quot;.Secondactivity&quot;
              android:label=&quot;Tech-Bgk App&quot;&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name=&quot;com.example.mohammadzakriya.tech_bgkapp.Secondactivity&quot; /&gt;
  
                  &lt;category android:name=&quot;android.intent.category.DEFAULT&quot; /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
          &lt;activity android:name=&quot;.ThirdActivity&quot;
                    android:label=&quot;Tech-Bgk App&quot;&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name=&quot;com.example.mohammadzakriya.tech_bgkapp.ThirdActivity&quot; /&gt;
  
                  &lt;category android:name=&quot;android.intent.category.DEFAULT&quot; /&gt;
              &lt;/intent-filter&gt;
  
          &lt;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>