outis
outis

Reputation: 77450

How to address "Cannot access class" error when binding to a class in a subpackage?

Starting with Android 3.2.1, binding to classes defined in sub-packages (e.g. Sub.Thing in com.example) results in an error:

Cannot access class 'Sub.Thing'. Check your module classpath for missing or conflicting dependencies

What's the cause of this errror? How can it be fixed?

Sample Code:

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<layout
        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"
        tools:context=".MainActivity"
    >

    <data>
        <import type="com.example.Sub.Thing"/>
        <variable name="data" type="Thing"/>
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
                android:id="@+id/name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/activity_vertical_margin"
                android:layout_marginStart="@dimen/activity_horizontal_margin"
                android:layout_marginEnd="@dimen/activity_horizontal_margin"
                android:text="@{data.name}"/>

        <TextView
                android:id="@+id/value"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/activity_vertical_margin"
                android:layout_marginStart="@dimen/activity_horizontal_margin"
                android:layout_marginEnd="@dimen/activity_horizontal_margin"
                android:text="@{data.value}"/>

    </LinearLayout>
</layout>

Thing.kt:

package com.example.Sub

data class Thing(
    var name:String,
    var value:String
)

MainActivity.kt:

package com.example

import android.databinding.DataBindingUtil
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import com.example.Sub.Thing
import com.example.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //setContentView(R.layout.activity_main)

        val activityMain:ActivityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main)
        activityMain.data = Thing("bam", "bug-AWWK!")
    }
}

Upvotes: 0

Views: 1337

Answers (1)

outis
outis

Reputation: 77450

By convention (and for good reason), only class names are supposed to start uppercase. It appears newer versions of the compiler assume (enforce?) this convention (uppercase letters later in the name are accepted, though this may cause other issues).

The solution is simple: rename the subpackage to use lowercase, which can be done by right-clicking it in the Android project view and clicking Refactor → Rename, or by clicking it and opening the Refactor → Rename menu item.

Upvotes: 4

Related Questions