Reputation: 362
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 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 is that, when I go to Second
and touch the button, the program is force closed and gives error
Unfortunately, TwoActivity has stopped.
At this point, and after some googling and finding nothing, I made some dumb guesses:
First
is the MainActivity
of the program,
I cannot make an intent to it.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.
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.
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.
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"
...
/>
Upvotes: 3
Views: 2567
Reputation: 13009
This answer is based on the full code which mohammad .k copied to chat
Note that the error message was
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
So there is a method missing for AppCompatButton, but Second
extends ActionBarActivity
. Why is the button treated as AppCompatButton? Why only the button in Second
?
Comparing the layout files, it turns out that the layout for the second Activity
contains the line
android:theme="@style/AppTheme"
And that's it: Our benevolent IDE (Android Studio?) generated the AppTheme as
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
So the Button
element in the layout file was treated as belonging to the AppCompat universe and behind the curtains the poor old deprecated ActionBarActivity failed to answer correctly when the runtime asked for the OnClickListener
method...BOOM
Solution: remove the android:theme
Upvotes: 1
Reputation: 4781
Here is the correct / better approach to navigate to next activity, you need to give it a try.
Instead of specifying the function in the XML, you can use onclicklisteners for the button / view which will navigate to next activity. Look below.
FirstActivity
XML: Remove the onclick from the XML.
<Button
...
android:id="@+id/button1to2new"
...
/>
Java
protected void onCreate (Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
EditText e = (EditText) findViewById(R.id.editText);
Button nextButton = (Button) findViewById(R.id.button1to2new);
nextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(FirstActivity.this, Second.class);
i.putExtra("user_data_1", e.getText().toString()); // user_data_1 is the key which is used in the next activity to retrieve the data.
startActivity(i);
}
});
}
Then in the second activity
SecondActivity
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(""user_data_1));
}
The above code will work fine for passing the data between the activities.
if you want go back or close the current activity, just press back press / use finish();
in any of the click listeners.
Additional Links :
How do I pass data between Activities in Android application?
Best wishes..!!
Upvotes: 0