onClick()不会去下一个Activity

时间:2014-01-17 09:43:08

标签: android

我正在尝试转到我的应用的下一个活动但是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>

4 个答案:

答案 0 :(得分:11)

您覆盖onClickListener的{​​{1}}。在您的代码中,您在同一个按钮上设置了两个不同的R.id.button1。由于是onClickListener操作,您将只获得最后一个实例

答案 1 :(得分: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); 
    }
});
}

}

答案 2 :(得分:1)

从您的代码中删除addListenerOnButton();并在您调用mp.start();的同一个lintner中启动活动,即

imageButton.setOnClickListener(new View.OnClickListener() {

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

答案 3 :(得分: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); 
        }
    });
}

你所做的是你覆盖了R.id.button1的onClickListener,这是不合适的。