当回到活动应用程序崩溃

时间:2014-04-01 07:27:19

标签: android

我的代码正常工作并播放主要活动的音乐背景,但是在我的代码中通过按钮返回到主要活动时通过按钮,应用程序崩溃和强制关闭按钮进入另一个活动。

我能做什么?

请帮帮我

这是我的活动:

public class Main extends Activity implements OnClickListener {
AnimationDrawable anim;
MediaPlayer player;
BackgroundSound mBackgroundSound = new BackgroundSound();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ImageView iv = (ImageView) findViewById(R.id.imageView1);
    iv.setBackgroundResource(R.anim.animation);
    anim = (AnimationDrawable) iv.getBackground();
    ImageButton btnfehrest=(ImageButton) findViewById(R.id.btnfehrest);
    btnfehrest.setOnClickListener(this);
    ImageButton btndarbareh=(ImageButton) findViewById(R.id.btndarbareh);
    btndarbareh.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    switch(v.getId())
    {
    case R.id.btnfehrest :
        Intent infehrest=new Intent(Main.this,Fehrest.class);
        startActivity(infehrest);
        break;
    case R.id.btndarbareh :
        Intent indarbareh=new Intent(Main.this,DarbarehMain.class);
        startActivity(indarbareh);
        break;
    }
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if (hasFocus) {
        anim.start();
    } else {
        anim.stop();
    }
}
public void onResume() {
    super.onResume();
    mBackgroundSound.execute(null);
}
public class BackgroundSound extends AsyncTask<Void, Void, Void> {
    protected Void doInBackground(Void... params) {
        player = MediaPlayer.create(Main.this, R.raw.chera); 
        player.setLooping(true); // Set looping 
        player.setVolume(100,100); 
        player.start(); 
        return null;
    }
}
 }

和我的logcat:

04-02 12:04:04.522: E/AndroidRuntime(1245): FATAL EXCEPTION: main
04-02 12:04:04.522: E/AndroidRuntime(1245): java.lang.RuntimeException: Unable to resume activity {com.aseman14.fatemiye/com.aseman14.fatemiye.Main}: java.lang.IllegalStateException: Cannot execute task: the task has already been executed (a task can be executed only once)
04-02 12:04:04.522: E/AndroidRuntime(1245):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2120)
04-02 12:04:04.522: E/AndroidRuntime(1245):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2135)
04-02 12:04:04.522: E/AndroidRuntime(1245):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:957)
04-02 12:04:04.522: E/AndroidRuntime(1245):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-02 12:04:04.522: E/AndroidRuntime(1245):     at android.os.Looper.loop(Looper.java:130)
04-02 12:04:04.522: E/AndroidRuntime(1245):     at android.app.ActivityThread.main(ActivityThread.java:3683)
04-02 12:04:04.522: E/AndroidRuntime(1245):     at java.lang.reflect.Method.invokeNative(Native Method)
04-02 12:04:04.522: E/AndroidRuntime(1245):     at java.lang.reflect.Method.invoke(Method.java:507)
04-02 12:04:04.522: E/AndroidRuntime(1245):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-02 12:04:04.522: E/AndroidRuntime(1245):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-02 12:04:04.522: E/AndroidRuntime(1245):     at dalvik.system.NativeStart.main(Native Method)
04-02 12:04:04.522: E/AndroidRuntime(1245): Caused by: java.lang.IllegalStateException: Cannot execute task: the task has already been executed (a task can be executed only once)
04-02 12:04:04.522: E/AndroidRuntime(1245):     at android.os.AsyncTask.execute(AsyncTask.java:383)
04-02 12:04:04.522: E/AndroidRuntime(1245):     at com.aseman14.fatemiye.Main.onResume(Main.java:68)
04-02 12:04:04.522: E/AndroidRuntime(1245):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1150)
04-02 12:04:04.522: E/AndroidRuntime(1245):     at android.app.Activity.performResume(Activity.java:3832)
04-02 12:04:04.522: E/AndroidRuntime(1245):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2110)
04-02 12:04:04.522: E/AndroidRuntime(1245):     ... 10 more

4 个答案:

答案 0 :(得分:0)

我想已经开始的asynctask,BackgroundSound试图重新开始,因为它是在onResume中定义的。因此它崩溃了。在onCreate中定义然后尝试。我想它会解决你的问题。

答案 1 :(得分:0)

您无法重复使用任务。你必须每次都创建一个新的。

替换

mBackgroundSound.execute(null);

使用:

mBackgroundSound = new BackgroundSound();
mBackgroundSound.execute(null);

答案 2 :(得分:0)

尝试从onResume删除以下行并进行检查。

mBackgroundSound.execute(null);

因为您尝试运行已在运行的任务。因此,在开始执行onResume之前,请检查任务是否正在运行状态。或者尝试在staet再次执行之前停止它,然后执行任务。我不知道这是否正确。无论如何让我知道发生了什么。

答案 3 :(得分:0)

问题是,如果您使用该异步任务实例在类中定义Asynch任务实例,您只能在活动中执行一次,因此您将尝试创建您在课程中需要的实例我将建议此答案它肯定会工作....

public void onResume() {
    super.onResume();
    BackgroundSound mBackgroundSound = new BackgroundSound();
    mBackgroundSound.execute(null);
}
相关问题