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)
}