ActionBar.setDisplayHomeAsUpEnabled(布尔值)'在null对象引用上

时间:2017-04-28 13:02:57

标签: javascript java android android-studio

import android.content.Context;
import android.graphics.Color;
import android.icu.text.SimpleDateFormat;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import com.github.sundeepk.compactcalendarview.CompactCalendarView;
import com.github.sundeepk.compactcalendarview.domain.Event;

import java.util.Date;
import java.util.Locale;

public class Calendarpage extends AppCompatActivity {

    //modify here
    ActionBar actionBar = null;

    CompactCalendarView compactCalendar;
    private SimpleDateFormat dateFormatMonth = new SimpleDateFormat ("MMM- yyyy", Locale.getDefault());

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

        // modify here
        ActionBar myActionBar = getSupportActionBar();
        if (myActionBar != null) {
            myActionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setTitle(null);
        }

        compactCalendar = (CompactCalendarView) findViewById(R.id.compactcalendar_view);
        compactCalendar.setUseThreeLetterAbbreviation(true);

        Event ev1 = new Event(Color.RED, 1514160000L, "Chirstmas Day");
        compactCalendar.addEvent(ev1);

        compactCalendar.setListener(new CompactCalendarView.CompactCalendarViewListener() {
            @Override
            public void onDayClick(Date dateClicked) {
                Context context = getApplicationContext();

                if (dateClicked.toString().compareTo("Mon Dec 25 00:00:00 AST 2017") == 0) {
                    Toast.makeText(context, "Christmas Day", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(context, "No Events Planned That Day", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onMonthScroll(Date firstDayOfNewMonth) {
                getSupportActionBar.setTitle(String.valueOf(dateFormatMonth.format(firstDayOfNewMonth)));
            }
        });
    }
}

现在添加了您所做的更改,但getSupportActionBar。是红色的?它只是要求我在右键单击时创建局部变量。非常强调这一点,因为我无法继续这个页面工作..因为我仍然需要能够将事件添加到日期

1 个答案:

答案 0 :(得分:0)

尝试进行此更改

在你的OnCreate中:

    ActionBar myActionBar = getSupportActionBar();
    if (myActionBar != null) {
        myActionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setTitle(null);
    }

在你的setListener

        @Override
        public void onMonthScroll(Date firstDayOfNewMonth) {
             getSupportActionBar().setTitle(String.valueOf(
             dateFormatMonth.format(firstDayOfNewMonth)));
        }
祝你好运

编辑试试这个

        compactCalendar.setListener(new CompactCalendarView.CompactCalendarViewListener() {    
            @Override
            public void onMonthScroll(Date firstDayOfNewMonth) {
                setNewTitleBar(dateFormatMonth.format(firstDayOfNewMonth));
            }
        });
    }

    private void setNewTitleBar(String newTitle) {
        getSupportActionBar().setTitle(newTitle);
    }
}