skip to Main Content

My DetailActivity.java file shows an error by not picking ActivityDetailBinding. Other Activity files are working fine and I put;

buildFeatures{
    viewBinding true
}

in build.gradle file before start working on xml files. Only DetailActivity.java file is showing this error.

Error displaying is;

import com.example.juizeesapp.databinding.ActivityDetailBinding; ^ symbol: class ActivityDetailBinding location: package com.example.juizeesapp.databinding

Please look into this help me through this. TIA.

Here is my code.

activity_detail.xml

<LinearLayout
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"
android:orientation="vertical"
tools:context=".DetailActivity"
>

 <androidx.cardview.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

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

<ImageView
        android:id="@+id/detailImage"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:scaleType="centerCrop"
        app:srcCompat="@drawable/apple" />

<TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="5dp"
        android:text="Juice Name"
        android:textColor="#000000"
        android:textSize="24sp"
        android:textStyle="bold" />

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="25dp"
        android:gravity="center"
        android:orientation="horizontal">

<ImageView
            android:id="@+id/subtract"
            android:layout_width="30dp"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/minus" />

<TextView
            android:id="@+id/quantity"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="2dp"
            android:layout_marginLeft="2dp"
            android:layout_marginEnd="2dp"
            android:layout_marginRight="2dp"
            android:text="1"
            android:textSize="18sp"
            android:textStyle="bold" />

<ImageView
            android:id="@+id/add"
            android:layout_width="30dp"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/plus" />

</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginStart="20dp"
    android:layout_marginLeft="20dp"
    android:layout_marginEnd="20dp"
    android:layout_marginRight="20dp"
    android:orientation="vertical">

<TextView
        android:id="@+id/detailDescription"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_marginBottom="10dp"
        android:gravity="center"
        android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam iaculis." />

<EditText
        android:id="@+id/nameBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:background="@drawable/edit_textbackground"
        android:ems="10"
        android:hint="Name"
        android:inputType="textPersonName"
        android:minHeight="48dp"
        android:padding="10dp" />

<EditText
        android:id="@+id/phoneBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:background="@drawable/edit_textbackground"
        android:ems="10"
        android:hint="Phone Number"
        android:inputType="phone"
        android:minHeight="48dp"
        android:padding="10dp" />

<TextView
        android:id="@+id/textView4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="6dp"
        android:gravity="center"
        android:text="Price"
        android:textColor="#000000" />

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal">

<TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Rs. "
            android:textColor="@color/red"
            android:textSize="18dp"
            android:textStyle="bold" />

<TextView
            android:id="@+id/priceLbl"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="45"
            android:textColor="@color/red"
            android:textSize="18dp"
            android:textStyle="bold" />

</LinearLayout>

<Button
        android:id="@+id/insertBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:background="@drawable/btn_background"
        android:text="Place Your Order" />

</LinearLayout>
</LinearLayout>

DetailActivity.java

package com.example.juizeesapp;

import android.os.Bundle;
import android.os.PersistableBundle;
import androidx.annotation.Nullable;

import androidx.appcompat.app.AppCompatActivity;
import com.example.juizeesapp.databinding.ActivityDetailBinding;

 public class DetailActivity extends AppCompatActivity {

ActivityDetailBinding binding;

@Override
      public void onCreate(@Nullable Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         binding = ActivityDetailBinding.inflate(getLayoutInflater());
         setContentView(binding.getRoot());

         int image = getIntent().getIntExtra("image" , 0);
         int price = Integer.parseInt(getIntent().getStringExtra("price"));
         String name = getIntent().getStringExtra("name");
         String description = getIntent().getStringExtra("desc");

         binding.detailImage.setImageResource(image);
         binding.priceLbl.setText(String.format("%d", price));
         binding.nameBox.setText(name);
         binding.detailDescription.setText(description);
    }
}

2

Answers


  1. Chosen as BEST ANSWER

    It was a typo error in the manifest file. I stated .DetailActivity at the manifest file is;

    <activity
            android:name=".DetailActivity"
            android:exported="false" />
    

    But when I changed it into;

    <activity android:name=".DetailActivity"></activity>
    

    Problem solved.


  2. you have not declared binding
    ActivityDetailBinding binding;

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search