Android - 如何在单击另一个类中的按钮后重新启动MainActivity?

时间:2014-07-02 02:17:51

标签: java android button android-activity

所以我对Android很新。 Lemme首先解释一下: 我正在尝试制作一个小型便携式音乐应用程序,仅用于我的手机,并测试我的技能/审查我所学到的内容。我有一个主动设置,用4个按钮中的一个选择一首歌,然后它会启动另一个活动,按钮可以暂停,恢复并返回到歌曲选择屏幕(MainActivity)。我试图让后退按钮同时释放播放器并完成活动,我尝试了许多不同的东西,但似乎没有任何效果;应用程序因某些异常而关闭(常见的是NullPointerException)。

所以这里是MainActivity:

package me.lemmy.portablemusic.app;

import android.media.MediaPlayer;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class MainActivity extends ActionBarActivity {

Button wreckingBall, happy, lig, ligMulti;
Intent songPickedActivity = new Intent("me.lemmy.portablemusic.app.SONGPICKED");
public static MediaPlayer player;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    //set buttons
    wreckingBall = (Button) findViewById(R.id.song_wreck);
    happy = (Button) findViewById(R.id.song_happy);
    lig = (Button) findViewById(R.id.song_lig);
    ligMulti = (Button) findViewById(R.id.song_lig_multi);

    //listeners
    wreckingBall.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(songPickedActivity);
            player = MediaPlayer.create(MainActivity.this, R.raw.wrecking_ball);
            player.start();
        }
    });

    happy.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(songPickedActivity);
            player = MediaPlayer.create(MainActivity.this, R.raw.happy);
            player.start();
        }
    });

    lig.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(songPickedActivity);
            player = MediaPlayer.create(MainActivity.this, R.raw.let_it_go);
            player.start();
        }
    });

    ligMulti.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(songPickedActivity);
            player = MediaPlayer.create(MainActivity.this, R.raw.let_it_go_multi);
            player.start();
        }
    });
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

public static MediaPlayer getPlayer(){
    return player;
}

}

这是我的SongPicked活动:

package me.lemmy.portablemusic.app;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.media.MediaPlayer;
import android.widget.TextView;

/**
 * Created by Lemuel on 6/20/14.
 */
public class SongPicked extends Activity {

TextView text;
Button pause, resume, back;
MediaPlayer player;

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_songpicked);

    text = (TextView) findViewById(R.id.tvPlaying);
    pause = (Button) findViewById(R.id.buttonPause);
    resume = (Button) findViewById(R.id.buttonResume);
    back = (Button) findViewById(R.id.buttonBack);

    player = MainActivity.getPlayer();

    pause.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            player.pause();
        }
    });

    resume.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            player.start();
        }
    });

    back.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view){
            player.release();
        }
    });

}

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

感谢任何帮助,谢谢! 附:我知道我不能在我的应用程序中使用受版权保护的音乐,这只是一个测试。

1 个答案:

答案 0 :(得分:2)

我建议首先确保活动在AndroidManifest文件中。其次是改变你的意图:

Intent songPickedActivity = new Intent("me.lemmy.portablemusic.app.SONGPICKED");

成:

Intent songPickedActivity = new Intent(this, SongPicked.class);

回到MainActivity时,也可以在songPickedActivity上执行此操作。您可以使用putExtra将数据发送到下一个意图。有关详细信息,请单击此link