简体   繁体   中英

Cannot Resolve symbol 'binding' error

So I recently started using binding and I enabled it in my gradle but when I try and use it I get an error? I synced the gradle and the project and everything is fine but when I try use the binding it gives me the "Cannot resolve symbol 'binding'" error. Help please! This is my xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.mike.myapplication.adminScreen"
tools:showIn="@layout/activity_admin_screen">

<EditText
    android:id="@+id/nameEditText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="textPersonName"
    android:text="Name"
    tools:layout_constraintTop_creator="1"
    android:layout_marginStart="37dp"
    android:layout_marginTop="31dp"
    tools:layout_constraintLeft_creator="1"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<EditText
    android:id="@+id/timeToPrepEditText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="textPersonName"
    android:text="Time To Prepare"
    tools:layout_constraintTop_creator="1"
    tools:layout_constraintRight_creator="1"
    app:layout_constraintRight_toRightOf="@+id/nameEditText"
    android:layout_marginTop="20dp"
    app:layout_constraintTop_toBottomOf="@+id/nameEditText"
    tools:layout_constraintLeft_creator="1"
    app:layout_constraintLeft_toLeftOf="@+id/nameEditText" />

<EditText
    android:id="@+id/descEditText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="textPersonName"
    android:text="Description"
    tools:layout_constraintTop_creator="1"
    tools:layout_constraintRight_creator="1"
    app:layout_constraintRight_toRightOf="@+id/timeToPrepEditText"
    android:layout_marginTop="7dp"
    app:layout_constraintTop_toBottomOf="@+id/timeToPrepEditText"
    tools:layout_constraintLeft_creator="1"
    app:layout_constraintLeft_toLeftOf="@+id/timeToPrepEditText" />

<EditText
    android:id="@+id/priceEditText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="textPersonName"
    android:text="Price"
    tools:layout_constraintTop_creator="1"
    tools:layout_constraintRight_creator="1"
    app:layout_constraintRight_toRightOf="@+id/descEditText"
    android:layout_marginTop="10dp"
    app:layout_constraintTop_toBottomOf="@+id/descEditText"
    tools:layout_constraintLeft_creator="1"
    app:layout_constraintLeft_toLeftOf="@+id/descEditText" />

<Button
    android:id="@+id/addButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Add Item"
    android:onClick="addButtonClicked"
    tools:layout_constraintTop_creator="1"
    android:layout_marginTop="24dp"
    app:layout_constraintTop_toBottomOf="@+id/priceEditText"
    tools:layout_constraintLeft_creator="1"
    app:layout_constraintLeft_toLeftOf="@+id/priceEditText" />

<Button
    android:id="@+id/deleteButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Delete Item"
    android:onClick="deleteButtonClicked"
    tools:layout_constraintTop_creator="1"
    android:layout_marginTop="15dp"
    app:layout_constraintTop_toBottomOf="@+id/addButton"
    tools:layout_constraintLeft_creator="1"
    app:layout_constraintLeft_toLeftOf="@+id/addButton" />

<Button
    android:id="@+id/databaseButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Display Database"
    android:onClick="dispDatabaseButton"
    tools:layout_constraintTop_creator="1"
    android:layout_marginTop="-79dp"
    app:layout_constraintTop_toBottomOf="@+id/addButton"
    tools:layout_constraintLeft_creator="1"
    app:layout_constraintLeft_toLeftOf="@+id/addButton"
    android:layout_marginLeft="156dp" />

<TextView
    android:id="@+id/myText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="-41dp"
    android:layout_marginStart="15dp"
    android:layout_marginTop="54dp"
    android:onClick="dispDatabaseButton"
    android:text="Results"
    android:textSize="30sp"
    android:textStyle="bold"
    app:layout_constraintLeft_toRightOf="@+id/deleteButton"
    app:layout_constraintTop_toBottomOf="@+id/deleteButton"
    tools:layout_constraintLeft_creator="1"
    tools:layout_constraintTop_creator="1" />

How do I fix my error?

In addition here is the code for the activity, in my case it isn't the default main activity but instead AdminScreen.

public class adminScreen extends AppCompatActivity {



@Override
protected void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_admin_screen);




}

private void saveToDB() {
    SQLiteDatabase database = new DBSQLiteHelper(this).getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(Menu.MenuItems.COLUMN_NAME, binding.nameEditText.getText().toString());
    values.put(Menu.MenuItems.COLUMN_PREP, binding.timeToPrepEditText.getText().toString());
    values.put(Menu.MenuItems.COLUMN_DESCRIPTION, binding.descEditText.getText().toString());
    values.put(Menu.MenuItems.COLUMN_PRICE, binding.priceEditText.getText().toString());

    long newRowId = database.insert(Menu.MenuItems.TABLE_NAME, null, values);

    Toast.makeText(this, "The new Row Id is " + newRowId, Toast.LENGTH_LONG).show();
     }

}

If you mean DataBinding library you should build you layout this way

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
   <data>
       <variable name="user" type="com.example.User"/>
   </data>
   <LinearLayout
       android:orientation="vertical"
       android:layout_width="match_parent"
       android:layout_height="match_parent">
       <TextView android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="@{user.firstName}"/>
       <TextView android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="@{user.lastName}"/>
   </LinearLayout>
</layout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM