Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

android studio app crash when open but no error found

Im currently making calendar events or reminder. i tried the app when i want to check of the calendar work and i can get the add reminder screen open but when i start the app it immediately crash.
this is the code for the customcalendar

package com.example.calendarevent2;

import android.app.AlertDialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.Layout;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AnalogClock;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import androidx.annotation.Nullable;

import org.w3c.dom.Text;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

public class CustomCalendarView extends LinearLayout {
    ImageButton NextButton,PreviousButton;
    TextView CurrentDate;
    GridView gridView;
    private static final int MAX_CALENDAR_DAYS = 42;
    Calendar calendar = Calendar.getInstance(Locale.ENGLISH);
    Context context;
    SimpleDateFormat dateFormat = new SimpleDateFormat("MMMM yyyy",Locale.ENGLISH);
    SimpleDateFormat monthFormat = new SimpleDateFormat("MMMM",Locale.ENGLISH);
    SimpleDateFormat yearFormate = new SimpleDateFormat("yyyy",Locale.ENGLISH);

    MyGridAdapter myGridAdapter;
    AlertDialog alertDialog;
    List<Date> dates = new ArrayList<>();
    List<Events> eventsList = new ArrayList<>();

    DBOpenHelper dbOpenHelper;

    public CustomCalendarView(Context context) {

    public CustomCalendarView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;

        PreviousButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                calendar.add(Calendar.MONTH, -1);
        NextButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                calendar.add(Calendar.MONTH, 1);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                View addView = LayoutInflater.from(parent.getContext()).inflate(R.layout.add_newevent_layout,null);
                EditText EventName = addView.findViewById(R.id.events_id);
                TextView EventTime = addView.findViewById(R.id.eventtime);
                ImageButton SetTime = addView.findViewById(R.id.seteventtime);
                Button AddEvent = addView.findViewById(R.id.addevent);
                SetTime.setOnClickListener(new OnClickListener() {
                    public void onClick(View v) {
                        Calendar calendar = Calendar.getInstance();
                        int hours = calendar.get(Calendar.HOUR_OF_DAY);
                        int minute = calendar.get(Calendar.MINUTE);
                        TimePickerDialog timePickerDialog = new TimePickerDialog(addView.getContext(), R.style.Theme_AppCompat_Dialog
                                , new TimePickerDialog.OnTimeSetListener() {
                            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                                Calendar c = Calendar.getInstance();
                                SimpleDateFormat hformate = new SimpleDateFormat("K:mm a", Locale.ENGLISH);
                                String event_Time = hformate.format(c.getTime());

                String date = dateFormat.format(dates.get(position));
                String month = monthFormat.format(dates.get(position));
                String year = yearFormate.format(dates.get(position));

                AddEvent.setOnClickListener(new OnClickListener() {
                    public void onClick(View v) {
                alertDialog = builder.create();

    public CustomCalendarView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    private void SaveEvent(String event,String time,String date,String month,String year){
        DBOpenHelper dbOpenHelper = new DBOpenHelper(context);
        SQLiteDatabase database = dbOpenHelper.getWritableDatabase();
        Toast.makeText(context, "Event Saved", Toast.LENGTH_SHORT).show();

    private void InitializeLayout(){
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.calender_layout, this);
        NextButton = view.findViewById(R.id.nextBtn);
        PreviousButton = view.findViewById(R.id.previousBtn);
        CurrentDate = view.findViewById(R.id.current_Date);
        gridView = view.findViewById(R.id.gridview);
    private void SetUpCalendar(){
        String  currwntDate = dateFormat.format(calendar.getTime());
        Calendar monthCalendar= (Calendar) calendar.clone();
        int FirstDayofMonth = monthCalendar.get(Calendar.DAY_OF_WEEK)-1;
        monthCalendar.add(Calendar.DAY_OF_MONTH, -FirstDayofMonth);

        while (dates.size() < MAX_CALENDAR_DAYS){
            monthCalendar.add(Calendar.DAY_OF_MONTH, 1);
        myGridAdapter = new MyGridAdapter(context,dates,calendar,eventsList);
    private void CollectEventsPerMonth(String Month,String year){
        dbOpenHelper= new DBOpenHelper(context);
        SQLiteDatabase database = dbOpenHelper.getReadableDatabase();
        Cursor cursor = dbOpenHelper.ReadEventsperMonth(Month,year,database);
            String event = cursor.getString(cursor.getColumnIndex(DBStructure.EVENT));
            String time = cursor.getString(cursor.getColumnIndex(DBStructure.TIME));
            String date = cursor.getString(cursor.getColumnIndex(DBStructure.DATE));
            String month = cursor.getString(cursor.getColumnIndex(DBStructure.MONTH));
            String Year = cursor.getString(cursor.getColumnIndex(DBStructure.YEAR));
            Events events = new Events(event,time,date,month,Year);

this is the code for the DBOpenHelper

package com.example.calendarevent2;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

public class DBOpenHelper extends SQLiteOpenHelper {
    private static final String CREATE_EVENTS_TABLE = "create table "+DBStructure.EVENT_TABLE_NAME+"(ID INTEGER PRIMARY KEY AUTOINCREMENT, "
            +DBStructure.EVENT+" TEXT, "+DBStructure.TIME+" TEXT, "+DBStructure.DATE+" TEXT, "+DBStructure.MONTH+" TEXT, "+DBStructure.YEAR+" TEXT";
    private static final String DROP_EVENTS_TABLE= "DROP TABLE IF EXISTS"+DBStructure.EVENT_TABLE_NAME;

    public DBOpenHelper(@Nullable Context context) {
        super(context, DBStructure.DB_NAME, null,DBStructure.DB_VERSION);

    public void onCreate(SQLiteDatabase db) {


    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    public void SaveEvent(String event,String time,String date,String month,String year,SQLiteDatabase database){
        ContentValues contentValues = new ContentValues();
        database.insert(DBStructure.EVENT_TABLE_NAME, null, contentValues);
    public Cursor ReadEvents(String date,SQLiteDatabase database){
        String [] Projections = {DBStructure.EVENT,DBStructure.TIME,DBStructure.DATE,DBStructure.MONTH,DBStructure.YEAR};
        String Selection = DBStructure.DATE +"=?";
        String [] SelectionArgs = {date};

        return database.query(DBStructure.EVENT_TABLE_NAME,Projections,Selection,SelectionArgs, null, null, null);
    public Cursor ReadEventsperMonth(String month,String year,SQLiteDatabase database){
        String [] Projections = {DBStructure.EVENT,DBStructure.TIME,DBStructure.DATE,DBStructure.MONTH,DBStructure.YEAR};
        String Selection = DBStructure.MONTH +"=? and "+DBStructure.YEAR+"=?";
        String [] SelectionArgs = {month,year};
        return database.query(DBStructure.EVENT_TABLE_NAME,Projections,Selection,SelectionArgs, null, null, null);

this is the build.gradle i changed to

dependencies {

    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.2.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'androidx.recyclerview:recyclerview:1.1.0-alpha04'
    implementation 'com.google.android.material:material:1.1.0-alpha05'

there are no error or fault found in the code but when i start it just crash

Link to post
Share on other sites

Download the Android studio installer again. 


Run it and select repair as the option during the installation process. 


It's possible Android studio installed incorrectly. No consistent expansion of why it happens, it just does sometimes. That's all programs though. 

Don’t take any of my posts seriously. 


LTT Fan Fiction:


PC game list: 


Link to post
Share on other sites
10 hours ago, Chris2002 said:

there are no error or fault found in the code but when i start it just crash

Are you using a phone or emulator?  I find my phone doesn't always display the crash information easily.  The emulator usually does a pretty good job on indicating the line it's crashing on.


Another thing to consider is adding some debug text

Log.d("Category", "Log Text");


That way, you can put it around areas that might be crashing (or calls that you know are called when the program starts up).  That way you can trace back (albeit slower) to where the bug actually is coming from (you have to make sure to run it in Debug mode...actually make sure to run your program in debug mode anyways, it should be right next to the run mode)



    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    	Log.d("SQLUpgrade", "Entry");
    	Log.d("SQLUpgrade", "execSQL");
    	Log.d("SQLUpgrade", "onCreated Called");

If you were to see in the debug messages "Entry" and "execSQL" but not "onCreated Called" then you would know that onCreate is failing out.  It's more old school, but it can really help track down minor bugs that cause weird behaviour

3735928559 - Beware of the dead beef

Link to post
Share on other sites

I redo the process and found out the reason for the crash


when i inserted this line the app crashes. any reason why?

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now