title: Android start activity with button?
body: <p>i keep getting a null pointer exception</p>
  
  <p>I have a problem about starting a new activity via using button.</p>
  
  <p>I already checked the previous questions related to mine problem, one of them is almost same but solutions didn't work out for me. So here is my problem.</p>
  
  <p>whenever i start the app it crashes.
  i have a main class called MainActivity and new Activity called login</p>
  
  <p>Main class</p>
  
  <pre><code>public class MainActivity extends Activity {
  
      Intent myIntent = new Intent(MainActivity.this, login.class);
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          Button loginButton  = (Button) findViewById(R.id.button_login);
  
  
          loginButton.setOnClickListener(new View.OnClickListener() {
              public void onClick(View v) {
  
                  startActivity(myIntent);
              }
          });
  
      }
  
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
  
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
      }
  
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
          // as you specify a parent activity in AndroidManifest.xml.
          int id = item.getItemId();
          if (id == R.id.action_settings) {
              return true;
          }
          return super.onOptionsItemSelected(item);
      }
  
      /**
       * A placeholder fragment containing a simple view.
       */
      public static class PlaceholderFragment extends Fragment {
  
          public PlaceholderFragment() {
          }
  
          @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container,
                  Bundle savedInstanceState) {
              View rootView = inflater.inflate(R.layout.fragment_main, container, false);
              return rootView;
          }
      }
  
  }
  </code></pre>
  
  <p>logcat</p>
  
  <pre><code>02-18 10:27:42.520    1098-1098/com.example.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
      Process: com.example.app, PID: 1098
      java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.MainActivity}: java.lang.NullPointerException
              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
              at android.app.ActivityThread.access$800(ActivityThread.java:135)
              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
              at android.os.Handler.dispatchMessage(Handler.java:102)
              at android.os.Looper.loop(Looper.java:136)
              at android.app.ActivityThread.main(ActivityThread.java:5017)
              at java.lang.reflect.Method.invokeNative(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:515)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
              at dalvik.system.NativeStart.main(Native Method)
       Caused by: java.lang.NullPointerException
              at com.example.app.MainActivity.onCreate(MainActivity.java:30)
              at android.app.Activity.performCreate(Activity.java:5231)
              at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
              at android.app.ActivityThread.access$800(ActivityThread.java:135)
              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
              at android.os.Handler.dispatchMessage(Handler.java:102)
              at android.os.Looper.loop(Looper.java:136)
              at android.app.ActivityThread.main(ActivityThread.java:5017)
              at java.lang.reflect.Method.invokeNative(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:515)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
              at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>thanks in advance!!!</p>
  
  <p>EDIT---> MANIFEST</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.app" &gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" &gt;
          &lt;activity
              android:name="com.example.app.MainActivity"
              android:label="@string/app_name" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
  
          &lt;activity
              android:name="com.example.app.login"
              android:label="@string/title_activity_login" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.LOGIN" /&gt;
                  &lt;category android:name="android.intent.category.DEFAULT" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p>MainActivity Layout</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      android:paddingBottom="@dimen/activity_vertical_margin"
      tools:context="com.example.app.MainActivity"&gt;
  
      //login Button
      &lt;Button
          android:layout_width="120dp"
          android:layout_height="wrap_content"
          android:text="Login"
          android:id="@+id/button_login"
          android:layout_marginBottom="160dp"
          android:layout_alignParentBottom="true"
          android:layout_alignParentLeft="true"
          android:layout_alignParentStart="true"
          android:layout_marginLeft="23dp" /&gt;
  
      //high score button
      &lt;Button
          android:layout_width="120dp"
          android:layout_height="wrap_content"
          android:text="high score"
          android:id="@+id/highScore"
          android:layout_alignTop="@+id/button_login"
          android:layout_toRightOf="@+id/button_login"
          android:layout_marginLeft="45dp" /&gt;
  
      //play Button
      &lt;Button
          android:layout_width="150dp"
          android:layout_height="wrap_content"
          android:text="Play"
          android:id="@+id/Play"
          android:layout_above="@+id/button_login"
          android:layout_centerHorizontal="true"
          android:layout_marginBottom="55dp" /&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>Edited corrections with imports</p>
  
  <pre><code>package com.example.app;
  
  import android.app.Activity;
  import android.content.Intent;
  import android.support.v7.app.ActionBarActivity;
  import android.support.v4.app.Fragment;
  import android.os.Bundle;
  import android.view.LayoutInflater;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.Button;
  import android.widget.ImageButton;
  
  public class MainActivity extends Activity {
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          Button loginButton  = (Button) findViewById(R.id.button_login);
  
  //        if (savedInstanceState == null) {
  //            getSupportFragmentManager().beginTransaction()
  //                    .add(R.id.container, new PlaceholderFragment())
  //                    .commit();
  //        }
  
          loginButton.setOnClickListener(new View.OnClickListener() {
              public void onClick(View v) {
                  Intent myIntent = new Intent(MainActivity.this, login.class);
                  startActivity(myIntent);
              }
          });
  
      }
  
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
  
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
      }
  
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
          // as you specify a parent activity in AndroidManifest.xml.
          int id = item.getItemId();
          if (id == R.id.action_settings) {
              return true;
          }
          return super.onOptionsItemSelected(item);
      }
  
      /**
       * A placeholder fragment containing a simple view.
       */
      public static class PlaceholderFragment extends Fragment {
  
          public PlaceholderFragment() {
          }
  
          @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container,
                  Bundle savedInstanceState) {
              View rootView = inflater.inflate(R.layout.fragment_main, container, false);
              return rootView;
          }
      }
  
  }
  </code></pre>