我的Android应用

时间:2017-06-08 06:59:39

标签: java android

我是android开发的新开发者我在Play商店中有两个应用程序,但我的应用程序现在无法使用android 6& android 7我收到此消息

  

的RuntimeException   和NullPointerException

这是我的主要活动代码,我遇到了问题:

public class Recored extends Activity {     

Button rec,stop;
private MediaRecorder myRecords;
private String outputFile = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.recored);

    stop = (Button)findViewById(R.id.bStop);
    rec = (Button)findViewById(R.id.bRecord);

    stop.setEnabled(false);
    myRecords = new MediaRecorder();
    myRecords.setAudioSource(MediaRecorder.AudioSource.MIC);
    myRecords.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    myRecords.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
    Date createdDate = new Date();
    File myDir = new File(Environment.getExternalStorageDirectory()+"/Recordings");
    if (!myDir.exists()){
        if (myDir.mkdir());
    }
        outputFile = myDir.getAbsolutePath() + "/" + createdDate + ".mp3";
        myRecords.setOutputFile(outputFile);

        rec.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    myRecords.prepare();
                    myRecords.start();


                }

                catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
                rec.setEnabled(false);
                stop.setEnabled(true);
                Toast.makeText(getApplicationContext(),"Recording started",Toast.LENGTH_LONG).show();
            }
        });
        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myRecords.stop();
                myRecords.release();
                myRecords = null;
                stop.setEnabled(false);
                rec.setEnabled(true);
            }
        });


}

}

2 个答案:

答案 0 :(得分:0)

您是否要求获得运行时权限?

 private static final int REQUEST_RECORD_AUDIO_PERMISSION = 200;

  private String[] permissions = {Manifest.permission.RECORD_AUDIO};

//将此代码添加到OnCreate

ActivityCompat.requestPermissions(this, permissions, REQUEST_RECORD_AUDIO_PERMISSION);

覆盖此方法

  @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case REQUEST_RECORD_AUDIO_PERMISSION:
                permissionToRecordAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
                break;
        }
        if (!permissionToRecordAccepted) finish();

    }

答案 1 :(得分:0)

需要在android 6&amp ;;上处理运行时权限上层版。您必须要求许可并根据许可被拒绝或接受来管理您的代码。

您可以参考以下链接获取运行时权限 https://developer.android.com/training/permissions/requesting.html