2014-01-17 22 views
0

我想要去我的應用程序的下一個活動,但onClick無法進入下一個活動。這是我迄今爲止嘗試過的代碼。 onClick播放我剛剛添加的聲音。在它工作正常之前。onClick()不會去下一個活動

public class MainActivity extends Activity { 

ImageButton imageButton; 
Animation performAnimation1; 
ImageView androidImageView; 
MediaPlayer mp; 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    addListenerOnButton(); 
    performAnimation1 = AnimationUtils.loadAnimation(this, R.layout.animation1); 
    performAnimation1.setRepeatCount(4); 
    androidImageView = (ImageView)findViewById(R.id.androidImageView); 
    androidImageView.startAnimation(performAnimation1); 

    mp = MediaPlayer.create(this, R.raw.click); 
    imageButton = (ImageButton)this.findViewById(R.id.button1); 
    imageButton.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      mp.start(); 
     } 
    }); 
    } 

public void addListenerOnButton() { 
    imageButton = (ImageButton) findViewById(R.id.button1); 
    imageButton.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 

      Intent intent = new Intent(MainActivity.this, Numbers.class); 
      startActivity(intent); 
     } 
    }); 
} 

} 

Mainfest.xml

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

    <uses-sdk 
     android:minSdkVersion="1" 
     android:targetSdkVersion="18" /> 

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:screenOrientation="landscape" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.android.learning_numbers.MainActivity" 
      android:label="@string/app_name" 
      android:theme="@android:style/Theme.NoTitleBar.Fullscreen" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
     android:name=".Numbers" 
     android:theme="@android:style/Theme.NoTitleBar.Fullscreen" /> 

    </application> 

</manifest> 

回答

1

試試這個..

public class MainActivity extends Activity { 

ImageButton imageButton; 
Animation performAnimation1; 
ImageView androidImageView; 
MediaPlayer mp; 


@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 
addListenerOnButton(); 
performAnimation1 = AnimationUtils.loadAnimation(this, R.layout.animation1); 
performAnimation1.setRepeatCount(4); 
androidImageView = (ImageView)findViewById(R.id.androidImageView); 
androidImageView.startAnimation(performAnimation1); 

mp = MediaPlayer.create(this, R.raw.click); 
imageButton = (ImageButton)this.findViewById(R.id.button1); 
imageButton.setOnClickListener(new View.OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 

     mp.start(); 

Intent intent = new Intent(MainActivity.this, Numbers.class); 
     startActivity(intent); 
    } 
}); 
} 

} 
11

您覆蓋onClickListenerR.id.button1。在你的代碼中,你在同一個按鈕上設置了兩個不同的onClickListener。而且,由於是set操作將只得到最後一個實例

1

從您的代碼刪除addListenerOnButton();並在同一林特納開始活動,你在呼喚mp.start();

imageButton.setOnClickListener(new View.OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     mp.start(); 
     Intent intent = new Intent(MainActivity.this, Numbers.class); 
     startActivity(intent); 
    } 
}); 
} 
0

要麼刪除以下代碼從oncreate

imageButton = (ImageButton)this.findViewById(R.id.button1); 
    imageButton.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      mp.start(); 
     } 
    }); 

或者從你的活動

public void addListenerOnButton() { 
    imageButton = (ImageButton) findViewById(R.id.button1); 
    imageButton.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 

      Intent intent = new Intent(MainActivity.this, Numbers.class); 
      startActivity(intent); 
     } 
    }); 
} 

刪除代碼你做什麼,你是在overrided爲onClickListener這R.id.button1是不妥當的。