First of all, forgive me if the question has became too long :)
I wrote a (very) simple program, called TwoActivity
, and I got an error.
The program
The program purpose is to ask user his/her name and print Hello, <name>
in another activity. The program consists of two activities:First
and Second
. First
itself consists of an EditText
to get name of the user and a button to enable user submit the name. Second
consists of a TextView
to print the hello expression and a button to return to First
.
This is the code:
onClick
method of button in First
:
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);
}
M
in Second
:
public static final String M="message";
onCreate()
of Second
:
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));
}
onClick
method of button in Second
:
public void onClickGoBack(View view)
{
Intent i=new Intent(this,First.class);
startActivity(i);
}
I think other parts of codes need not to be mentioned.
The problem
The problem is that, when I go to Second
and touch the button, the program is force closed and gives error
Unfortunately, TwoActivity has stopped.
My approaches
At this point, and after some googling and finding nothing, I made some dumb guesses:
- Dumb guess #1: Because
First
is theMainActivity
of the program, I cannot make an intent to it. - Dumb guess #2: Because
First
has been created before, I cannot make an intent to it again.
So, I changed the program to see whether my dumb guesses are really dumb. And they were, as I was sure.
The porgram reworked
I added another activity, called Third
to the program. I also added another button to First
, so that the user can send his/her name to Second
or Third
. I also added another button to Second
that made an intent to Third
and created it, that is something like this:
onClick
of second button in Second
:
public void onClickNext(View view)
{
Intent i=new Intent(this,Third.class);
startActivity(i);
}
Third
is fully like Second
. It has two buttons. First one makes an intent to First
(so it is the back button) and second button makes an intent to Second
and creates it.
A more strange behavior
At this point, I got a really strange behavior. When I touch back button in Third
, the program works correctly. But when I touch the same back button in Second
, the program gives the error mentioned earlier. Further more, when I touch the button in Third
in order to go to Second
the program works correctly, too, but in Second
it raises the error. Totally, the program works correctly until you enter Second
. Touching both of the buttons in Second
makes an error. I cannot understand what is the problem at all.
Comment: Note that if I press the back button of the phone itself, or if I make First
parent of Second
and use back button in action bar, the program works correctly.
Update
This is the stack trace of reworked program when error is raised (This error is raised when I click the button (with id button2to3new
) to go from Second
to Third
. The same error occurs when I click button to return to First
.):
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)
The second line says that I haven't onClickNext()
method as onClick
for button that takes user from Second
to Third
in my activity class, but I have:
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);
}
And this is part of the activity_second
layout file:
<Button
...
android:id="@+id/button2to3new"
android:onClick="onClickNext"
...
/>