【问题标题】:How to solve this error: Android resource linking failed?如何解决此错误:Android 资源链接失败?
【发布时间】:2020-01-10 14:04:15
【问题描述】:

下面这两个代码在我的 Android Studio 项目中的同一个 MainActivity 中

1) 将智能手机中的音频重新编码并在按下播放按钮时播放:

public class Main2Activity extends AppCompatActivity {

    //Declare variables
    Button btnRecord, btnStopRecord, btnPlay, btnStop;
    //String pathSave ="";
    private static String pathSave;
    MediaRecorder mediaRecorder;
    MediaPlayer mediaPlayer;

    final int REQUEST_PERMISSION_CODE=1000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        //Request Runtime permission
        if(!checkPermissionFromDevice()){
            requestPermission();
        }


        //Init View
        btnPlay = (Button) findViewById(R.id.btnPlay);
        btnRecord = (Button) findViewById(R.id.btnStartRecord);
        btnStop = (Button) findViewById(R.id.btnStop);
        btnStopRecord = (Button) findViewById(R.id.btnStopRecord);



        btnRecord.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick (View view){

                if (checkPermissionFromDevice())
                {

                    pathSave = Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+ UUID.randomUUID().toString()+"audio_record.3gp";
                    setupMediaRecorder();
                    try{
                        mediaRecorder.prepare();
                        mediaRecorder.start();
                    }  catch (IOException e){
                        e.printStackTrace();
                    }

                    btnPlay.setEnabled(false);
                    btnStop.setEnabled(false);

                    Toast.makeText(Main2Activity.this, "Recording...", Toast.LENGTH_SHORT).show();


                }

                else{

                    requestPermission();
                }
            }
        });


        btnStopRecord.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mediaRecorder.stop();
                btnStopRecord.setEnabled(false);
                btnPlay.setEnabled(true);
                btnRecord.setEnabled(true);
                btnStop.setEnabled(false);
            }
        });

        btnPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                btnStop.setEnabled(true);
                btnStopRecord.setEnabled(false);
                btnRecord.setEnabled(false);

                mediaPlayer = new MediaPlayer();
                try{
                    mediaPlayer.setDataSource(pathSave);
                    mediaPlayer.prepare();
                } catch (IOException e){
                    e.printStackTrace();
                }

                mediaPlayer.start();
                Toast.makeText(Main2Activity.this, "Playing...", Toast.LENGTH_SHORT).show();
            }
        });

        btnStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                btnStopRecord.setEnabled(false);
                btnRecord.setEnabled(true);
                btnStop.setEnabled(false);
                btnPlay.setEnabled(true);

                if (mediaPlayer != null){

                    mediaPlayer.stop();
                    mediaPlayer.release();
                    setupMediaRecorder();
                }
            }
        });


    }

    private void setupMediaRecorder(){
        mediaRecorder = new MediaRecorder();
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mediaRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
        mediaRecorder.setOutputFile(pathSave);
    }

    private void requestPermission() {
        ActivityCompat.requestPermissions(this, new  String[]{
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.RECORD_AUDIO
        } ,REQUEST_PERMISSION_CODE);

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode)
        {
            case REQUEST_PERMISSION_CODE:
            {
                if(grantResults.length>0 && grantResults[0]== PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(this, "Permission Granted", Toast.LENGTH_SHORT).show();
                }
                else {
                    Toast.makeText( this, "Permission Denied", Toast.LENGTH_SHORT).show();
                }
            }
            break;
        }
    }

    private boolean checkPermissionFromDevice(){
        int write_external_storage_result = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
        int record_audio_result = ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO);
        return write_external_storage_result == PackageManager.PERMISSION_GRANTED &&
                record_audio_result == PackageManager.PERMISSION_GRANTED;

    }

2) 保存创建音频文件的目录并将其转换为矢量:

String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+UUID.randomUUID().toString()+"audio_record.3gp";

public byte[] convert(String path) throws IOException {

    FileInputStream fis = new FileInputStream(path);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] b = new byte[1024];

    for (int readNum; (readNum = fis.read(b)) != -1; ) {
        bos.write(b, 0, readNum);
    }

    byte[] bytes = bos.toByteArray();

    return bytes;

}

问题

构建输出:

构建:构建失败

AAPT 错误:(1 个错误)

Android 资源链接失败

错误信息示例: (1) AAPT: C:\Users\cmorais.gradle\caches\transforms-2\files-2.1\cb634fe4b4d1fdfcd5255e485d30a0c1\material- 1.0.0\res\anim-v21\design_bottom_sheet_slide_in.xml:17:错误:找不到资源整数/bottom_sheet_slide_duration(又名 com.example.appsom:integer/bottom_sheet_slide_duration)。 (2) C:\Users\cmorais.gradle\caches\transforms-2\files-2.1\cb634fe4b4d1fdfcd5255e485d30a0c1\material-1.0.0\res\anim-v21\design_bottom_sheet_slide_out.xml:17: 错误:找不到资源整数/bottom_sheet_slide_duration(又名 com.example.appsom:integer/bottom_sheet_slide_duration)。 – moraiscarolinav 2 天前
(3) C:\Users\cmorais.gradle\caches\transforms-2\files-2.1\cb634fe4b4d1fdfcd5255e485d30a0c1\material-1.0.0\res\animator-v21 \design_appbar_state_list_animator.xml:19:错误:找不到属性 state_liftable(又名 com.example.appsom:state_liftable)。 (4) C:\Users\cmorais.gradle\caches\transforms-2\files-2.1\cb634fe4b4d1fdfcd5255e485d30a0c1\material-1.0.0\res\animator-v21\design_appbar_state_list_animator.xml:19: 错误:未找到属性 state_lifted(又名 com.example.appsom:state_lifted)。 ... 该项目有 (20) 个错误消息,末尾带有 “未找到” p>

3) 我的 MainActivity XML 文件(文本):

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/maxwellBackground"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btnStartRecord"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="60dp"
        android:background="#FC9B03"
        android:text="@string/button_start"
        android:textColor="@android:color/background_light"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView2" />

    <Button
        android:id="@+id/btnStopRecord"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="55dp"
        android:background="#FC9B03"
        android:text="@string/button_stop"
        android:textColor="@android:color/background_light"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/textView3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btnStartRecord"
        app:layout_constraintVertical_bias="0.161" />

    <Button
        android:id="@+id/btnPlay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/ColorYellow"
        android:text="@string/button_play"
        android:textColor="@android:color/background_light"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/btnStop"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.507"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView3"
        app:layout_constraintVertical_bias="0.504" />

    <Button
        android:id="@+id/btnStop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="128dp"
        android:background="@color/ColorYellow"
        android:text="@string/button_stop_play"
        android:textColor="@android:color/background_light"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.507"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/record_area"
        android:textColor="@android:color/background_light"
        android:textSize="24sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.088" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/play_area"
        android:textColor="@android:color/background_light"
        android:textSize="24sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.573" />


</androidx.constraintlayout.widget.ConstraintLayout>

按钮关联(XML 设计)

[https://i.stack.imgur.com/uiO6F.png][1]

【问题讨论】:

  • 你能分享你的xml文件吗?
  • @JDevoloper 如何在这里分享?
  • 您可以更新您的问题,我想您的问题出在您的 xml 文件中。我的意思是你的 logcat 中没有提到的文件。请检查@Muhammed 的答案。是这样的。您也可以修复从 xml 文件中删除这些值的问题。
  • 不要贴代码,错误日志在评论区。评论旨在要求澄清或建议对帖子进行改进。也不要发布问题作为答案。取而代之的是,请edit(下面的按钮标记您的问题)提出问题并发布。
  • @Shashanth 好的,我用错误 cmets 编辑了我的帖子。谢谢。

标签: java android android-studio vector audio-recording


【解决方案1】:

不是名为 com.example.appsom:integer/bottom_sheet_slide_duration 的文件或资源)。 在项目的 \res\values 文件夹中创建 integers.xml 文件。在文件中创建你的整数:

<resources>
<integer name="bottom_sheet_slide_duration">value</integer></resources>

【讨论】:

  • 执行此操作我收到以下消息:“无法解析符号‘值’”
  • 在platform/android目录中搜索com.android.support:support-v4:+并将其替换为com.android.support:support-v4:27.1.0。
猜你喜欢
  • 2022-01-24
  • 1970-01-01
  • 2020-02-29
  • 2019-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-09
  • 2022-10-13
相关资源
最近更新 更多