0

I've just finished my last android project but I'm facing a weird problem. All the time I was working on the app, I used to run it on my device by clicking that green button and it worked fine. But now that I've created my app's apk file, it installes on device with no problem, but at the time I want to start the app, it crashes out and never starts.

Any help will be appreciated. Thanks.

UPDATE: Dependencies:

implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support:design:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
implementation 'com.android.support:support-vector-drawable:26.1.0'
implementation 'com.android.support:preference-v14:26.1.0'
implementation 'com.github.codekidX:storage-chooser:2.0.4.2'
implementation 'com.google.firebase:firebase-storage:16.0.1'
implementation 'com.google.firebase:firebase-auth:16.0.3'
implementation 'com.google.firebase:firebase-core:16.0.1'
implementation 'android.arch.lifecycle:viewmodel:1.1.1'
implementation 'android.arch.lifecycle:extensions:1.1.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.android.support:cardview-v7:26.1.0'

ViewModel Class:

private class MyImageViewModel: ViewModel(){
    fun returnImagePath(): String{
        return createdImageFile
        //createdImageFile is a global string that contains the path of an image and its default value is ""
    }
}

ViewModelUsage:

val model = ViewModelProviders.of(this).get(MyImageViewModel::class.java)
    createdImageFile = model.returnImagePath()
    if (createdImageFile != ""){
        //do sth with the image file path
    }
Soroush
  • 89
  • 3
  • 11

2 Answers2

0

Solution:

Instead of:

val model = ViewModelProviders.of(this).get(MyImageViewModel::class.java)

write:

var model: MyImageViewModel()

then,

model = ViewModelProviders.of(this).get(MyImageViewModel::class.java)

Try it, Hopefully it should work.

Ümañg ßürmån
  • 9,695
  • 4
  • 24
  • 41
  • Sorry nothing changed – Soroush Aug 28 '18 at 18:27
  • @Soroush Sorry, but I had no such errors.. Infact I used your code, your dependency, everything. App works fine. My device Moto X Play. I made the apk, copied in internal storage, installed opened fine. – Ümañg ßürmån Aug 28 '18 at 18:47
  • @Soroush Try this: "'implementation 'android.arch.lifecycle:viewmodel:1.1.1' -ktx implementation 'android.arch.lifecycle:extensions:1.1.1' -ktx" – Ümañg ßürmån Aug 28 '18 at 18:51
0

Thanks to everyone who tried to help, I found the problem. It is a little stupid thing. ViewModel class should not be private! That's it.

Soroush
  • 89
  • 3
  • 11