3

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 the MainActivity 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"
...
/>
Mohammad
  • 364
  • 1
  • 6
  • 17
  • 2
    “Unfortunately Application has stopped” -- use LogCat to examine the Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare Jan 22 '16 at 17:25
  • My guess is: the method names in layout file and the java code do not match. Please add the layout of "Second" and the whole java code of "Second", if you want to keep your question short you could remove some of the stack trace :) – Bö macht Blau Jan 22 '16 at 17:46
  • @0X0nosugar No. The methods names are the same in both files. See the updated question. – Mohammad Jan 23 '16 at 15:43
  • You and I know that you basically know how to write an OnClickListener and most likely it's something like a curly bracket in the wrong place which makes your app crash. Now remote debugging is not exactly efficient. So if you like you can [copy your code to chat](http://chat.stackoverflow.com/rooms/101472/discussion-between-0x0nosugar-and-mohammad-k) – Bö macht Blau Jan 23 '16 at 15:55

2 Answers2

1

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

Community
  • 1
  • 1
Bö macht Blau
  • 11,639
  • 4
  • 30
  • 53
  • I tried removing `android:theme` and the program worked correctly. But using `AppCompatActivity` as parent of all activities, especially for `Second`, does not solve the problem. Why? Should I change anything else - except activities themselves - to `AppCompatActivity` ? – Mohammad Jan 25 '16 at 12:08
  • @mohammad .k - yeah, you caught me there. That was an obviously uneducated guess and it does not work. So I guess (no, I'm sure ;-) ) my answer has to be edited yet again. – Bö macht Blau Jan 25 '16 at 12:16
0

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?

Passing data

Android intent

Best wishes..!!

Community
  • 1
  • 1
Ragu Swaminathan
  • 3,507
  • 22
  • 28