我有这段代码,当我转到主要活动时,它立即提示日历视图,而当我选择一个日期时,它立即在Alertdialog中提示“ GrabageApp已停止”。这是我的代码。
public class MainActivity extends AppCompatActivity {
public static final String RESULT = "result";
public static final String EVENT = "event";
private static final int ADD_NOTE = 44;
static CalendarView mCalendarView;
private List<EventDay> mEventDays = new ArrayList<>();
Database_Helper db;
MyEventDay myEventDay1;
private BottomSheetBehavior mBottomSheetBehavior;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
db = new Database_Helper(this);
final View bottomSheet = findViewById(R.id.bottom_sheet);
final ImageButton button_add_event = (ImageButton) findViewById(R.id.button_add_event);
mBottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
mCalendarView = (CalendarView) findViewById(R.id.calendarView);
FloatingActionButton floatingActionButton = (FloatingActionButton) findViewById(R.id.floatingActionButton);
button_add_event.setOnClickListener(new View.OnClickListener() {
boolean bottomexpand = false;
@Override
public void onClick(View v) {
if(bottomexpand == false) {
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
button_add_event.setImageResource(R.mipmap.arrowdown);
Intent addItem=new Intent(MainActivity.this,MainActivity.class);
startActivity(addItem);
bottomexpand=true;
}else if(bottomexpand){
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
button_add_event.setImageResource(R.mipmap.arrowup);
bottomexpand = false;
}
}
});
mBottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
boolean bottomexpand1 = false;
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
switch (newState) {
case BottomSheetBehavior.STATE_COLLAPSED:
break;
case BottomSheetBehavior.STATE_DRAGGING:
while(true) {
if (bottomexpand1 == false) {
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_DRAGGING);
bottomexpand1 = true;
button_add_event.setImageResource(R.mipmap.arrowup);
} else {
if (bottomexpand1) {
button_add_event.setImageResource(R.mipmap.arrowdown);
bottomexpand1 = false;
}
}
break;
}
case BottomSheetBehavior.STATE_EXPANDED:
break;
case BottomSheetBehavior.STATE_HIDDEN:
break;
case BottomSheetBehavior.STATE_SETTLING:
break;
}
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
}
});
// get all data from sqlite
Cursor cursor = db.getDataClass("Select * From Table_Task");
if (cursor != null && cursor.getCount() > 0) {
while (cursor.moveToNext()) {
String task = cursor.getString(0);
String location_ = cursor.getString(1);
String when = cursor.getString(2);
String starttime = cursor.getString(3);
String month = cursor.getString(4);
SimpleDateFormat format = new SimpleDateFormat("dd MMMM yyyy");
try {
Date date = format.parse(when);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
myEventDay1 = new MyEventDay(cal,R.drawable.today, task,location_,month);
mEventDays.add(myEventDay1);
mCalendarView.setEvents(mEventDays);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addNote();
}
});
mCalendarView.setOnDayClickListener(new OnDayClickListener() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onDayClick(EventDay eventDay) {
previewNote(eventDay);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == ADD_NOTE && resultCode == RESULT_OK) {
/* // get all data from sqlite
Cursor cursor = db.getDataClass("Select * From Table_Task");
if (cursor != null && cursor.getCount() > 0) {
while (cursor.moveToNext()) {
String task = cursor.getString(0);
String location_ = cursor.getString(1);
String when = cursor.getString(2);
String starttime = cursor.getString(3);
Toast.makeText(this,task+when+location_+starttime,Toast.LENGTH_LONG).show();
}
}*/
MyEventDay myEventDay = data.getParcelableExtra(RESULT);
mCalendarView.setDate(myEventDay.getCalendar());
mEventDays.add(myEventDay);
mCalendarView.setEvents(mEventDays);
}
}
private void addNote() {
Intent intent = new Intent(this, AddNoteActivity.class);
startActivityForResult(intent, ADD_NOTE);
}
@RequiresApi(api = Build.VERSION_CODES.N)
private void previewNote(EventDay eventDay) {
// get all data from sqlite
Cursor cursor = db.getDataClass("Select * From Table_Task");
Intent intent = new Intent(this, NotePreviewActivity.class);
if(eventDay instanceof MyEventDay){
}
intent.putExtra("Val",String.valueOf(getFormattedDate(mCalendarView.getSelectedDate().getTime())));
startActivity(intent);
}
}
我是实现日历视图的新手。结果将是选择日期时。活动本身会提示它,但有错误。我试图检查单击它时的意图以及清单文件。请有人指出语法错误。