Question

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"

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top