Как установить источник данного файла PNG для ImageButton? - PullRequest
0 голосов
/ 26 мая 2019

Я пытаюсь установить локальный сохраненный файл png в качестве источника для моей кнопки png, но изображение не установлено на кнопке, и я получаю это предупреждение: W / ImageView: resolUri не удалось на плохом растровом изображении uri: android.widget.ImageButton {391ee46 VFED..C .. ...... I.0,0-0,0 # 7f070059 приложение: id / messages_remove_button}.Хотя я не понимаю, поскольку мой файл сохраняется локально, что он имеет отношение к URI.

Первоначально я пытался установить атрибут src в моем файле макета xml, но он тот же.

private void makeListAdapter(List<String> notifications){
        ViewGroup container = findViewById(R.id.notification_item);
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.alarm_item_layout, container, false);
        ImageButton imageButton = view.findViewById(R.id.notification_remove_button);
        Drawable trashPng = getResources().getDrawable(R.drawable.trash, null);
        imageButton.setImageDrawable(trashPng);
        itemList = new ArrayList<>();
        for (String notification : notifications){
            Map<String, Object> notificationItem = new HashMap<>();
            notificationItem.put("alarm_time", notification);
            notificationItem.put("remove_button", imageButton);
            itemList.add(notificationItem);
        }
        String[] from = {"alarm_time", "remove_button"};
        int[] to = {R.id.notification_time, R.id.notification_remove_button};

        notificationListView = findViewById(R.id.notificationList);
        ListAdapter notificationsListAdapter = new SimpleAdapter(this, itemList, R.layout.alarm_item_layout, from, to);
        notificationListView.setAdapter(notificationsListAdapter);
    }

Вот мой item_layout xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/notification_item"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:padding="16dp"
              android:orientation="horizontal">

    <TextView
            android:id="@+id/notification_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="28sp"
            android:gravity="start"/>

    <ImageButton
            android:id="@+id/notification_remove_button"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_gravity="end"
            android:contentDescription="@string/removeButtonDescription"/>

</LinearLayout>

А вот макет активности xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_margin="10dp"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/notificationList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/temperatureLabel"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/temperatureLabelText"/>

    <Button
        android:id="@+id/timeButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/notificationButtonText"/>

</LinearLayout>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...