0

我正在为我当前的应用程序使用 ViewBinding,但是遇到了问题。

在 MainActivity 中,我调用了 xml 的 inflate,但它显示以下错误

Error

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.foxcode.androidscreenmirroring/com.foxcode.androidscreenmirroring.view.MainActivity}: android.view.InflateException: Binary XML file line #11: Binary XML file line #21: Error inflating class ImageView
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2613)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
    at android.app.ActivityThread.-wrap11(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1517)
    at android.os.Handler.dispatchMessage(Handler.java:111)
    at android.os.Looper.loop(Looper.java:227)
    at android.app.ActivityThread.main(ActivityThread.java:6102)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:961)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:822)
 Caused by: android.view.InflateException: Binary XML file line #11: Binary XML file line #21: Error inflating class ImageView
    at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
    at com.foxcode.androidscreenmirroring.databinding.ActivityMainBinding.inflate(ActivityMainBinding.java:52)
    at com.foxcode.androidscreenmirroring.databinding.ActivityMainBinding.inflate(ActivityMainBinding.java:46)
    at com.foxcode.androidscreenmirroring.view.MainActivity.onCreate(MainActivity.java:45)
    at android.app.Activity.performCreate(Activity.java:6280)
...

这是xml代码和我调用的主要代码

activity_main.xml

<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawerLayout"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".view.MainActivity">

    <include
        android:id="@+id/data"
        layout="@layout/layout_data" />

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        ... />

</androidx.drawerlayout.widget.DrawerLayout>

布局数据.xml

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="@drawable/cst_background"
    >

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guide"
        ... />

    // error in xml
    <ImageView
        android:id="@+id/btn_connect"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/cst_background_logo"
        android:onClick="connect"
        android:padding="20dp"
        android:src="@drawable/ic_connect_wifi"
        android:tint="#EEFBFBFB"
        app:layout_constraintBottom_toTopOf="@id/guide"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
// error in code
    binding = ActivityMainBinding.inflate(getLayoutInflater());
    setContentView(binding.getRoot());
}

我已经找到了很多方法,但仍然找不到解决方案,希望你能帮助。

谢谢大家

4

1 回答 1

0

从和中删除android:background="@drawable/cst_background_logo"此行ImageView

也改变ImageViewdrawableandroid:src="@drawable/ic_connect_wifi"

于 2020-05-04T09:52:48.487 回答