app force关闭没有令人信服的理由

时间:2014-02-05 18:16:14

标签: android

以下代码运行良好且正常但是当我单击后面时,应用程序崩溃并强制关闭它。 (此代码用于来自sdcard的播放文件,如果没有下载显示提醒)

什么??

我该怎么办?

请帮帮我

import java.io.File;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

 public class AudioPlayer extends Activity implements OnClickListener {
 Button playButton;
 MediaPlayer player;
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 player = MediaPlayer.create(this,Uri.fromFile(audioFile));
 playButton = (Button) this.findViewById(R.id.ButtonPlayStop);
 playButton.setOnClickListener(this);
 }Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
 File sdcard = Environment.getExternalStorageDirectory();
 File audioFile = new File(sdcard.getPath() + "/bluetooth/یه سوال دارم مگه1.mp3");


public void onClick(View v) {


 if(audioFile.exists())
{

    if(player!=null)
    {
       if(player.isPlaying())
       {
          player.pause();
       }   
       else
       {
         player.start();
       }
    }
}
 else
  {
Builder alert = new AlertDialog.Builder( AudioPlayer.this);
                alert.setTitle("Alert");
                alert.setMessage("فایل دانلود نشده است");
                alert.setPositiveButton("OK", null);
                alert.show();
}

}

@Override
public void onBackPressed() {
if( player.isPlaying() ) {
 player.stop();
}
super.onBackPressed();
}
}

和我的logcat:

 02-05 21:49:28.319: E/AndroidRuntime(5063): FATAL EXCEPTION: main
 02-05 21:49:28.319: E/AndroidRuntime(5063): java.lang.NullPointerException
 02-05 21:49:28.319: E/AndroidRuntime(5063):    at          com.example.downloadplay.AudioPlayer.onBackPressed(AudioPlayer.java:64)
 02-05 21:49:28.319: E/AndroidRuntime(5063):    at android.app.Activity.onKeyUp(Activity.java:1898)
 02-05 21:49:28.319: E/AndroidRuntime(5063):    at android.view.KeyEvent.dispatch(KeyEvent.java:1290)
 02-05 21:49:28.319: E/AndroidRuntime(5063):    at android.app.Activity.dispatchKeyEvent(Activity.java:2078)
 02-05 21:49:28.319: E/AndroidRuntime(5063):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1722)
 02-05 21:49:28.319: E/AndroidRuntime(5063):    at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2627)
 02-05 21:49:28.319: E/AndroidRuntime(5063):    at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2602)
 02-05 21:49:28.319: E/AndroidRuntime(5063):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1874)
02-05 21:49:28.319: E/AndroidRuntime(5063):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-05 21:49:28.319: E/AndroidRuntime(5063):     at android.os.Looper.loop(Looper.java:130)
02-05 21:49:28.319: E/AndroidRuntime(5063):     at     android.app.ActivityThread.main(ActivityThread.java:3687)
02-05 21:49:28.319: E/AndroidRuntime(5063):     at java.lang.reflect.Method.invokeNative(Native Method)
 02-05 21:49:28.319: E/AndroidRuntime(5063):    at java.lang.reflect.Method.invoke(Method.java:507)
02-05 21:49:28.319: E/AndroidRuntime(5063):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
02-05 21:49:28.319: E/AndroidRuntime(5063):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
 02-05 21:49:28.319: E/AndroidRuntime(5063):    at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

只需检查你的onbackpressed()

中的玩家是否为空

答案 1 :(得分:0)

当您的文件不存在时,您的播放器对象为空。如果要从本地播放文件,则需要使用构造函数创建MediaPlayer。

就像那样:

public class MainActivity extends Activity {

    private MediaPlayer mPlayer;
    private File mAudioFile;

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

        File externalStorage = Environment.getExternalStorageDirectory();
        mAudioFile = new File(externalStorage.getPath() + "/bluetooth/یه سوال دارم مگه1.mp3");

        try {
            mPlayer = new MediaPlayer();
            mPlayer.setDataSource(mAudioFile.getAbsolutePath());
            mPlayer.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }

        findViewById(R.id.play_stop_button).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mAudioFile.exists()) {
                    if (mPlayer != null) {
                        if (mPlayer.isPlaying()) {
                            mPlayer.pause();
                        } else {
                            mPlayer.start();
                        }
                    }
                } else {
                    Builder alert = new AlertDialog.Builder(MainActivity.this);
                    alert.setTitle("Alert");
                    alert.setMessage("فایل دانلود نشده است");
                    alert.setPositiveButton("OK", null);
                    alert.show();
                }
            }
        });
    }

    @Override
    public void onBackPressed() {
        mPlayer.stop();
        mPlayer.release();
        mPlayer = null;
        super.onBackPressed();
    }
}