2015-08-08 160 views
0

我想寫我的應用程序第二個頁面,其中包含3個按鈕。每個人都打開新的活動,這是我的代碼和我的xml的第二類,我的清單代碼沒有工作。在android工作室的第二個活動中打開第三個活動

我的課

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(); 
} 

}

我的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> 

我的清單

<?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> 

回答

0

您在這裏混合了兩兩件事; onOptionsItemSelected用於處理菜單項點擊,而不是按鈕點擊。刪除onOptionsItemSelected方法,並直接在onCreate方法中使用按鈕點擊邏輯。這樣的事情:

@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)); 
     } 
    }); 
} 
+0

對不起,新的聽到請告訴我在哪裏? – SARA

+0

我可以問其他問題嗎? – SARA

+0

這是我的一個活動點擊BackPressed我已經寫了一個函數,但它關閉漏洞的應用程序,並沒有回到先前的活動 – SARA

相關問題