MagicMashRoom / SuperCalendar

@Deprecated android 自定义日历控件 支持左右无限滑动 周月切换 标记日期显示 自定义显示效果跳转到指定日期
2.76k stars 484 forks source link

v1.3.1 CustomDayView 为 null #4

Open 1277379283 opened 7 years ago

1277379283 commented 7 years ago

你好,很高兴能将这个自定义控件分享出来,本人非常喜欢,但是在本人开发的过程中,总是报了一个 CustomDayView 方法为 null 的问题,如下所示:

Caused by: java.lang.NullPointerException at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:529) at android.view.View.measure(View.java:15518) at com.ldf.calendar.view.DayView.setupLayoutResource(DayView.java:44) at com.ldf.calendar.view.DayView.(DayView.java:32) at calendar.oa.com.myapplication.CustomDayView.(CustomDayView.java:36) at calendar.oa.com.myapplication.MainActivity.initCalendarView(MainActivity.java:114) at calendar.oa.com.myapplication.MainActivity.onCreate(MainActivity.java:61)

image

*开始我以为控件一直没有获取到,后来即使是改成这样: image

也还是会报空,不知道问题出在哪,所以能帮忙看看具体问题出现在哪嘛?

MagicMashRoom commented 7 years ago

at calendar.oa.com.myapplication.MainActivity.initCalendarView(MainActivity.java:114) 把你的MainActivity代码截个图看一下吧

1277379283 commented 7 years ago

image

MagicMashRoom commented 7 years ago

麻烦再把CustomDayView的全部代码发一下吧

1277379283 commented 7 years ago

package calendar.oa.com.myapplication;

import android.content.Context; import android.graphics.Color; import android.view.LayoutInflater; import android.view.View; import android.widget.ImageView; import android.widget.TextView;

import com.ldf.calendar.Utils; import com.ldf.calendar.component.State; import com.ldf.calendar.interf.IDayRenderer; import com.ldf.calendar.model.CalendarDate; import com.ldf.calendar.view.DayView;

/**

public class CustomDayView extends DayView {

private TextView dateTv;
private ImageView marker;
private View selectedBackground;
private View todayBackground;
private final CalendarDate today = new CalendarDate();

/**
 * Constructor. Sets up the MarkerView with a custom layout resource.
 *
 * @param context
 * @param layoutResource the layout resource to use for the MarkerView
 */
public CustomDayView(Context context, int layoutResource) {
    super(context, layoutResource);
    final LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View  contentView = inflater.inflate(R.layout.custom_day,null);
    dateTv = (TextView) contentView.findViewById(R.id.date);
    marker = (ImageView) contentView.findViewById(R.id.maker);
    selectedBackground = contentView.findViewById(R.id.selected_background);
    todayBackground = contentView.findViewById(R.id.today_background);
}

@Override
public void refreshContent() {
    renderToday(day.getDate());
    renderSelect(day.getState());
    renderMarker(day.getDate(), day.getState());
    super.refreshContent();
}

private void renderMarker(CalendarDate date, State state) {
    if(Utils.loadMarkData().containsKey(date.toString())) {
        if(state == State.SELECT || date.toString().equals(today.toString())) {
            marker.setVisibility(GONE);
        } else {
            marker.setVisibility(VISIBLE);
            if(Utils.loadMarkData().get(date.toString()).equals("0") ){
                marker.setEnabled(true);
            } else {
                marker.setEnabled(false);
            }
        }
    } else {
        marker.setVisibility(GONE);
    }
}

private void renderSelect(State state) {
    if (state == State.SELECT) {
        selectedBackground.setVisibility(VISIBLE);
        dateTv.setTextColor(Color.WHITE);
    } else if(state == State.NEXT_MONTH || state == State.PAST_MONTH) {
        selectedBackground.setVisibility(GONE);
        dateTv.setTextColor(Color.parseColor("#d5d5d5"));
    } else {
        selectedBackground.setVisibility(GONE);
        dateTv.setTextColor(Color.parseColor("#111111"));
    }
}

private void renderToday(CalendarDate date) {
    if(date != null) {
        if(date.equals(today)) {
            dateTv.setText("今");
            todayBackground.setVisibility(VISIBLE);
        } else {
            dateTv.setText(date.day + "");
            todayBackground.setVisibility(GONE);
        }
    }
}

@Override
public IDayRenderer copy() {
    return new CustomDayView(context , layoutResource);
}

}

1277379283 commented 7 years ago

嗨 我发现 CustomDayView一直获取的控件就是null ,但是 我用了 很多方法 还是不行,有更好的建议嘛?

MagicMashRoom commented 7 years ago

你把你的项目包发过来吧 这么描述我也没法改 QQ 2286767746

zhanghangyes commented 7 years ago

同上, CustomDayView customDayView = new CustomDayView(context , R.layout.custom_day);也是返回null

PhoenixGJH commented 7 years ago

我也遇到了这个问题,三星note2,4.3会出这个问题,7.1.1以及8.0不会出现这个问题。

fenwind commented 7 years ago

我也遇到了这个问题,4.3会出这个问题,5.1,7.1.1以及8.0不会出现这个问题。

fenwind commented 7 years ago

(⊙o⊙)哦已经 解决了,这个控件很不错。收藏了哈哈

JobeReeves commented 7 years ago

楼上怎么解决的

fenwind commented 7 years ago

qq 20170920173117 加上 红框里的一行

JobeReeves commented 7 years ago

谢谢,解决了~。貌似是RelativeLayout的一个bug,系统低于4.4.4都会有问题,改为继承LinearLayout就OK了