gpt4 book ai didi

java - 从 Android 中的非 Activity 类调用方法

转载 作者:行者123 更新时间:2023-12-02 12:29:01 26 4
gpt4 key购买 nike

我有一个弹出窗口(非 Activity 类),其中有一些按钮。其中之一(btn_audio),播放或暂停声音,它就像一个魅力。
现在我想在我的 HomeActivity 中调用两个方法, play()pause() 以及在我的所有 Activity 中的 onStop() 方法内部 Activity 。不起作用,在其他 Activity 中调用 play()pause() 会发生 NullPointException。

public class Popup_Menu extends Activity implements OnClickListener {
Button btn_audio;
public static MediaPlayer player; String play_or_pause;
int num_ply, tim_pos; int [] resID;// Audio
SharedPreferences sp;
void showPopup(final Activity context) {

this.context=context;
LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = layoutInflater.inflate(R.layout.popup_layout_menu, viewGroup);
sp = context.getSharedPreferences("text", MODE_PRIVATE);
btn_audio = (Button) layout.findViewById(R.id.btn_audio); // audio
layout.findViewById(R.id.btn_audio).setOnClickListener(this);


@Override
public void onClick(View whichButtonIsClicked) {
switch (whichButtonIsClicked.getId()) {
case R.id.btn_audio:
if (play_or_pause.equals("play")) {
pause();
} else {
play(context);
}
sp.edit().putString("play_or_pause", play_or_pause).commit(); // save current stat
} ////// close of showPopup()

//define play and pause methods
public void pause() {
player.pause();
btn_audio.setBackgroundResource(R.drawable.icon_audio_pause);
play_or_pause="pause"; // current state is 'pause'
}

public void play(final Context context) {
if(num_ply==9){num_ply=0; }
resID = new int []{ R.raw.tarane,R.raw.toofan,R.raw.shane,R.raw.kharazmi,R.raw.golhaye_khofte,R.raw.rang,R.raw.naghmeh,R.raw.dar_rahe_to,R.raw.emperor};
player=MediaPlayer.create(context,resID[num_ply]);
player.seekTo(tim_pos);
player.start();
btn_audio.setBackgroundResource(R.drawable.icon_audio_play);
play_or_pause="play"; // current state is 'play'
player.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer player) {
tim_pos=0; num_ply++; play(context);
}
});
}
}

和我的 HomeActivity:

public class HomeActivity extends Activity implements OnClickListener {
Popup_Menu ppp;
@Override
protected void onCreate..............
ppp = new Popup_Menu();
SharedPreferences sp = getSharedPreferences("text", MODE_PRIVATE);

if(sp.getString("play_or_pause", "play").equals("pause")){
ppp.play(getApplicationContext()); // ERROR
} // audio

} /// close of onCreate

@Override
public void onStop() {
super.onStop();
if(play_or_pause.equals("play")){
ppp.tim_pos= ppp.player.getCurrentP

好吧,我决定使用服务来解决这个问题,它对于 play() 来说效果很好。但是在播放时,我仍然无法从 SoundService 调用暂停()方法,因为播放器对象为空。我认为通过从 SoundService 类(Popup_Menu 类中的 ss)创建一个对象,默认构造函数将玩家值设置为 null(当播放且玩家对象不为 null 时)。并调用player.pause发生错误。请让我暂停这个 one_week_friend!!!

public class SoundService extends Service {

MediaPlayer player; int num_ply, tim_pos; int [] resID;


public int onStartCommand(Intent intent, int flags, int startId) {

resID = new int []{ R.raw.tarane,R.raw.toofan,R.raw.shane,R.raw.kharazmi,
R.raw.golhaye_khofte,R.raw.rang,R.raw.naghmeh,R.raw.dar_rahe_to,R.raw.emperor };
player=MediaPlayer.create(this,resID[num_ply]);

play();
return Service.START_FLAG_REDELIVERY;
}

public void play() {
if(num_ply==9){num_ply=0; }
player.seekTo(tim_pos);
player.start();
player.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer player) {
tim_pos=0; num_ply++; play();
}
});
}

public void resume(){
player.seekTo(tim_pos);
player.start();
}

public void pause() {
tim_pos= player.getCurrentPosition(); ////// ERROR
player.pause(); ////// ERROR
num_ply++;
}


public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}

还有我的 Popup_Menu 类:

@Override
public void onClick(View whichButtonIsClicked) {
switch (whichButtonIsClicked.getId()) {

case R.id.btn_audio:
if (play_or_pause.equals("play")) {
btn_audio.setBackgroundResource(R.drawable.icon_audio_pause);
play_or_pause="pause"; // current state is 'pause'
SoundService ss = new SoundService();
ss.pause(); ////// ERROR
} else {
btn_audio.setBackgroundResource(R.drawable.icon_audio_play);
play_or_pause="play"; // current state is 'play'
Intent sound_Intent = new Intent(context , SoundService.class); // start service
context.startService(sound_Intent);
}
sp.edit().putString("play_or_pause", play_or_pause).commit(); // save current state
break;

日志猫:

08-05 01:03:31.440: E/AndroidRuntime(25356): FATAL EXCEPTION: main
08-05 01:03:31.440: E/AndroidRuntime(25356): java.lang.NullPointerException
08-05 01:03:31.440: E/AndroidRuntime(25356): at com.codegostarNiloo.negar.SoundService.pause(SoundService.java:47)
08-05 01:03:31.440: E/AndroidRuntime(25356): at com.codegostarNiloo.negar.Popup_Menu.onClick(Popup_Menu.java:234)
08-05 01:03:31.440: E/AndroidRuntime(25356): at android.view.View.performClick(View.java:4209)
08-05 01:03:31.440: E/AndroidRuntime(25356): at android.view.View$PerformClick.run(View.java:17457)
08-05 01:03:31.440: E/AndroidRuntime(25356): at android.os.Handler.handleCallback(Handler.java:725)
08-05 01:03:31.440: E/AndroidRuntime(25356): at android.os.Handler.dispatchMessage(Handler.java:92)
08-05 01:03:31.440: E/AndroidRuntime(25356): at android.os.Looper.loop(Looper.java:153)
08-05 01:03:31.440: E/AndroidRuntime(25356): at android.app.ActivityThread.main(ActivityThread.java:5341)
08-05 01:03:31.440: E/AndroidRuntime(25356): at java.lang.reflect.Method.invokeNative(Native Method)
08-05 01:03:31.440: E/AndroidRuntime(25356): at java.lang.reflect.Method.invoke(Method.java:511)
08-05 01:03:31.440: E/AndroidRuntime(25356): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:929)
08-05 01:03:31.440: E/AndroidRuntime(25356): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
08-05 01:03:31.440: E/AndroidRuntime(25356): at dalvik.system.NativeStart.main(Native Method)
08-05 01:03:32.066: D/dalvikvm(25356): threadid=11: interp stack at 0x5f1c9000

最佳答案

首先,如果它是非 Activity ,则不要扩展 Activity ,正如您所说,那么传递上下文优于 Activity

其次,有一个名为 DialogFragment 的类,旨在用作弹出对话框

不过,我认为您正在寻找类似以下内容的内容

public class Popup_Menu implements OnClickListener {
Button btn_audio;
public MediaPlayer player;
String play_or_pause;
int num_ply, tim_pos;
int [] resID;// Audio
SharedPreferences sp;
Context context ;

public Popup_Menu(final Context context) {

this.context= context;
sp = this.context.getSharedPreferences("text", Context.MODE_PRIVATE);

// LinearLayout viewGroup = (LinearLayout) view.findViewById(R.id.popup);

LayoutInflater layoutInflater = LayoutInflater.from(this.context);
layout = layoutInflater.inflate(R.layout.popup_layout_menu, viewGroup);
btn_audio = (Button) layout.findViewById(R.id.btn_audio); // audio
btn_audio.setOnClickListener(this);
}

您现在可以创建一个新的 Popup_Menu(HomeActivity.this)

关于java - 从 Android 中的非 Activity 类调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45374508/

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