Home > Software design >  The activity and XML won't sync in android studio || error : unresolved reference: android,tool
The activity and XML won't sync in android studio || error : unresolved reference: android,tool

Time:01-20

my activity does not sync with its layout xml. I really dont know why the coding seems okay. Im so sorry. Im a beginner dev, i really need to finish my project asap.

This is my activity using Kotlin in android studio

package com.pkndegwa.mycarmaintenance

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.work.Data
import androidx.work.ExistingWorkPolicy.REPLACE
import androidx.work.OneTimeWorkRequest
import androidx.work.WorkManager
import kotlinx.android.synthetic.service_list_item.*
import com.google.android.material.snackbar.Snackbar.LENGTH_LONG
import com.google.android.material.snackbar.Snackbar.make
import com.pkndegwa.mycarmaintenance.NotifyWork.Companion.NOTIFICATION_ID
import com.pkndegwa.mycarmaintenance.NotifyWork.Companion.NOTIFICATION_WORK
import java.lang.System.currentTimeMillis
import java.text.SimpleDateFormat
import java.util.*
import java.util.Locale.getDefault
import java.util.concurrent.TimeUnit.MILLISECONDS

class AlarmActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.service_list_item)

        userInterface()
    }

    private fun userInterface() {
        setSupportActionBar(toolbar)

        val titleNotification = getString(R.string.notification_title)

        done_fab.setOnClickListener {
            val customCalendar = Calendar.getInstance()
            customCalendar.set(
                date_p.year, date_p.month, date_p.dayOfMonth, time_p.hour, time_p.minute, 0
            )
            val customTime = customCalendar.timeInMillis
            val currentTime = currentTimeMillis()
            if (customTime > currentTime) {
                val data = Data.Builder().putInt(NOTIFICATION_ID, 0).build()
                val delay = customTime - currentTime
                scheduleNotification(delay, data)

                val titleNotificationSchedule = getString(R.string.notification_schedule_title)
                val patternNotificationSchedule = getString(R.string.notification_schedule_pattern)
                make(
                    service_card,
                    titleNotificationSchedule   SimpleDateFormat(
                        patternNotificationSchedule, getDefault()
                    ).format(customCalendar.time).toString(),
                    LENGTH_LONG
                ).show()
            } else {
                val errorNotificationSchedule = getString(R.string.notification_schedule_error)
                make(service_card, errorNotificationSchedule, LENGTH_LONG).show()
            }
        }
    }

    private fun scheduleNotification(delay: Long, data: Data) {
        val notificationWork = OneTimeWorkRequest.Builder(NotifyWork::class.java)
            .setInitialDelay(delay, MILLISECONDS).setInputData(data).build()

        val instanceWorkManager = WorkManager.getInstance(this)
        instanceWorkManager.beginUniqueWork(NOTIFICATION_WORK, REPLACE, notificationWork).enqueue()
    }
}

This is my layout.xml. This layout been use by two activity which is "ServiceListAdapter" and "AlarmActivity"

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView 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:id="@ id/service_card"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginHorizontal="8dp"
    android:layout_marginVertical="4dp"
    android:backgroundTint="#E9E9ED"
    android:clickable="true"
    android:focusable="true"
    android:minHeight="@dimen/button_min_height"
    app:cardElevation="1dp">

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

        <LinearLayout
            android:id="@ id/services_heading_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:orientation="horizontal">

            <ImageView
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_margin="12dp"
                android:layout_weight="0"
                android:importantForAccessibility="no"
                android:src="@drawable/ic_baseline_build_circle_24" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:orientation="vertical">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/services"
                    android:textAllCaps="false"
                    android:textAppearance="?attr/textAppearanceBody2"
                    android:textColor="@color/black"
                    android:textSize="16sp" />

                <TextView
                    android:id="@ id/services_done_list"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAllCaps="false"
                    android:textSize="14sp"
                    tools:text="Oil Filer, Spark Plugs, Battery" />

            </LinearLayout>

            <ImageView
                android:id="@ id/services_expand_view"
                android:layout_width="32dp"
                android:layout_height="32dp"
                android:layout_margin="12dp"
                android:layout_weight="0"
                android:contentDescription="@string/expand_more_button"
                android:src="@drawable/ic_baseline_expand_more_24" />
        </LinearLayout>

        <com.google.android.material.divider.MaterialDivider
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@ id/services_details_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingHorizontal="6dp"
            android:paddingTop="0dp"
            android:paddingBottom="8dp"
            android:visibility="gone"
            app:layout_constraintEnd_toStartOf="@ id/services_left_layout"
            app:layout_constraintStart_toStartOf="@ id/services_left_layout">

            <LinearLayout
                android:id="@ id/services_left_layout"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="8dp"
                android:orientation="vertical"
                app:layout_constraintEnd_toStartOf="@id/services_right_layout"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/service_date"
                    android:textAppearance="?attr/textAppearanceBody2"
                    android:textColor="@color/black"
                    android:textSize="16sp" />

                <TextView
                    android:id="@ id/service_date_text_view"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAllCaps="false"
                    android:textSize="14sp"
                    tools:text="01 Nov 2022" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="24dp"
                    android:text="@string/current_mileage"
                    android:textAppearance="?attr/textAppearanceBody2"
                    android:textColor="@color/black"
                    android:textSize="16sp" />

                <TextView
                    android:id="@ id/current_mileage_text_view"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAllCaps="false"
                    android:textSize="14sp"
                    tools:text="2,000 km" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="24dp"
                    android:text="@string/next_service_mileage"
                    android:textAppearance="?attr/textAppearanceBody2"
                    android:textColor="@color/black"
                    android:textSize="16sp" />

                <TextView
                    android:id="@ id/next_service_mileage_text_view"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAllCaps="false"
                    android:textSize="14sp"
                    tools:text="4,000 km" />

            </LinearLayout>

            <LinearLayout
                android:id="@ id/services_right_layout"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="8dp"
                android:orientation="vertical"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toEndOf="@id/services_left_layout"
                app:layout_constraintTop_toTopOf="parent">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/next_service_date"
                    android:textAppearance="?attr/textAppearanceBody2"
                    android:textColor="@color/black"
                    android:textSize="16sp" />

                <TextView
                    android:id="@ id/next_service_date_text_view"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAllCaps="false"
                    android:textSize="14sp"
                    tools:text="01 Mar 2023" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="24dp"
                    android:text="@string/total_cost"
                    android:textAppearance="?attr/textAppearanceBody2"
                    android:textColor="@color/black"
                    android:textSize="16sp" />

                <TextView
                    android:id="@ id/total_cost_text_view"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAllCaps="false"
                    android:textSize="14sp"
                    tools:text="1,500 KSH" />

            </LinearLayout>

            <TextView
                android:id="@ id/service_notes_title_text_view"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginTop="24dp"
                android:text="@string/notes"
                android:textAppearance="?attr/textAppearanceBody2"
                android:textColor="@color/black"
                android:textSize="16sp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/services_left_layout" />

            <TextView
                android:id="@ id/service_notes_text_view"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:textAllCaps="false"
                android:textSize="14sp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/service_notes_title_text_view"
                tools:text="Testing out the notes appearance" />

            <!--            This section is to create our alarm from different page since Examiner-->
            <!--            want the user to separate the alarm from the add service -->

            <EditText
                android:id="@ id/titleET"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/service_notes_text_view"
                android:hint="Title for your notification"
                tools:context=".ui.AlarmActivity"
                android:autofillHints="Which Car" />


            <EditText
                android:id="@ id/messageET"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="You can set your own notification here"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/titleET"
                tools:context=".ui.AlarmActivity"
                android:autofillHints="what service" />

            <DatePicker
                android:id="@ id/date_p"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginLeft="-30dp"
                android:layout_marginRight="-30dp"
                android:layout_weight="1"
                android:calendarViewShown="false"
                android:datePickerMode="spinner"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@id/messageET"
                tools:context=".ui.AlarmActivity"/>

            <TimePicker
                android:id="@ id/time_p"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginLeft="-30dp"
                android:layout_marginRight="-30dp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                android:timePickerMode="spinner"
                app:layout_constraintTop_toBottomOf="@id/date_p"
                tools:context=".ui.AlarmActivity"/>

            <Button
                android:id="@ id/done_fab"
                android:layout_marginTop="20dp"
                android:layout_gravity="center"
                android:text="schedule_notification"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@id/time_p"
                tools:context=".ui.AlarmActivity"/>


            <LinearLayout
                android:id="@ id/services_actions_container"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="end|center_vertical"
                android:orientation="horizontal"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/done_fab">


                <Button
                    android:id="@ id/edit_service_button"
                    style="@style/Widget.MaterialComponents.Button.TextButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginHorizontal="4dp"
                    android:layout_marginTop="8dp"
                    android:layout_marginBottom="4dp"
                    android:padding="2dp"
                    android:text="@string/edit"
                    android:textAppearance="?attr/textAppearanceBody2"
                    android:textColor="@color/black"
                    app:drawableTopCompat="@drawable/ic_baseline_edit_24" />

                <Button
                    android:id="@ id/delete_service_button"
                    style="@style/Widget.MaterialComponents.Button.TextButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginHorizontal="2dp"
                    android:layout_marginTop="8dp"
                    android:layout_marginBottom="4dp"
                    android:padding="2dp"
                    android:text="@string/delete"
                    android:textAppearance="?attr/textAppearanceBody2"
                    android:textColor="@color/black"
                    app:drawableTopCompat="@drawable/ic_baseline_delete_24" />

            </LinearLayout>

        </androidx.constraintlayout.widget.ConstraintLayout>

    </LinearLayout>

</com.google.android.material.card.MaterialCardView>

CodePudding user response:

Before onCreate you need to initialize all widget like

Button done_fab;//in JAVA

private lateinit var done_fab: Button //in Kotlin

//Add other here

and after setContentView you need to initialize the same widget with name like

done_fab=findViewById(R.id.done_fab);//in java

done_fab= findViewById(R.id.done_fab)//in Kotlin

//Add other here

it will be ready to use in overall activity class.

  • Related