title: Back button to main activity raises "Unfortunately Application has stopped"
body: <p><strong>First of all, forgive me if the question has became too long :)</strong>  </p>
  
  <p>I wrote a (very) simple program, called <code>TwoActivity</code>, and I got an error.   </p>
  
  <h2>The program</h2>
  
  <p>The program purpose is to ask user his/her name and print <code>Hello, &lt;name&gt;</code> in another activity. The program consists of two activities:<code>First</code> and <code>Second</code>. <code>First</code> itself consists of an <code>EditText</code> to get name of the user and a button to enable user submit the name. <code>Second</code> consists of a <code>TextView</code> to print the hello expression and a button to return to <code>First</code>.  </p>
  
  <p>This is the code:  </p>
  
  <p><code>onClick</code> method of button in <code>First</code>:</p>
  
  <pre><code>public void onClickGoToSecond(View view)
  {
      Intent i=new Intent(this,Second.class);
      EditText e=(EditText) findViewById(R.id.editText);
      i.putExtra(Second.M,e.getText().toString());
      startActivity(i);
  }
  </code></pre>
  
  <p><code>M</code> in <code>Second</code>:</p>
  
  <pre><code>public static final String M="message";
  </code></pre>
  
  <p><code>onCreate()</code> of <code>Second</code>:</p>
  
  <pre><code>protected void onCreate(Bundle savedInstanceState)
  {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_second);
      Intent i=getIntent();
      TextView t=(TextView) findViewById(R.id.textView);
      t.setText("Hello "+i.getStringExtra(M));
  }
  </code></pre>
  
  <p><code>onClick</code> method of button in <code>Second</code>:</p>
  
  <pre><code>public void onClickGoBack(View view)
  {
      Intent i=new Intent(this,First.class);
      startActivity(i);
  }
  </code></pre>
  
  <p>I think other parts of codes need not to be mentioned.</p>
  
  <h2>The problem</h2>
  
  <p>The problem is that, when I go to <code>Second</code> and touch the button, the program is force closed and gives error  </p>
  
  <pre><code>Unfortunately, TwoActivity has stopped.
  </code></pre>
  
  <h2>My approaches</h2>
  
  <p>At this point, and after some googling and finding nothing, I made some <strong>dumb guesses</strong>:  </p>
  
  <ul>
  <li><strong>Dumb guess #1</strong>: Because <code>First</code> is the <code>MainActivity</code> of the program,
  I cannot make an intent to it.</li>
  <li><strong>Dumb guess #2</strong>: Because <code>First</code> has been created before, I cannot make
  an intent to it <em>again</em>.</li>
  </ul>
  
  <p>So, I changed the program to see whether my dumb guesses are really dumb. And they were, as I was sure.</p>
  
  <h2>The porgram reworked</h2>
  
  <p>I added another activity, called <code>Third</code> to the program. I also added another button to <code>First</code>, so that the user can send his/her name to <code>Second</code> or <code>Third</code>. I also added another button to <code>Second</code> that made an intent to <code>Third</code> and created it, that is something like this:<br>
  <code>onClick</code> of second button in <code>Second</code>:</p>
  
  <pre><code>public void onClickNext(View view)
  {
      Intent i=new Intent(this,Third.class);
      startActivity(i);
  }
  </code></pre>
  
  <p><code>Third</code> is fully like <code>Second</code>. It has two buttons. First one makes an intent to <code>First</code> (so it is the back button) and second button makes an intent to <code>Second</code> and creates it.</p>
  
  <h2>A more strange behavior</h2>
  
  <p>At this point, I got a really strange behavior. When I touch back button in <code>Third</code>, the program works correctly. But when I touch the same back button in <code>Second</code>, the program gives the error mentioned earlier. Further more, when I touch the button in <code>Third</code> in order to go to <code>Second</code> the program works correctly, too, but in <code>Second</code> it raises the error. Totally, the program works correctly until you enter <code>Second</code>. Touching both of the buttons in <code>Second</code> makes an error. I cannot understand what is the problem at all.</p>
  
  <p><strong><em>Comment</strong>: Note that if I press the back button of the phone itself, or if I make <code>First</code> parent of <code>Second</code> and use back button in action bar, the program works correctly.</em></p>
  
  <h2>Update</h2>
  
  <p>This is the stack trace of reworked program when error is raised (This error is raised when I click the button (with id <code>button2to3new</code>) to go from <code>Second</code> to <code>Third</code>. The same error occurs when I click button to return to <code>First</code>.):</p>
  
  <pre><code>Process: com.example.mohammad.twoactivity, PID: 7564
      java.lang.IllegalStateException: Could not find a method onClickNext(View) in the activity class android.support.v7.internal.view.ContextThemeWrapper for onClick handler on view class android.support.v7.widget.AppCompatButton with id 'button2to3new'
              at android.view.View$1.onClick(View.java:3966)
              at android.view.View.performClick(View.java:4652)
              at android.view.View$PerformClick.run(View.java:19319)
              at android.os.Handler.handleCallback(Handler.java:733)
              at android.os.Handler.dispatchMessage(Handler.java:95)
              at android.os.Looper.loop(Looper.java:146)
              at android.app.ActivityThread.main(ActivityThread.java:5641)
              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:1288)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1104)
              at dalvik.system.NativeStart.main(Native Method)
       Caused by: java.lang.NoSuchMethodException: onClickNext [class android.view.View]
              at java.lang.Class.getConstructorOrMethod(Class.java:472)
              at java.lang.Class.getMethod(Class.java:857)
              at android.view.View$1.onClick(View.java:3959)
              at android.view.View.performClick(View.java:4652)
              at android.view.View$PerformClick.run(View.java:19319)
              at android.os.Handler.handleCallback(Handler.java:733)
              at android.os.Handler.dispatchMessage(Handler.java:95)
              at android.os.Looper.loop(Looper.java:146)
  </code></pre>
  
  <p>The second line says that I haven't <code>onClickNext()</code> method as <code>onClick</code> for button that takes user from <code>Second</code> to <code>Third</code> in my activity class, but I have:</p>
  
  <pre><code>public void onClickBack(View view)
  {
      Intent i=new Intent(this,First.class);
      startActivity(i);
  }
  public void onClickNext(View view)
  {
      Intent i=new Intent(this,Third.class);
      startActivity(i);
  }
  </code></pre>
  
  <p>And this is part of the <code>activity_second</code> layout file:</p>
  
  <pre><code>&lt;Button
  ...
  android:id="@+id/button2to3new"
  android:onClick="onClickNext"
  ...
  /&gt;
  </code></pre>