-1

I read some posts in the forum, but they didn't work for me...

I have two buttons in the Main, and I want that each one open another acivity when pressed...

Here is my code:

In the code for when one button is clicked:

public void onClick(View v)
{
switch (v.getId()) {
    case R.id.button1:
        Intent intent = new Intent(this, Class1.class);
        startActivity(intent);
         break;

    case R.id.button2:
        Intent intentC = new Intent(this, Class2.class);
        startActivity(intentC);
        break;
    }
}

This is my code for the buttons, in XML file:

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button1"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/button1"
    android:layout_marginTop="25dp"
    android:onClick="onClick"
    android:text="Button 2" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="42dp"
    android:onClick="onClick"
    android:text="Button 1" />

And this is what I put on AndroidManifest (the name of the activities):

<activity android:name="MyActivity1"></activity>
<activity android:name="MyActivity2" ></activity>

Someone could tell what is wrong or missing here?

When I click in one button, the program stops... a message box appears: "unfortunately, app stopped"

  • 1
    what error do you have? why do you think this is not working ? – Lena Bru Jan 14 '14 at 21:23
  • You should detail a little bit more what's happening when you click one of them – nKn Jan 14 '14 at 21:23
  • 1
    Is `onClick(View v)` a method defined directly as part of your main `Activity` or is it part of an inline / anonymous definition of an `OnClickListener`? Show us how you set the listener for your buttons. – Squonk Jan 14 '14 at 21:26
  • 1
    @Squonk I thought that was the problem at first but its declared in the xml. – codeMagic Jan 14 '14 at 21:28
  • 1
    @codeMagic : Indeed it is - that'll teach me not to skim read. – Squonk Jan 14 '14 at 21:29
  • what does the logcat says? – Alejandro Cumpa Jan 14 '14 at 22:13
  • Can you maybe post the complete AndroidManifest.xml and the top part of your class file where you try to start the activity, i.e. from "package foo..." to "public class bar..." ? – bgse Jan 14 '14 at 22:37
  • Possible duplicate of [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – Zoe Aug 24 '19 at 11:10

1 Answers1

1

For some reason, although you have declared your activities in the AndroidManifest, it's not matching. I'd suggest trying to type the full name of your package into the AndroidManifest name, so: com.eggakin.Activity1

nKn
  • 13,691
  • 9
  • 45
  • 62