1

The project run with no error on my MacBook intel chip but it gave me E/AndroidRuntime: FATAL EXCEPTION: main on M1 chip I can't find where the problem is. I suppose there is no error in my code because is already run on other devices and just facing different errors while trying to run it on M1.

the error message: E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.memeorshower, PID: 10861 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.memeorshower/com.example.memeorshower.MainActivity}: java.lang.RuntimeException: Cannot create an instance of class com.example.memeorshower.viewmodel.ImageTmpViewModel at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) at android.os.Handler.dispatchMessage(Handler.java:107) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7356) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

Thanks in advance

here is my mainActivity code

package com.example.memeorshower

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.lifecycle.ViewModelProvider
import com.example.memeorshower.database.texttmp.TextTmp
import com.example.memeorshower.databinding.ActivityMainBinding
import com.example.memeorshower.viewmodel.ImageTmpViewModel
import com.example.memeorshower.viewmodel.TextTmpViewModel

class MainActivity : AppCompatActivity() {
    lateinit var binding: ActivityMainBinding

    lateinit var myImageTmpViewModel: ImageTmpViewModel
    lateinit var myTextTmpViewModel: TextTmpViewModel


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        myImageTmpViewModel = ViewModelProvider(this).get(ImageTmpViewModel::class.java)
        myTextTmpViewModel = ViewModelProvider(this).get(TextTmpViewModel::class.java)

        // todo: you have access to both view models, load your data in them.

        binding.NewButton.setOnClickListener { makeNewMeme() }
        binding.MyProjectButton.setOnClickListener { showMyProjects() }
        binding.DatabaseButton.setOnClickListener { showDatabase() }

    }

    private fun showDatabase() {
        val intent = Intent(this, MemeDatabaseActivity::class.java)
        startActivity(intent)
    }

    private fun showMyProjects() {
        val intent = Intent(this, MyProjectsActivity::class.java)
        startActivity(intent)

    }

    private fun makeNewMeme() {
        val intent = Intent(this, TextTemplateActivity::class.java)
        startActivity(intent)
    }
}


build.gradle in app folder:

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'
}

android {
    compileSdkVersion 30

    defaultConfig {
        applicationId "com.example.memeorshower"
        minSdkVersion 14
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    buildFeatures {
        viewBinding true
    }

}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
//    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
    implementation 'androidx.recyclerview:recyclerview:1.2.1'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

    // Navigation Component
    implementation 'androidx.navigation:navigation-fragment-ktx:2.2.2'
    implementation 'androidx.navigation:navigation-ui-ktx:2.2.2'

    // Room components
    implementation "androidx.room:room-runtime:2.2.5"
    annotationProcessor "androidx.room:room-compiler:2.2.5"
    implementation "androidx.room:room-ktx:2.2.5"
    androidTestImplementation "androidx.room:room-testing:2.2.5"

    // Lifecycle components
    implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
    implementation "androidx.lifecycle:lifecycle-common-java8:2.2.0"
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0"

    // Kotlin components
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.72"
    api "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.5"
    api "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.5"

    implementation 'com.burhanrashid52:photoeditor:1.5.1'
}

build.gradle:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    ext.kotlin_version = "1.4.10"
    ext.room_version = '2.2.5'
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.0.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
        jcenter() // Warning: this repository is going to shut down soon


    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

ImageTmpViewModel class:

class ImageTmpViewModel(application: Application): AndroidViewModel(application) {
    private val readAllData: List<ImageTmp>
    private val imgDao = AppDatabase.getDatabase(application).imagetmpDao()

    init {
        readAllData = imgDao.getAll()
    }

    fun addImage(imgtmp: ImageTmp){
        viewModelScope.launch(Dispatchers.IO) {
            imgDao.addImage(imgtmp)
        }
    }

}

ImageTmpDao:

@Dao
interface ImageTmpDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun upsertByReplacement(image: List<ImageTmp>)

    @Insert(onConflict = OnConflictStrategy.IGNORE)
    suspend fun addImage(image: ImageTmp)

    @Query("SELECT * FROM image_tmp_table ORDER BY id ASC")
    fun getAll(): List<ImageTmp>

    @Query("SELECT * FROM image_tmp_table WHERE id = :id ")
    fun getByID(id: Int): ImageTmp

    @Delete
    fun delete(imgtmp: ImageTmp)
}
narsan
  • 62
  • 10
  • How does the constructor of your `ImageTmpViewModel` look like? – Muhannad Fakhouri Dec 12 '21 at 15:22
  • I edit the question and add it @MuhannadFakhouri – narsan Dec 12 '21 at 15:29
  • Do you get any Inner Exception too? is `imgDao.getAll()` safe to run on UI Thread? please post the full stacktrace – Muhannad Fakhouri Dec 12 '21 at 15:31
  • I know what are you talking about I already handle that and as I said it work perfectly on all other devices and the issue only happen on M1 but I add the code thanks in advance @MuhannadFakhouri – narsan Dec 12 '21 at 15:49
  • try updating room to 2.4.0 alpha 3, according to their changelog they should have done some fixes regarding this https://developer.android.com/jetpack/androidx/releases/room#2.4.0-alpha03 – Muhannad Fakhouri Dec 12 '21 at 15:56
  • still getting the error before facing this error I had `Execution failed for task :app:kaptDebugKotlin.` and with a solution found in stackoverflow I change `kapt "androidx.room:room-compiler:2.2.5"` to `annotationProcessor "androidx.room:room-compiler:2.2.5"` after solving that error this error raise :( @MuhannadFakhouri – narsan Dec 12 '21 at 16:38
  • Did you update to the latest version? Try running a clean build after updating the version – Muhannad Fakhouri Dec 12 '21 at 21:14

0 Answers0