简体   繁体   English

在android studio中的第二个活动中打开第三个活动

[英]open third activity in second activity in android studio

I am trying to write my app second page which contains 3 button. 我试图写我的应用程序第二页,其中包含3个按钮。 Every one open new activity that is my code and my xml of second class and my manifest the code didn't work. 每个打开的新活动都是我的代码,我的第二类xml和清单是我的代码不起作用。

my class 我的课

package com.mccam.pianopro;

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

public class SecondPage extends Activity {
    private Button mButtonPlay;
    private MenuItem item;

    @Override
protected void onCreate(Bundle SecodState) {
    super.onCreate(SecodState);
    setContentView(R.layout.second);
    onOptionsItemSelected(item);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.piano:
        mButtonPlay = (Button)findViewById(R.id.piano);
        mButtonPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(SecondPage.this, MainActivity.class));
                finish();
            }
        });

        case R.id.other:
            mButtonPlay = (Button)findViewById(R.id.other);
            mButtonPlay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(SecondPage.this, OtherInd.class));
                    finish();
                }
            });

        case R.id.about_us:
            mButtonPlay = (Button)findViewById(R.id.about_us);
            mButtonPlay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(SecondPage.this, AboutUs.class));
                    finish();
                }
            });
    }
    return true;
}

@Override
public void onBackPressed() {
    super.onBackPressed();
    finish();
}

} }

my xml layout 我的xml布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/otherrrr"
android:weightSum="1">

    <LinearLayout
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_weight="0.22">

    <Button
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:layout_weight="20"
    android:background="@drawable/piano1"
    android:id="@+id/piano" />

    <Button
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:layout_weight="20"
    android:background="@drawable/other1"
    android:onClick=""
    android:id="@+id/other" />

    <Button
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:layout_weight="20"
    android:background="@drawable/about1"
    android:id="@+id/about_us" />
    </LinearLayout>

my manifest 我的清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mccam.pianopro"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="9"
    android:targetSdkVersion="22" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.mccam.pianopro.MainActivity"
        android:label="@string/app_name"
        android:screenOrientation="landscape" >

    </activity>

    <activity android:name="com.mccam.pianopro.SplashMain"
        android:label="@string/app_name"
        android:screenOrientation="landscape">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name="com.mccam.pianopro.SecondPage"
        android:label="@string/SecondPage"
        android:screenOrientation="portrait">

    </activity>

    <activity android:name="com.mccam.pianopro.OtherInd"
        android:label="@string/Other"
        android:screenOrientation="landscape">

    </activity>

    <activity android:name="com.mccam.pianopro.AboutUs"
        android:label="@string/AboutUs"
        android:screenOrientation="landscape">

    </activity>

    <activity
        android:name="com.google.android.gms.ads.AdActivity"
                    android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|    screenSize|smallestScreenSize"
        android:theme="@android:style/Theme.Translucent" />
</application>

</manifest>

You are mixing two things here; 您在这里混合了两件事; onOptionsItemSelected is used for handling menu item clicks, not button clicks. onOptionsItemSelected用于处理菜单项的单击,而不是按钮的单击。 Remove the onOptionsItemSelected method and directly use your button click logic in the onCreate method. 删除onOptionsItemSelected方法,并直接使用onCreate方法中的按钮单击逻辑。 Something like this: 像这样:

    @Override
protected void onCreate(Bundle SecodState) {
    super.onCreate(SecodState);
    setContentView(R.layout.second);

    Button mButtonPlay1, mButtonPlay2, mButtonPlay3;

    mButtonPlay1 = (Button)findViewById(R.id.piano);
    mButtonPlay1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(SecondPage.this, MainActivity.class));
        }
    });

    mButtonPlay2 = (Button)findViewById(R.id.other);
    mButtonPlay2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(SecondPage.this, OtherInd.class));
        }
    });

    mButtonPlay3 = (Button)findViewById(R.id.about_us);
    mButtonPlay3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(SecondPage.this, AboutUs.class));
        }
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM