Android Studio用模拟器运行时显示不出ImageView里的图片

在android 开发中碰到从一个activity转到第二个activity时

第一个activity中的imageView图片可以显示

第二个activity中的imageView无法显示,但是可以显示背景色

求大神解答原因

mainActivity

package com.example.yuexiangpu2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;

import java.io.File;
import java.io.*;

public class MainActivity extends AppCompatActivity  implements View.OnClickListener{
    private Button btnok;
    private Button btnrecord;
    private Button btncancel;
    private Button btnlisten;
    private MyMediaPlayer myMediaPlayer;
    private MyMediaRecorder myMediaRecorder;
    private File soundFile;

//选择视图
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

//初始化视图
    private void initView() {
        btnok = (Button) findViewById(R.id.btnok);
        btnrecord = (Button)this.findViewById(R.id.btnrecord);
        btncancel = (Button)this.findViewById(R.id.btncancel);
        btnlisten = (Button)this.findViewById(R.id.btnlisten);

//btnok跳转视图
        btnok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, FinishRecordActivity.class);
                startActivity(intent);
            }
        });

第二个activity

package com.example.yuexiangpu2;

import android.app.Activity;
import android.os.Bundle;

public class FinishRecordActivity extends Activity{

//设置视图
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_finishrecord);
    }

}

MainActivity的layout

<?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"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/imgrecording"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_alignParentTop="true"
        android:background="#2B2B2B"
        android:contentDescription="@string/app_name"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.299"
        app:srcCompat="@drawable/recording" />

    <Button
        android:id="@+id/btnrecord"
        android:layout_width="220dp"
        android:layout_height="220dp"
        android:layout_centerHorizontal="true"
        android:background="#00000000"
        app:layout_constraintBottom_toBottomOf="@+id/imgrecording"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/imgrecording"
        app:layout_constraintVertical_bias="1.0" />

    <Button
        android:id="@+id/btnok"
        android:layout_width="150dp"
        android:layout_height="50dp"
        android:layout_marginTop="56dp"
        android:background="@drawable/ok"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imgrecording" />

    <Button
        android:id="@+id/btncancel"
        android:layout_width="150dp"
        android:layout_height="50dp"
        android:layout_marginStart="68dp"
        android:layout_marginLeft="68dp"
        android:layout_marginBottom="48dp"
        android:background="@drawable/cancel"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.33"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btnok"
        app:layout_constraintVertical_bias="1.0" />

    <Button
        android:id="@+id/btnlisten"
        android:layout_width="150dp"
        android:layout_height="50dp"
        android:background="@drawable/listen"
        app:layout_constraintBottom_toTopOf="@+id/btncancel"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btnok"
        app:layout_constraintVertical_bias="0.54" />
</androidx.constraintlayout.widget.ConstraintLayout>

第二个activity的layout

<?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"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/imgrecording"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_alignParentTop="true"
        android:contentDescription="@string/app_name"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.299"
        app:srcCompat="@drawable/recording" />


    <Button
        android:id="@+id/btnconvert"
        android:layout_width="150dp"
        android:layout_height="50dp"
        android:layout_marginTop="76dp"
        android:background="@drawable/convert"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imgrecording" />

    <Button
        android:id="@+id/btnsave"
        android:layout_width="150dp"
        android:layout_height="50dp"
        android:background="@drawable/save"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btnconvert"
        app:layout_constraintVertical_bias="0.33" />

</androidx.constraintlayout.widget.ConstraintLayout>

1个回答

让两个view的id不同试一试。

nihuayewoa
nihuayewoa 你好~找到问题了,是activity里头文件的关系
5 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐