마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

LocalDate 사용하는데 에러가납니다

0 추천
package com.example.two;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import java.time.LocalDate;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.ArrayList;



public class MainActivity extends AppCompatActivity implements CalendarAdapter.OnItemListener
{
    private TextView monthYearText;
    private RecyclerView calendarRecyclerView;
    public LocalDate selectedDate;

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initWidgets();
        selectedDate=LocalDate.now();
        setMonthView();
    }

    private void initWidgets()
    {
        calendarRecyclerView=findViewById(R.id.calendarRecyclerView);
        monthYearText=findViewById(R.id.monthYearTV);
    }


    @RequiresApi(api = Build.VERSION_CODES.O)
    private void setMonthView()
    {
        monthYearText.setText(monthYearFromDate(selectedDate));
        ArrayList<String> daysInMonth = daysInMonthArray(selectedDate);

        CalendarAdapter calendarAdapter=new CalendarAdapter(daysInMonth,this);
        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(), 7);
        calendarRecyclerView.setLayoutManager(layoutManager);
        calendarRecyclerView.setAdapter(calendarAdapter);
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    private ArrayList<String> daysInMonthArray(LocalDate selectedDate)
    {
        ArrayList<String> daysInMonthArray =new ArrayList<>();
        TemporalAccessor date = null;
        YearMonth yearMonth= YearMonth.from(date);

        int daysInMonth=yearMonth.lengthOfMonth();

        LocalDate firstOfMonth = selectedDate.withDayOfMonth(1);
        int dayOfweek = firstOfMonth.getDayOfWeek().getValue();

        for(int i=1; i<=42; i++)
        {
            if(i<=dayOfweek || i>daysInMonth + dayOfweek)
            {
                daysInMonthArray.add("");
            }
            else
            {
                daysInMonthArray.add(String.valueOf(i-dayOfweek));
            }
        }
        return daysInMonthArray;
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    private String monthYearFromDate(LocalDate date)
    {
        DateTimeFormatter formatter=DateTimeFormatter.ofPattern("MMMM yyyy");
        return date.format(formatter);
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    public void previousMonthAction(View view)
    {
        selectedDate=selectedDate.minusMonths(1);
        setMonthView();
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    public void nextMonthAction(View view)
    {
        selectedDate = selectedDate.plusMonths(1);
        setMonthView();
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public void onItemClick(int position, String dayText)
    {
    if(dayText.equals(""))
    {
        String message="Selected Date"+ dayText+""+monthYearFromDate(selectedDate);
        Toast.makeText(this,message,Toast.LENGTH_LONG).show();
    }
    }
}

왜 LocalDate.now(); 가 안되는걸까요...?

 

2021-09-29 18:31:15.578 3777-3777/com.example.two E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.two, PID: 3777
    java.lang.NoClassDefFoundError: Failed resolution of: Ljava/time/LocalDate;
        at com.example.two.MainActivity.onCreate(MainActivity.java:35)

너야나 (120 포인트) 님이 2021년 9월 30일 질문

1개의 답변

0 추천
혹시 빌드파일에 자바8을 사용하는데 필요한 세팅은 추가하셨나요? 아래 링크에 빌드파일을 설정하는 내용이 있습니다. 확인해 보세요.

https://developer.android.com/studio/write/java8-support

참고로, 제가 알기로는 java8은 사용하려면 minSdk = 26로 설정해야 할 겁니다. 해당 문서를 잘 읽어보시기 바랍니다.
spark (230,170 포인트) 님이 2021년 9월 30일 답변
...