Search code examples
androidandroid-permissionsandroid-resourcesuser-permissions

Error accessing R resource after moving project


I have incomplete project source files. I filled a newly created project with these. But now I can't access the R.string resource. By the way; I would like to point out that I have also tried all the solutions listed on the https://www.geeksforgeeks.org/different-ways-to-fix-cannot-resolve-symbol-r-in-android-studio/ website.

I am getting the following errors.

Errors

error: cannot find symbol

Toast.makeText(this, R.string.permission_audio, Toast.LENGTH_SHORT).show();

^ symbol: variable permission_audio location: class string

protected void checkPermissionResult(final int requestCode, final String permission, final boolean result) {
        // show Toast when there is no permission
        if (Manifest.permission.RECORD_AUDIO.equals(permission)) {
            onUpdateAudioPermission(result);
            if (!result) {
                Toast.makeText(this, R.string.permission_audio, Toast.LENGTH_SHORT).show();
            }
        }
        if (Manifest.permission.WRITE_EXTERNAL_STORAGE.equals(permission)) {
            onUpdateExternalStoragePermission(result);
            if (!result) {
                Toast.makeText(this, R.string.permission_ext_storage, Toast.LENGTH_SHORT).show();
            }
        }
        if (Manifest.permission.INTERNET.equals(permission)) {
            onUpdateNetworkPermission(result);
            if (!result) {
                Toast.makeText(this, R.string.permission_network, Toast.LENGTH_SHORT).show();
            }
        }
    }

protected boolean checkPermissionWriteExternalStorage() {
        if (!PermissionCheck.hasWriteExternalStorage(this)) {
            MessageDialogFragment.showDialog(this, REQUEST_PERMISSION_WRITE_EXTERNAL_STORAGE,
                    R.string.permission_title, R.string.permission_ext_storage_request,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE});
            return false;
        }
        return true;
    }

protected boolean checkPermissionAudio() {
        if (!PermissionCheck.hasAudio(this)) {
            MessageDialogFragment.showDialog(this, REQUEST_PERMISSION_AUDIO_RECORDING,
                    R.string.permission_title, R.string.permission_audio_recording_request,
                    new String[]{Manifest.permission.RECORD_AUDIO});
            return false;
        }
        return true;
    }

AndroidManifest.xml

<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-feature android:glEsVersion="0x00020000" android:required="true" />

Strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">ScreenRecordingSample</string>

</resources>

Solution

  • I think you maybe lack the basic understanding of what R actually is. It basically is the way to refer to anything in your res folder. If you don't have a string called permission_audio you also can't use it. All these R.string values are values you need to define yourself, inside the project, in the res folder.

    Strings are typically defined inside res\values\strings.xml

    Inside you will find something like

    <resources>
        <string name="app_name">App name</string>
    </resources>
    

    Here all other strings also need to be defined, so if you change it to

    <resources>
        <string name="app_name">App name</string>
        <string name="permission_audio">Text for showing permission audio</string>
    </resources>
    

    you will notice that R.string.permission_audio won't give an error anymore