簡體   English   中英

Android應用程式在開啟時會關閉,但不會當機

[英]Android app closes when opened, but it doesn't crash

我當時正在開發一個Android Studio應用程序,並且一直運行到一定程度。 我添加了一個MediaRecorder來記錄音頻,並編寫了用於記錄和停止記錄的代碼。 當我嘗試測試代碼時,所有內容都會正確編譯並下載。 但是,當應用程序打開時,它將立即關閉。 檢查logcat時,似乎沒有明顯的錯誤。

Logcat:

2019-06-24 15:11:19.302 30658-30658/? I/art: Late-enabling -Xcheck:jni
2019-06-24 15:11:19.495 30658-30658/com.example.visualizercopy W/System: ClassLoader referenced unknown path: /data/app/com.example.visualizercopy-1/lib/arm64
2019-06-24 15:11:19.515 30658-30658/com.example.visualizercopy I/InstantRun: starting instant run server: is main process
2019-06-24 15:11:19.732 30658-30658/com.example.visualizercopy I/Timeline: Timeline: Activity_launch_request intent: act=android.content.pm.action.REQUEST_PERMISSIONS pkg=com.google.android.packageinstaller time:351586171
2019-06-24 15:11:19.887 30658-30722/com.example.visualizercopy I/Adreno: QUALCOMM build                   : d3015e9, I25dc76dc3f
    Build Date                       : 01/26/17
    OpenGL ES Shader Compiler Version: XE031.09.00.03
    Local Branch                     : 
    Remote Branch                    : refs/tags/AU_LINUX_ANDROID_LA.BF64.1.2.3_RB1.07.00.00.258.011
    Remote Branch                    : NONE
    Reconstruct Branch               : NOTHING
2019-06-24 15:11:19.895 30658-30722/com.example.visualizercopy I/OpenGLRenderer: Initialized EGL, version 1.4
2019-06-24 15:11:19.895 30658-30722/com.example.visualizercopy D/OpenGLRenderer: Swap behavior 1
2019-06-24 15:11:20.071 30658-30658/com.example.visualizercopy D/Editor: setInputTypeforClipTray(): 0
2019-06-24 15:11:20.157 30658-30658/com.example.visualizercopy D/Editor: setInputTypeforClipTray(): 0
2019-06-24 15:11:20.161 30658-30658/com.example.visualizercopy D/Editor: hideClipTrayIfNeeded() TextView is focused!! hideClipTray()
2019-06-24 15:11:20.336 30658-30658/com.example.visualizercopy W/IInputConnectionWrapper: reportFullscreenMode on inexistent InputConnection
2019-06-24 15:11:20.481 30658-30658/com.example.visualizercopy W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
2019-06-24 15:11:20.483 30658-30658/com.example.visualizercopy W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
2019-06-24 15:11:20.484 30658-30658/com.example.visualizercopy W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
2019-06-24 15:11:20.523 30658-30658/com.example.visualizercopy D/Editor: hideClipTrayIfNeeded() TextView is focused!! hideClipTray()
2019-06-24 15:11:20.612 30658-30658/com.example.visualizercopy W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
2019-06-24 15:11:20.615 30658-30658/com.example.visualizercopy W/IInputConnectionWrapper: reportFullscreenMode on inexistent InputConnection
2019-06-24 15:11:20.615 30658-30658/com.example.visualizercopy W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
2019-06-24 15:11:20.620 30658-30658/com.example.visualizercopy W/IInputConnectionWrapper: finishComposingText on inactive InputConnection

我試圖在網上尋找解決問題的方法,但似乎沒有一個可行。 我怎樣才能解決這個問題? -編輯-我添加的代碼:

public class MainActivity extends Activity implements ESenseConnectionListener {
        private static final int REQUEST_RECORD_AUDIO_PERMISSION = 200;
        private static String FILE_NAME_MIC="recording.3gp";
        private MediaRecorder recorder;
        private boolean permissionToRecordAccepted = false;
        private String [] permissions = {Manifest.permission.RECORD_AUDIO};

        @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();
       }
    private void startRecording() {
        recorder = new MediaRecorder();
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        recorder.setOutputFile(FILE_NAME_MIC);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

        try {
            recorder.prepare();
        } catch (IOException e) {
            Log.e("Record", "prepare() failed");
        }

        recorder.start();
    }
    private void stopRecording(){
        recorder.stop();
        recorder.release();
        recorder = null;
    }

搖籃:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    defaultConfig {
        applicationId "com.example.visualizercopy"
        minSdkVersion 23
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0-beta01'
    implementation 'com.android.support.constraint:constraint-layout:2.0.0-beta1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

表現:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:dist="http://schemas.android.com/apk/distribution"
    package="io.esense">

    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>

    <dist:module dist:instant="true" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

您不能像這樣將隨機名稱傳遞給MediaRecorder:

private static String FILE_NAME_MIC="recording.3gp";

FILE_NAME_MIC必須表示存儲空間中的某個位置。

final static String FILE_NAME_MIC = Environment.getExternalStorageDirectory() + "/" + "MyAppDirectory"+ "/recording.mp3";

現在我們知道我們想向/從設備寫入/讀取內容,但是我們不能僅僅這樣做。 我們需要獲得許可才能這樣做。 首先,您需要向清單中添加權限,例如:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>


<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

接下來,我們需要請求多個權限。 該鏈接可以教您所有需要了解的內容,以及更多:

MyMediaRecorder

結論:您的FILE_NAME_MIC和權限是“崩潰”的根源。

嘗試刪除:

if (!permissionToRecordAccepted ) finish();

看看會發生什么。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM