gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 05:40:57 25 4
gpt4 key购买 nike

所以我对 Android 还很陌生。让我先解释一下:我正在尝试制作一个小型可移植音乐应用程序,仅适用于我的手机并测试我的技能/回顾我所学到的知识。我的主要 Activity 设置为使用 4 个按钮中的一个选择一首歌曲,它将启动另一个 Activity ,其中的按钮用于暂停、恢复和返回歌曲选择屏幕(MainActivity)。我正在尝试使用后退按钮来释放玩家并完成 Activity ,并且我尝试了很多不同的方法,但似乎没有任何效果;应用程序由于某些异常而关闭(常见的异常是 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 Activity :

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

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

最佳答案

我建议首先确保该 Activity 位于 AndroidManifest 文件中。其次是改变你的 Intent :

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

进入:

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

返回MainActivity 时,在songPickedActivity 上也执行此操作。您可以使用 putExtra 将数据发送到下一个 Intent 。欲了解更多信息,请点击link .

关于java - Android - 单击另一个类中的按钮后如何重新启动 MainActivity?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24521902/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com