Search code examples
androidandroid-studiokotlinandroid-sqlite

App Shows White Screen and closed automatically(Android Studio)


My app is not working I try to implement a simple app showing contents and contents are stored in sql database. My app starts and show a complete white screen and after some time it closed automatically. After closing I got a error in logcat is java.lang.OutOfMemoryError: OutOfMemoryError thrown while trying to throw OutOfMemoryError;

no stack trace available

User.kt

class User{
    var id = 0
    var time = ""
    var amp = ""
    var text = ""
}

MainActivity.kt

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(toolbar)
        title = "Maintainer"
        val dbHelper = SchDbHandler(this)
        val recyclerView = findViewById<RecyclerView>(R.id.recycler)
        recyclerView.layoutManager = LinearLayoutManager(this)
        val adapter = Adapter(dbHelper.readData())
        recyclerView.adapter = adapter
        fab.setOnClickListener {
            val user = User()
            user.time = "10:10"
            user.amp = "AM"
            user.text = "I am here"
            dbHelper.insertData(user)
            adapter.notifyItemChanged(dbHelper.readData().size - 1)
        }
    }
}

Database Handler file

class SchDbHandler(var context: Context): SQLiteOpenHelper(context, DB_NAME, null, DB_VERSION) {
    override fun onCreate(db: SQLiteDatabase?) {
        val createScheduleTable: String = "CREATE TABLE $TABLE_NAME (" +
        "$COL_ID INTEGER PRIMARY KEY AUTOINCREMENT," +
        "$COL_TIME VARCHAR(256)," +
        "$COL_AMP VARCHAR(256)," +
        "$COL_TEXT VARCHAR(256)); "
        db?.execSQL(createScheduleTable)
    }

    override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    fun insertData(user: User){
        val db = this.writableDatabase
        var cv = ContentValues()
        cv.put(COL_TIME, user.time)
        cv.put(COL_AMP, user.amp)
        cv.put(COL_TEXT, user.text)
        var result = db.insert(TABLE_NAME, null, cv)
        if(result == (-1).toLong()){
            Toast.makeText(context, "FAILED", Toast.LENGTH_SHORT).show()
        }
        else{
            Toast.makeText(context, "SUCCESS", Toast.LENGTH_SHORT).show()
        }
    }

    fun readData():MutableList<User>{
        var list: MutableList<User> = ArrayList()
        val db = this.readableDatabase
        val query = " SELECT * from $TABLE_NAME "
        val result = db.rawQuery(query, null)
        if(result.moveToFirst()){
            do{
                var user = User()
                user.id = result.getString(result.getColumnIndex(COL_ID)).toInt()
                user.time = result.getString(result.getColumnIndex(COL_TIME))
                user.amp = result.getString(result.getColumnIndex(COL_AMP))
                user.text = result.getString(result.getColumnIndex(COL_TEXT))
                list.add(user)
            }while(result.moveToFirst())
        }
        result.close()
        db.close()
        return list
    }
}

Solution

  • Thanks to all who tried to solve. But It's now working. What I did is just replace this code segment with

    val result = db.rawQuery(query, null)
            if(result.moveToFirst()){
                do{
                    var user = User()
                    user.id = result.getString(result.getColumnIndex(COL_ID)).toInt()
                    user.time = result.getString(result.getColumnIndex(COL_TIME))
                    user.amp = result.getString(result.getColumnIndex(COL_AMP))
                    user.text = result.getString(result.getColumnIndex(COL_TEXT))
                    list.add(user)
                }while(result.moveToFirst())
            }
            result.close()
    

    to

    val cursor: Cursor = db.rawQuery(query, null)
            if(cursor.moveToFirst()) {
                while (!cursor.isAfterLast) {
                    var user = User()
                    user.id = cursor.getString(cursor.getColumnIndex(COL_ID)).toInt()
                    user.time = cursor.getString(cursor.getColumnIndex(COL_TIME))
                    user.amp = cursor.getString(cursor.getColumnIndex(COL_AMP))
                    user.text = cursor.getString(cursor.getColumnIndex(COL_TEXT))
                    list.add(user)
                    cursor.moveToNext();
                }
            }
            cursor.close()