小编aro*_*nyi的帖子

Android 12 BroadcastReceiver onReceive Intent.getExtras 始终为 null

我正在尝试构建一个 stackview 小部件,它在滑动时应该显示不同的数据。到目前为止,一切正常,但是当我想接收小部件中单击的卡的索引时,我总是得到一个额外的或“null”的操作。我已经尝试了几种解决方法,但没有一个在我的应用程序中起作用。我还设置了一个全新的应用程序,只是为了使这项工作完全隔离。但在这里我得到了完全相同的行为。

我尝试传递索引的几种解决方法:

我观察到,在生成的 exampleData 列表中的正确位置调用 getViewAt 函数之前,它会被多次调用,位置(p0)为“0”...但我不知道这是否会导致空值在 onReceive 函数中,当单击卡片时。

这是我的编码:

清单.xml

<service android:name=".ExampleWidgetService"
        android:permission="android.permission.BIND_REMOTEVIEWS">
</service>
Run Code Online (Sandbox Code Playgroud)

组件布局

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <StackView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/stackview">
    </StackView>

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/empty_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textColor="#ffffff"
        android:textStyle="bold"
        android:text="empty"
        android:textSize="20sp" />
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

小部件项条目

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:text="Icon"
        android:id="@+id/newwidgetentry">

    </TextView> …
Run Code Online (Sandbox Code Playgroud)

android broadcastreceiver android-widget stackview

5
推荐指数
1
解决办法
2203
查看次数