应用程序可以在api 25及更高版本上运行,但是当我在api级别23中安装时,不幸的是,在第二次打开活动时应用程序停止了。
进程:com.watools.statusforwhatsapp,PID:28976 android.view.InflateException:二进制XML文件第7行:二进制XML文件第7行:膨胀类错误 在android.view.LayoutInflater.inflate(LayoutInflater.java:539) 在android.view.LayoutInflater.inflate(LayoutInflater.java:423) 在com.watools.statusforwhatsapp.adapter.CaptionViewRvAdapter.onCreateViewHolder(CaptionViewRvAdapter.kt:28) 在com.watools.statusforwhatsapp.adapter.CaptionViewRvAdapter.onCreateViewHolder(CaptionViewRvAdapter.kt:18)
此xml文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/coordinator"
tools:context=".activity.HomePageActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/cardview_shadow_end_color">
<ImageView
android:id="@+id/design"
android:layout_width="match_parent"
android:layout_height="150dp"
android:src="@drawable/rect"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textSize="50dp"
android:layout_marginTop="30dp"
android:textColor="@color/cardview_light_background"
android:text="CAPTIONS"/>
<include
android:id="@+id/toolbar"
layout="@layout/toolbar"
android:visibility="invisible"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/captionTitleRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
android:layout_below="@+id/design"
android:layout_marginTop="-20dp"
android:layout_above="@+id/adView"
tools:layout_editor_absoluteY="-2dp" />
<com.airbnb.lottie.LottieAnimationView
android:id="@+id/animation_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:scaleType="centerCrop"
app:lottie_autoPlay="true"
app:lottie_fileName="loading.json"
app:lottie_loop="true" />
<!-- <Button
android:id="@+id/internetGone"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_centerInParent="true"
android:gravity="center"
android:text="Internet Connection Gone"
android:textSize="20dp" />-->
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="@string/adMob_banner_liveId"/>
</RelativeLayout>
适配器类
class CaptionViewRvAdapter(
private val context: Context,
private val dataList: List<Data>,
private val mInterstitialAd: InterstitialAd
) :
RecyclerView.Adapter<CaptionViewRvAdapter.MyViewHolder>() {
var msg = arrayOfNulls<String?>(dataList.size)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val view = LayoutInflater.from(context)
.inflate(R.layout.caption_view, parent, false)
return MyViewHolder(view)
}
override fun getItemCount(): Int {
return dataList.size
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val data = dataList[position]
holder.setData(data, position)
holder.itemView.copy.setOnClickListener {
copyText(position)
}
holder.itemView.wa.setOnClickListener {
shareWhatsapp(position)
}
holder.itemView.share_text.setOnClickListener {
shareText(position)
}
}
private fun shareWhatsapp(position: Int) {
try {
val sendIntent = Intent("android.intent.action.MAIN")
sendIntent.action = Intent.ACTION_SEND
sendIntent.type = "text/plain"
sendIntent.putExtra(Intent.EXTRA_TEXT, msg[position])
sendIntent.putExtra("jid", "$msg[]@s.whatsapp.net")
sendIntent.setPackage("com.whatsapp")
context.startActivity(sendIntent)
} catch (e: Exception) {
Toast.makeText(context, "Error/n$e", Toast.LENGTH_SHORT).show()
}
}
private fun copyText(position: Int) {
showInterstitial()
if (msg[position] == "") {
Toast.makeText(context, "Text is Empty", Toast.LENGTH_SHORT).show()
} else {
val clipboard =
context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val clip: ClipData =
ClipData.newPlainText("your_text_to_be_copied", msg[position])
clipboard.setPrimaryClip(clip)
Toast.makeText(context, "Text Copied", Toast.LENGTH_SHORT).show()
}
}
private fun shareText(position: Int) {
val sharingIntent =
Intent(Intent.ACTION_SEND)
sharingIntent.type = "text/plain"
val shareBody = msg[position].toString()
sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject Here")
sharingIntent.putExtra(Intent.EXTRA_TEXT, shareBody)
context.startActivity(Intent.createChooser(sharingIntent, "Share via"))
}
private fun showInterstitial() {
if (mInterstitialAd.isLoaded) {
mInterstitialAd.show()
}
}
inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun setData(data: Data?, pos: Int) {
data?.let {
itemView.tv_caption.text = dataList[pos].caption
msg[pos] = dataList[pos].caption
}
}
}