我的应用程序在 Play 商店中使用了 2 年,并且运行良好。但突然在过去两周内,用户报告应用程序在使用时崩溃。
所有崩溃都是针对 Android 10 用户的。
结构日志显示错误为,
java.lang.IllegalStateException: Only owner is able to interact with pending media content://media/external/images/media/259525
at android.os.Parcel.createException(Parcel.java:2079)
at android.os.Parcel.readException(Parcel.java:2039)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:188)
at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:151)
at android.content.ContentProviderProxy.openTypedAssetFile(ContentProviderNative.java:705)
at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1687)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1503)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1420)
at android.graphics.ImageDecoder$ContentResolverSource.createImageDecoder(ImageDecoder.java:277)
at android.graphics.ImageDecoder.decodeDrawableImpl(ImageDecoder.java:1743)
at android.graphics.ImageDecoder.decodeDrawable(ImageDecoder.java:1736)
at android.widget.ImageView.getDrawableFromUri(ImageView.java:1022)
at android.widget.ImageView.resolveUri(ImageView.java:991)
at android.widget.ImageView.setImageURI(ImageView.java:568)
at androidx.appcompat.widget.AppCompatImageView.setImageURI(AppCompatImageView.java:116)
at androidx.databinding.adapters.ImageViewBindingAdapter.setImageUri(ImageViewBindingAdapter.java:46)
Run Code Online (Sandbox Code Playgroud)
这个屏幕上发生的事情是,用户应该捕获屏幕截图。我保存此屏幕截图并稍后在ImageView. 我通过它来检索屏幕截图图像URI。
当我使用它的URI.
我今天搜索了很多,只发现了一些关于这个问题的不清楚的答案。
我在一个表单中有多个 TextView 和 EditText,所有这些都是由我的主题自定义的,我想每次重用相同的包含布局,但有参数,例如 TextView 内的文本或 EditText 内的提示。
我现在的form.xml:
<LinearLayout android: orientation="horizontal">
<TextView android:text="Username"/>
<EditText android:id="edittext_username"
android:hint="Enter username..."
android:inputType="text"/>
</LinearLayout>
<LinearLayout android: orientation="horizontal">
<TextView android:text="Password"/>
<EditText android:id="edittext_password"
android:hint="Enter password..."
android:inputType="password"/>
</LinearLayout>
... other fields ...
Run Code Online (Sandbox Code Playgroud)
我在主 form.xml 中想要什么:
<LinearLayout android:orientation="vertical" ...>
<include layout="form_edittext"
app:textview_text="@{`Username`}"
app:edittext_hint="@{`Enter username...`}"
... other parameters ...
/>
<include layout="form_edittext"
app:textview_text="@{`Password`}"
app:edittext_hint="@{`Enter password...`}"
... other parameters ...
/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
还有 form_edittext.xml:
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="textview_text" type="java.lang.String"/>
<variable name="edittext_id" type="java.lang.String"/>
<variable name="edittext_inputType" type="java.lang.String"/>
<variable name="edittext_hint" type="java.lang.String"/> …Run Code Online (Sandbox Code Playgroud) 比方说,我有一个简单的课程User。现在我想创建一个inner data class内部UserProperty类User,但是当我向类添加inner修饰符时,IDE 就会抱怨Modifier inner is incomplete with data。我缺少什么?dataUserProperty
现在有了视图模型,我们可以处理配置更改并管理 UI 数据,所以我觉得 onSaveInstance() 现在对我来说没有用处。
就像之前我们在 SaveInstanceState() 上存储较小的数据并在配置期间恢复它一样,现在使用视图模型我们可以轻松获取更新的数据。那么,如果我们使用 ViewModel,您能告诉我onSaveInstanceState()和onRestoreInstancestate()现在的实际用法是什么吗?
您能否告诉我它在当前 ViewModel 案例中的用法
我的代码
fun weatherByCity(cityName: String): LiveData<Resource<Response>> = liveData(Dispatchers.IO) {
val response = appRepository.weatherByCity(cityName)
emit(response)
}
fun weatherByZipCode(zipCode: String): LiveData<Resource<Response>> = liveData(Dispatchers.IO) {
val response = appRepository.weatherByZipCode(zipCode)
emit(response)
}
fun weatherByLocation(latLong: String): LiveData<Resource<Response>> = liveData(Dispatchers.IO) {
val response = appRepository.weatherByLocation(lat?:"", long?:"")
emit(response)
}
Run Code Online (Sandbox Code Playgroud)
在这里,我使用了三个 liveData{} 块,它们从存储库中获取数据并进行改造。但它们具有相同的返回类型LiveData<Resource<Response>>。我可以对所有三个都使用单个 liveData{},如果是,那么该怎么做?还有一个问题,使用 liveData(Dispatchers.IO) 还是使用 liveData{}(不使用 Dispatchers.IO)会有什么不同?
android kotlin android-livedata mutablelivedata kotlin-coroutines
如何在 MainActivity 中为 ListView 全局设置我的 ArrayAdapter ?
我的代码是:
class MainActivity : AppCompatActivity() {
var listItems = ArrayList<String>()
private var listAdapter: ArrayAdapter<String>? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
listAdapter = ArrayAdapter(
this,
android.R.layout.simple_list_item_1,
listItems
)
listfield.adapter = listAdapter
listAdapter.add("Hello World!")
listAdapter.add("RFID app")
Run Code Online (Sandbox Code Playgroud)
所以问题是这会在编译时出现错误: 智能转换为 ArrayAdapter 是不可能的,因为 'listAdapter' 是一个可变属性,此时可能已更改。 这是因为无法知道在调用 .Add 时另一个线程是否已将 listAdapter 设置回 null。
我需要 listAdapter 是全局的,因为我需要从回调函数中调用 .notifyDataSetChanged()(当条形码阅读器读取条形码时),如果我在 onCreate() 中声明 listAdapter,它不可用回调函数。所以它必须是一个属性。Kotlin 抱怨属性必须被初始化或声明为“抽象”。显然“抽象”在这里不起作用,但我不能将它初始化为“null”以外的其他形式,因为初始化需要上下文,并且上下文在 onCreate() 之前不可用。在 Kotlin 中如何处理这个问题?