【问题标题】:Why does my bitmap image appears behind the alertdialog?为什么我的位图图像出现在警报对话框后面?
【发布时间】:2021-02-26 14:08:37
【问题描述】:

代码:

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    image_person = (ImageView) findViewById(R.id.dialog_imageview);
}

public void checkBox_checker(String checkerID, String checkerName, String checkerSurname, String 
checkerDescription, Blob picture) throws SQLException 
{
    int blobLength = (int) picture.length();
    byte[] bytes = picture.getBytes(1,blobLength);

    Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);

    Log.d(TAG, "Checker box");

    LayoutInflater factory = LayoutInflater.from(MainActivity.this);

    final View alertDialog= factory.inflate(R.layout.sample, null);
    image_person.setImageBitmap(bitmap);

    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

    builder.setCancelable(true);
    builder.setView(alertDialog);
    builder.setTitle("Information");
    builder.setMessage(message);


    builder.setNegativeButton("Report", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {

        }
    });

    builder.setPositiveButton("Enter", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            
        }
    });
    builder.show();
}

Sample.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">


<ImageView
    android:id="@+id/dialog_imageview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

</RelativeLayout>

我的问题是图像被插入到警报对话框的后面,而不是在警报对话框内。有人可以帮助我了解为什么会这样吗?

我在 xml 文件中输入了图像,它确实显示在警报对话框中。所以不太清楚为什么会这样。

【问题讨论】:

    标签: android bitmap imageview android-alertdialog


    【解决方案1】:

    看起来您在布局activity_main 中有一个名为dialog_imageview 的单独ImageView。否则,活动将无法在此处找到视图:

    image_person = (ImageView) findViewById(R.id.dialog_imageview);
    

    然后会在下面几行导致空指针异常:

    image_person.setImageBitmap(bitmap);
    

    要解决所有这些问题,请从 activity_main XML 布局中删除 dialog_imageview

    然后,通过在其上调用findViewById 在您的膨胀sample 布局而不是在活动中找到正确的ImageView

    final View alertDialog = factory.inflate(R.layout.sample, null);
    image_person = (ImageView) alertDialog.findViewById(R.id.dialog_imageview);
    image_person.setImageBitmap(bitmap);
    

    【讨论】:

    • 是的,我的 activity_main 中有一个 ImageView。我在activity_main中删除了这段代码。我确实得到了一个空指针异常。我该如何解决这个问题?
    • @Paul 我看到您自己找到了最终解决方案。我编辑了我的完整答案。
    【解决方案2】:

    在 Inflated 布局之前不能执行“findViewById()”... 我认为您的 MainActivity 布局中有一个“dialog_imageview”,然后图像在错误的 ImageView 中更新。

    您只需移动“image_person = (ImageView)alertDialog.findViewById(R.id.dialog_imageview);”在“factory.inflate(R.layout.sample, null);”之后行。

    【讨论】:

    • 还是不行。图片留在对话框后面。
    【解决方案3】:

    首先正如霍夫曼正确指出的那样,我确实在 activty_man.xml 中复制了 ImageView。

    但是,问题仍然存在。

    经过大量调查,我意识到我错过了一行代码,

    之前:

    image_person = (ImageView) findViewById(R.id.dialog_imageview);
    

    修改后:

    image_person = (ImageView) alertDialog.findViewById(R.id.dialog_imageview);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多