user1091368
user1091368

Reputation: 359

How do I activite a button to switch to another layout upon clicking?

This seems to be a simple function but I have read and reviewed countless tutorials and tried multiple times to get this to work and I am still unsuccessful.

I have the opening xml page titled "main" I have the 2nd xml page titled "menu" I have the 1st java activity titled "ButtontestingActivity.java" I have the 2nd java activity titled "Menu"

In ButtontestingActivity.java I have ...

package com.package2.Buttontesting;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class ButtontestingActivity extends Activity {


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

   Button Button1 = (Button) findViewById(R.id.sweetness);
   Button1.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            startActivity(new Intent("com.package2.Buttontesting.MENU"));
        }
    });

}
@Override
protected void onPause() {
    super.onPause();
}

}

in main.xml I have ...

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/splash"
android:orientation="vertical" >

<Button
    android:id="@+id/sweetness"
    android:layout_width="40dp"
    android:layout_height="78dp"
    android:layout_marginLeft="52dp"
    android:layout_marginTop="40dp"
    android:visibility="visible"
    />

</LinearLayout>

In the manifest I have ...

<application
    android:icon="@drawable/face"
    android:label="@string/app_name" >
    <activity
        android:label="@string/app_name"
        android:name=".ButtontestingActivity" >
        <intent-filter >
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:label="@string/app_name"
        android:name=".Menu" >
        <intent-filter >
            <action android:name="com.package2.Buttontesting.MENU" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

and I have android:background="@drawable/menu" in my menu.xml

when I install the app in my AVD the first layout shows up fine but when I press the button to open the second layout the AVD sends the message

"The application Buttontesting (process com.package2.Buttontesting)has stopped unexpectedly. Please try again"

and then I'm returned to the app menu.

Upvotes: 1

Views: 619

Answers (2)

Nataliia.dev
Nataliia.dev

Reputation: 2972

Try this:

Intent intent = new Intent(ButtontestingActivity.this,Menu.class);
startActivity(intent);

also remove this from manifest file:

 <intent-filter >
        <action android:name="com.package2.Buttontesting.MENU" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>

after

 <activity
    android:label="@string/app_name"
    android:name=".Menu" >

Upvotes: 2

Dmitry Zaytsev
Dmitry Zaytsev

Reputation: 23952

use

startActivity(new Intent(ButtontestingActivity.this, Menu.class));

or

startActivity(new Intent(ButtontestingActivity.this, com.package2.Buttontesting.Menu.class));

And also delete intent-filter block from your menu activity, because app can have only one launcher activity.

Upvotes: 0

Related Questions