monstertjie_za
monstertjie_za

Reputation: 7803

Error: Package does not exist Android Studio

I am a beginner Android developer.

I am busy walking through a code sample on DataBiding. I have the following Activity:

package com.example.andre.vehicleasseessing;
import android.databinding.DataBindingUtil;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.example.andre.vehicleasseessing.databinding.LoginActivityBinding;

import Common.Login;

public class LoginActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LoginActivityBinding binding = DataBindingUtil.setContentView(this, R.layout.login_activity);

        Login loginObject = new Login();
        loginObject.setEmail("[email protected]");
        loginObject.setPassword("123456");

        binding.setLogin(loginObject);
    }
}

Whenever I try to use the LoginActivityBinding object, it imports the following package:

import com.example.andre.vehicleasseessing.databinding.LoginActivityBinding;

After this, I get the following error:

Error: package 'com.example.andre.vehicleasseessing.databinding' does not exist.

I agree that this package does not exist, but why does it try to import this package if it does not exist?

Upvotes: 0

Views: 5975

Answers (3)

monstertjie_za
monstertjie_za

Reputation: 7803

I solved the problem myself. I forgot that I renamed my data variable used for binding. My code was as follow:

<data>
    <variable
        name="login"
        type="Common.Login"/>
</data>

And then I would try and bind as follow:

android:text="@{oldDataSourceName.password}"/>

Changing the above to point to my data variable name, fixed my problem, as follow:

android:text="@{login.password}"/>

Upvotes: 0

George Mount
George Mount

Reputation: 20926

I'm guessing that you have a layout named login_activity.xml. Android Data Binding automatically generates a Binding class based on the name of your layout XML file in the databinding package under your application's package. Since your application's package is com.example.andre.vehicleasseessing, and your layout is login_activity.xml, the generated class will be com.example.andre.vehicleasseessing.databinding.LoginActivity.

I'm not sure why Android Studio is having difficulty. It could be that there is a cache problem or your removed the <layout> tags from the layout XML file. In any case, before you worry too much, try cleaning, compiling, and running it. Android Studio may just be confused and it may work anyway.

FYI, you can change the class name and/or package of the generated binding class. In the <data> tag, include the class name:

<layout xmlns:android="...">
    <data class="com.example.andre.vehicleasseessing.LoginActivity">
        <variable .../>
    </data>
    <FrameLayout ...>
    </FrameLayout>
</layout>

Upvotes: 1

Onur A.
Onur A.

Reputation: 3017

Here how it basically works, everything you type as an import statement, it tries to find that package/class to import it into your code so you can refer to it and its publicly available members and methods. So when you type import com.example.andre.vehicleasseessing.databinding of course it tries to import it into your code so you can start using it.

Upvotes: 0

Related Questions