title: Android Button onClick listener error
body: <p>This is ridiculous , I have a simple activity inside my project, with a button and some spinners. In onCreate of the activity, I want to set the onClick event for the button (just like I do for other activities in my project. But for some unknown reasons I gen an error when creating the activity that refers to the line with the setOnClickListener. If I comment the entire listener stuff, the activity works so it's all there in the On Click listener declaration. 
  Here is my code:</p>
  
  <pre><code> protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          requestWindowFeature(Window.FEATURE_NO_TITLE);
          setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
          setContentView(R.layout.activity_send_msg);
  
          final Button btn_rev = (Button) findViewById(R.id.btn_reveniremsg);
          btn_rev.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  finish();
              }
          });
  </code></pre>
  
  <p>(The error will point to the line with <strong>final Button btn_rev...</strong> of the above code)</p>
  
  <p>And now the Logat:</p>
  
  <pre><code>W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa62d9288)
  E/AndroidRuntime﹕ FATAL EXCEPTION: main
      java.lang.RuntimeException: Unable to start activity ComponentInfo{app_pathmyblabla./activitymyblabla._path}: java.lang.NullPointerException
              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
              at android.app.ActivityThread.access$600(ActivityThread.java:130)
              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
              at android.os.Handler.dispatchMessage(Handler.java:99)
              at android.os.Looper.loop(Looper.java:137)
              at android.app.ActivityThread.main(ActivityThread.java:4745)
              at java.lang.reflect.Method.invokeNative(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:511)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
              at dalvik.system.NativeStart.main(Native Method)
       Caused by: java.lang.NullPointerException
              at myblabla.app.SendMsgActivity.onCreate (SendMsgActivity.java:32)
              at android.app.Activity.performCreate(Activity.java:5008)
              at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
              at android.app.ActivityThread.access$600(ActivityThread.java:130)
              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
              at android.os.Handler.dispatchMessage(Handler.java:99)
              at android.os.Looper.loop(Looper.java:137)
              at android.app.ActivityThread.main(ActivityThread.java:4745)
              at java.lang.reflect.Method.invokeNative(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:511)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
              at dalvik.system.NativeStart.main(Native Method)
              I/Process﹕ Sending signal. PID: 5821 SIG: 9
  </code></pre>
  
  <p>And the line with the blue color is                 at myblabla.app.SendMsgActivity.onCreate (SendMsgActivity.java:32)</p>
  
  <p>The row that the blue link points to is the first line of my code above.</p>
  
  <p>What is going on?
  I had automatically generated the code of the clickListener, it was generated by the IDE (Android Studio). I just added <strong>finish();</strong> inside the onClick.</p>
  
  <p>Also, I have the same listener code in other activities, and they work just fine</p>
  
  <p>Please tell me what I am doing wrong.</p>
  
  <p>XML of the layout</p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      tools:context="mypack.app.SendMsgActivity"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:padding="10dp"&gt;
  
      &lt;LinearLayout
          android:orientation="horizontal"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"&gt;
  
          &lt;ImageView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:id="@+id/imageView"
              android:src="@drawable/abc_spinner_ab_focused_holo_light" /&gt;
  
          &lt;RelativeLayout
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:layout_gravity="right"
              android:gravity="right"&gt;
  
              &lt;Button
                  style="?android:attr/buttonStyleSmall"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="Renunta"
                  android:id="@+id/button"
                  android:layout_gravity="right"
                  android:gravity="right|end|fill_vertical" /&gt;
          &lt;/RelativeLayout&gt;
      &lt;/LinearLayout&gt;
  
      &lt;LinearLayout
          android:orientation="horizontal"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_alignParentRight="true"
          android:layout_alignParentEnd="true"
          android:id="@+id/linearLayout"&gt;
  
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Titlu mesaj"
              android:id="@+id/textView" /&gt;
  
          &lt;EditText
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:id="@+id/editText"
              android:layout_weight="1" /&gt;
      &lt;/LinearLayout&gt;
  
      &lt;LinearLayout
          android:orientation="horizontal"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:id="@+id/linearLayout3"&gt;
  
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Continut mesaj:"
              android:id="@+id/textView3"
              android:layout_gravity="center_vertical"
              android:layout_alignBottom="@+id/editText2"
              android:layout_alignParentLeft="true"
              android:layout_alignParentStart="true" /&gt;
  
          &lt;EditText
              android:layout_width="480dp"
              android:layout_height="wrap_content"
              android:inputType="textMultiLine"
              android:ems="10"
              android:id="@+id/editText2"
              android:layout_gravity="center_vertical"
              android:layout_below="@+id/linearLayout2"
              android:layout_alignParentRight="true"
              android:layout_alignParentEnd="true"
              android:layout_toRightOf="@+id/textView3" /&gt;
      &lt;/LinearLayout&gt;
  
      &lt;LinearLayout
          android:orientation="horizontal"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_below="@+id/linearLayout"
          android:layout_alignParentRight="true"
          android:layout_alignParentEnd="true"
          android:id="@+id/linearLayout2"&gt;
  
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Destinatie mesaj"
              android:id="@+id/textView2" /&gt;
  
          &lt;Spinner
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:id="@+id/spinnertipdest"
              android:entries="@array/tipuridestinatie"
              android:prompt="@string/tipdest_prompt"
              android:layout_weight="1" /&gt;
      &lt;/LinearLayout&gt;
  
      &lt;LinearLayout
          android:orientation="horizontal"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"&gt;
  
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Alege reprezentant"
              android:id="@+id/lbl_destinatie" /&gt;
  
          &lt;Spinner
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:id="@+id/spinneruzer"
              android:layout_weight="1" /&gt;
      &lt;/LinearLayout&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Thank you</p>