【问题标题】:error: attribute 'android:context' not found错误:找不到属性“android:context”
【发布时间】:2019-05-04 19:23:09
【问题描述】:

错误:未找到属性“android:context”。 消息{种类=错误,文本=错误:找不到属性 'android:context'。,来源=[C:\Users\eMotion4\AndroidStudioProjects\BSMAS\app\src\main\res\layout\activity_splashactivity.xml:2] , original message=, tool name=Optional.of(AAPT)}

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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"
    android:background="#FF9B1F1C"
    android:context=".splashactivity">

<ImageView
        android:layout_width="120dp"
        android:src="@drawable/bharatsathi"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/bharatsathi"

    />
</RelativeLayout>

【问题讨论】:

    标签: android android-manifest


    【解决方案1】:

    这仅仅是因为您为context 使用了不正确的属性。您需要使用tools:context 而不是android:context。因此,更新您的 xml 以像这样使用它:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        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"
        android:background="#FF9B1F1C"
        xmlns:tools="http://schemas.android.com/tools"
        tools:context=".splashactivity">
    
    <ImageView
            android:layout_width="120dp"
            android:src="@drawable/bharatsathi"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            android:id="@+id/bharatsathi"
    
        />
    </RelativeLayout>
    

    【讨论】:

    • 这对我有用。我正在使用 app:context=".MainActivity" 而不是 tools:context=".MainActivity" 并改变它就可以了。我也是使用 kotlin 进行 android 编程的新手,我正在关注官方文档。很有趣
    【解决方案2】:

    类区分大小写。

    android:context=".splashactivity"
    

    意味着在您的源代码根文件夹中应该有一个名为splashactivity 的Activity 子类。确保它存在。这将是 AppCompatActivity 的子类,它从 Activity 扩展而来。

    您的班级将通过调用setContentView(R.layout.activity_splashactivity); 来使用发布代码中提到的布局。


    注意:在 Java/Kotlin 中,根据命名约定,首选名称将是 SplashActivity。

    【讨论】:

      猜你喜欢
      • 2019-02-06
      • 2019-03-08
      • 2014-06-24
      • 2020-08-25
      • 2013-02-07
      • 2018-06-13
      • 2021-04-09
      • 2021-12-11
      • 2020-11-22
      相关资源
      最近更新 更多