admin管理员组

文章数量:1350072

I included a the Kotlin library () into my Java project. Using the recommendation from other posts i can get the project to compile and it half works. Using the information on the Readme file on github i converted it to Java and created the following classes:

MainActivity

public class AgendaActivity2 extends AppCompatActivity implements CalendarController {

private AgendaCalendarView agendaCalendarView;
private CalendarContentManager contentManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_agenda2);

    agendaCalendarView = findViewById(R.id.agenda_calendar_view_2);

    agendaCalendarView.getAgendaView().getAgendaListView().setSaveEnabled(false);

    // Setup date range
    Calendar minDate = Calendar.getInstance();
    Calendar maxDate = Calendar.getInstance();
    minDate.add(Calendar.MONTH, -10);
    minDate.add(Calendar.YEAR, -1);
    minDate.set(Calendar.DAY_OF_MONTH, 1);
    maxDate.add(Calendar.YEAR, 1);

    // Create your event adapter (implemented below)
    EventAgendaAdapter adapter = new EventAgendaAdapter(this);

    // Initialize the calendar content manager
    contentManager = new CalendarContentManager(
            this,
            agendaCalendarView,
            adapter
    );
    contentManager.setLocale(Locale.getDefault());
    contentManager.setDateRange(minDate, maxDate);



    List<CalendarEvent> events = new ArrayList<>();
    Calendar eventStart = Calendar.getInstance();
    Calendar eventEnd = Calendar.getInstance();
    eventEnd.add(Calendar.HOUR, 2);

    events.add(new MyCalendarEvent(
            eventStart,
            eventEnd,
            new DayItem(eventStart.getTime(), eventStart.getFirstDayOfWeek(),false,"March"),
            new Event("Meeting", "Project discussion")
    ));

    eventStart.add(Calendar.DAY_OF_MONTH,2);
    eventEnd.add(Calendar.DAY_OF_MONTH,2);

    events.add(new MyCalendarEvent(
            eventStart,
            eventEnd,
            new DayItem(eventStart.getTime(), eventStart.getFirstDayOfWeek(),false,"March"),
            new Event("Meeting", "Project discussion")
    ));

    contentManager.loadItemsFromStart(events);
}

// CalendarController interface methods:

@Override
public int getEmptyEventLayout() {
    // Return your layout resource for an "empty event"
    return R.layout.empty_event_item;
}

@Override
public int getEventLayout() {
    // Return your layout resource for an event item
    return R.layout.event_item;
}

@Override
public void onDaySelected(IDayItem dayItem) {
    // Handle a day being selected
    Toast.makeText(this, "Day selected: " + dayItem.getDate(), Toast.LENGTH_SHORT).show();
}

@Override
public void onScrollToDate(Calendar calendar) {
    // Optionally, handle scroll events (e.g., update toolbar title)
}
}

EventAgendaAdapter

public class EventAgendaAdapter extends DefaultEventAdapter {
private final SimpleDateFormat headerFormat;

public EventAgendaAdapter(Context context) {
    headerFormat = new SimpleDateFormat(
            "ddd MM yyyy",
            Locale.ENGLISH
    );
}

@Override
public int getEventLayout(boolean hasEvent) {
    return hasEvent ? R.layout.event_item : R.layout.empty_event_item;
}

@Override
public int getHeaderLayout() {
    return R.layout.view_agenda_header;
}

@Override
public void getEventItemView(@NonNull View view,
                             @NonNull CalendarEvent event,
                             int position) {
    TextView title = view.findViewById(R.id.event_title);
    TextView desc = view.findViewById(R.id.event_time);

    if (event.getEvent() instanceof Event) {
        Event javaEvent = (Event) event.getEvent();
        title.setText(javaEvent.getName());
        desc.setText(javaEvent.getDescription());
    }
}

@Override
public void getHeaderItemView(@NonNull View view, @NonNull Calendar day) {
    TextView header = view.findViewById(R.id.view_agenda_day_of_month);
    header.setText(headerFormat.format(day.getTime()));
}
}

MyCalendarEvent

public class MyCalendarEvent extends BaseCalendarEvent {

private Calendar startTime = Calendar.getInstance();
private Calendar endTime = Calendar.getInstance();
private Object event;
private IDayItem dayReference;
private IWeekItem weekReference;

public MyCalendarEvent(){}

public MyCalendarEvent(Calendar start, Calendar end,
                         IDayItem dayItem, Event event) {
    this.startTime = start;
    this.endTime = end;
    this.dayReference = dayItem;
    this.event = event;
}

@NotNull
@Override
public Calendar getStartTime() { return startTime; }

@NotNull
@Override
public Calendar getEndTime() { return endTime; }

@Nullable
@Override
public Object getEvent() { return event; }

@Override @NotNull
public MyCalendarEvent copy() {
    return new MyCalendarEvent(
            (Calendar) startTime.clone(),
            (Calendar) endTime.clone(),
            dayReference,
            (Event) event
    );
}

@Override
public boolean hasEvent() {
    return event != null;
}
}

Event

public class Event {
private long id;
private String name;
private String description;

public Event(String name, String description) {
    this.name = name;
    this.description = description;
}

// Getters and setters
public String getName() { return name; }
public String getDescription() { return description; }
}

I am getting a System.err in Logcat for these three portions of code:

constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {}
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {

    val inflater = context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
    inflater.inflate(R.layout.view_agenda, this, true)

    (findViewById(R.id.refresh_layout) as SwipeRefreshLayout).isEnabled = false
}
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {

     ...

     val inflater = context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
     inflater.inflate(R.layout.view_agendacalendar, this, true)
}

Following your advice in the comment, I searched the logcat in more detail and found more information:

Accessing hidden field Landroid/widget/AbsListView;->mSelectorRect:Landroid/graphics/Rect; (max-target-p, reflection, denied)
java.lang.NoSuchFieldException: No field mSelectorRect in class Landroid/widget/AbsListView; (declaration of 'android.widget.AbsListView' appears in /system/framework/framework.jar!classes4.dex)
    at java.lang.Class.getDeclaredField(Native Method)
    at se.emilsjolander.stickylistheaders.WrapperViewList.<init>(WrapperViewList.java:35)
    at se.emilsjolander.stickylistheaders.StickyListHeadersListView.<init>(StickyListHeadersListView.java:129)
    at se.emilsjolander.stickylistheaders.StickyListHeadersListView.<init>(StickyListHeadersListView.java:119)
    at com.ognev.kotlin.agendacalendarview.agenda.AgendaListView.<init>(AgendaListView.kt:18)
    at java.lang.reflect.Constructor.newInstance0(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
    at android.view.LayoutInflater.createView(LayoutInflater.java:742)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:894)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:849)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:1011)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:972)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:1014)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:972)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:1014)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:540)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:462)
    at com.ognev.kotlin.agendacalendarview.agenda.AgendaView.<init>(AgendaView.kt:28)
    at java.lang.reflect.Constructor.newInstance0(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
    at android.view.LayoutInflater.createView(LayoutInflater.java:742)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:894)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:849)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:1011)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:540)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:462)
    at com.ognev.kotlin.agendacalendarview.AgendaCalendarView.<init>(AgendaCalendarView.kt:64)
    at java.lang.reflect.Constructor.newInstance0(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
    at android.view.LayoutInflater.createView(LayoutInflater.java:742)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:894)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:849)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:1011)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:972)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:570)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:462)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:413)
    at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:751)
    at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:197)
    at com.ollytreeapplications.epilepsyjournal.agenda.AgendaActivity2.onCreate(AgendaActivity2.java:32)
    at android.app.Activity.performCreate(Activity.java:9002)
    at android.app.Activity.performCreate(Activity.java:8980)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1526)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:4030)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4235)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:112)
    at android.app.servertransaction.TransactionExecutor.executeNonLifecycleItem(TransactionExecutor.java:174)
    at android.app.servertransaction.TransactionExecutor.executeTransactionItems(TransactionExecutor.java:109)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:81)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2636)
    at android.os.Handler.dispatchMessage(Handler.java:107)
    at android.os.Looper.loopOnce(Looper.java:232)
    at android.os.Looper.loop(Looper.java:317)
    at android.app.ActivityThread.main(ActivityThread.java:8705)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:580)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:886)

Let me know if you need anything else.

本文标签: androidSystemerr when using inflate in kotlin library constructor in Java projectStack Overflow