using OkHTTP I am trying to retrieve a text file and put hat into the textView on the main activity,
Gradle will say build successful but the emulator app will just crash on launch and provide this error.
I have added the correct permissions in the AndroidManifest.xml and the correct implementation in the gradle build
package com.example.vectortestapi
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import okhttp3.*
import java.io.IOException
abstract class MainActivity : AppCompatActivity() {
private val mTextView: TextView = findViewById<TextView>(R.id.resultText);
private val client = OkHttpClient()
private val url = "https://publicobject.com/helloworld.txt"
private val request: Request = Request.Builder()
.url(url)
.build();
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
getAPI();
}
private fun getAPI(){
client.newCall(request).enqueue(object: Callback {
override fun onFailure(call: Call, e: IOException) {
e.printStackTrace()
}
override fun onResponse(call: Call, response: Response) {
val inputStream = response.body?.string()
runOnUiThread{
mTextView.text = inputStream
}
}
})
}
}
produces this error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.vectortestapi, PID: 9900
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.vectortestapi/com.example.vectortestapi.MainActivity}: java.lang.InstantiationException: java.lang.Class<com.example.vectortestapi.MainActivity> cannot be instantiated
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3365)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
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:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.InstantiationException: java.lang.Class<com.example.vectortestapi.MainActivity> cannot be instantiated
at java.lang.Class.newInstance(Native Method)
at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45)
at android.app.Instrumentation.newActivity(Instrumentation.java:1253)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3353)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
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:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
D/libEGL: loaded /vendor/lib/egl/libGLESv2_emulation.so
I/Process: Sending signal. PID: 9900 SIG: 9
Abstract classes cannot be instantiated, but they can be subclassed.
Related
I'm just starting with the Mapbox implementation in a native Android app and I just can't get it done.
The most basic thing (instantiating a MapboxNavigation can not be done). I get this error and the app closes:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.testemapbox, PID: 29380
java.lang.NoSuchMethodError: No static method getInstance()Lcom/mapbox/common/LogConfiguration; in class Lcom/mapbox/common/LogConfiguration; or its super classes (declaration of 'com.mapbox.common.LogConfiguration' appears in /data/app/~~CoIOdGRtIbaTlwz2rI-MFg==/com.example.testemapbox-_1CkcSTRk6UI5QXFz-w_6g==/base.apk)
at com.mapbox.navigation.utils.internal.LoggerProvider.initialize(LoggerProvider.kt:23)
at com.mapbox.navigation.core.MapboxNavigation.<init>(MapboxNavigation.kt:396)
at com.mapbox.navigation.core.MapboxNavigation.<init>(MapboxNavigation.kt:223)
at com.mapbox.navigation.core.MapboxNavigationProvider.create(MapboxNavigationProvider.kt:23)
at com.example.testemapbox.MainActivity.onCreate(MainActivity.kt:20)
at android.app.Activity.performCreate(Activity.java:8000)
at android.app.Activity.performCreate(Activity.java:7984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
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:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
And here is the snippet of my code:
package com.example.testemapbox
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import com.mapbox.navigation.base.options.NavigationOptions
import com.mapbox.navigation.base.trip.model.RouteProgress
import com.mapbox.navigation.core.MapboxNavigationProvider
import com.mapbox.navigation.core.trip.session.RouteProgressObserver
class MainActivity : AppCompatActivity(), RouteProgressObserver {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val navigationOptions = NavigationOptions.Builder(this)
.accessToken(getString(R.string.mapbox_access_token))
.build()
val mapboxNavigation = MapboxNavigationProvider.create(navigationOptions)
}
}
I need to know if I'm doing something wrong. I know the code isn't useful (just don't do anything) but I suppose that I should have to be able to do something like that.
I am trying to apply Hilt as a dependency injection framework for my multi module project. I have a module called :app which includes my Application class :
#HiltAndroidApp
class SampleApplication : Application() {
override fun onCreate() {
super.onCreate()
if (BuildConfig.DEBUG) Timber.plant(Timber.DebugTree())
}
}
I have a :feature module which includes my single Activity and Fragments using Navigation architecture component :
#AndroidEntryPoint
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
I can build the project successfully but as soon as it runs on the device, I receive following exception :
Process: com.android.sample.viaplay, PID: 27219
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.sample.viaplay/com.android.sample.viaplay.feature.list.MainActivity}: java.lang.IllegalStateException: Hilt Activity must be attached to an #AndroidEntryPoint Application. Found: class com.android.sample.viaplay.ViaplayApplication
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
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:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.IllegalStateException: Hilt Activity must be attached to an #AndroidEntryPoint Application. Found: class com.android.sample.viaplay.ViaplayApplication
at dagger.hilt.android.internal.managers.ActivityComponentManager.createComponent(ActivityComponentManager.java:83)
at dagger.hilt.android.internal.managers.ActivityComponentManager.generatedComponent(ActivityComponentManager.java:66)
at com.android.sample.viaplay.feature.list.Hilt_MainActivity.generatedComponent(Hilt_MainActivity.java:45)
at com.android.sample.viaplay.feature.list.Hilt_MainActivity.inject(Hilt_MainActivity.java:67)
at com.android.sample.viaplay.feature.list.Hilt_MainActivity$1.onContextAvailable(Hilt_MainActivity.java:38)
at androidx.activity.contextaware.ContextAwareHelper.dispatchOnContextAvailable(ContextAwareHelper.java:99)
at androidx.activity.ComponentActivity.onCreate(ComponentActivity.java:297)
at androidx.fragment.app.FragmentActivity.onCreate(FragmentActivity.java:273)
at androidx.appcompat.app.AppCompatActivity.onCreate(AppCompatActivity.java:115)
at com.android.sample.viaplay.feature.list.MainActivity.onCreate(MainActivity.kt:10)
at android.app.Activity.performCreate(Activity.java:8000)
at android.app.Activity.performCreate(Activity.java:7984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
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:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
As you see it says : Hilt Activity must be attached to an #AndroidEntryPoint Application.
You can find the source code at : https://github.com/alirezaeiii/Hilt-MultiModule-Cache
I missed to add Hilt plugin and dependencies in build.gradle.kts for feature module :
plugins {
id("commons.android-library")
id(BuildPlugins.HILT)
}
dependencies {
implementation(Dependencies.HILT)
kapt(AnnotationProcessorsDependencies.HILT)
}
object BuildPlugins {
const val HILT = "dagger.hilt.android.plugin"
}
object Dependencies {
const val HILT = "com.google.dagger:hilt-android:${BuildDependenciesVersions.DAGGER}"
object AnnotationProcessorsDependencies {
const val HILT = "com.google.dagger:hilt-android-compiler:${BuildDependenciesVersions.DAGGER}"
}
I have an app now published on the play store. In debug, the app runs perfectly but when I tried to download the release app crashes instantly and it gives me this error.
2021-03-04 15:02:40.195 15100-15100/com.uhi5d.spotibud E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.uhi5d.spotibud, PID: 15100
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.uhi5d.spotibud/com.uhi5d.spotibud.load.ContentActivity}: java.lang.NoSuchMethodException: d.e.a.e.b.<init> [interface c.p.i, class android.content.Context]
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3310)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3459)
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:2046)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:225)
at android.app.ActivityThread.main(ActivityThread.java:7564)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
Caused by: java.lang.NoSuchMethodException: d.e.a.e.b.<init> [interface c.p.i, class android.content.Context]
at java.lang.Class.getConstructor0(Class.java:2332)
at java.lang.Class.getConstructor(Class.java:1728)
at d.e.a.e.a.a(CustomViewModelFactory.kt:10)
at c.p.u.b(ViewModelProvider.java:187)
at c.p.u.a(ViewModelProvider.java:150)
at com.uhi5d.spotibud.load.ContentActivity.onCreate(ContentActivity.kt:33)
at android.app.Activity.performCreate(Activity.java:7916)
at android.app.Activity.performCreate(Activity.java:7903)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3285)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3459)
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:2046)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:225)
at android.app.ActivityThread.main(ActivityThread.java:7564)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
My ViewModel factory
public class CustomViewModelFactory(val lifecycleOwner: LifecycleOwner,public val context: Context):ViewModelProvider.Factory {
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
return modelClass.getConstructor(LifecycleOwner::class.java,Context::class.java).newInstance(lifecycleOwner,context)
}
}
And error part
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_content)
// ViewModel components
factory = CustomViewModelFactory(this,this)
viewmodel = ViewModelProvider(this, factory!!).get(MainViewModel::class.java)
I'm trying to request permistions as it is written in the https://developer.android.com/training/permissions/requesting
Something is wrong.
In Logcat there is no Log.d output from callback of registerForActivityResult.
proggy() not run from callback. It set permissions okey.
But
when I changed
implementation 'androidx.activity:activity-ktx:1.2.0-alpha06'
to
implementation 'androidx.activity:activity-ktx:1.2.0-alpha07'
I got:
2020-07-30 01:42:55.698 12259-12259/tk.kvakva.myapplication D/AndroidRuntime: Shutting down VM
2020-07-30 01:42:55.701 12259-12259/tk.kvakva.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: tk.kvakva.myapplication, PID: 12259
java.lang.RuntimeException: Unable to start activity ComponentInfo{tk.kvakva.myapplication/tk.kvakva.myapplication.MainActivity}: java.lang.IllegalArgumentException: Can only use lower 16 bits for requestCode
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)
Caused by: java.lang.IllegalArgumentException: Can only use lower 16 bits for requestCode
at androidx.fragment.app.FragmentActivity.checkForValidRequestCode(FragmentActivity.java:715)
at androidx.fragment.app.FragmentActivity.validateRequestPermissionsRequestCode(FragmentActivity.java:730)
at androidx.core.app.ActivityCompat.requestPermissions(ActivityCompat.java:500)
at androidx.activity.ComponentActivity$2.onLaunch(ComponentActivity.java:178)
at androidx.activity.result.ActivityResultRegistry$3.launch(ActivityResultRegistry.java:147)
at androidx.activity.result.ActivityResultLauncher.launch(ActivityResultLauncher.java:42)
at tk.kvakva.myapplication.MainActivity.onCreate(MainActivity.kt:39)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
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)
import android.Manifest
import android.content.pm.PackageManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.activity.result.contract.ActivityResultContracts
class MainActivity : AppCompatActivity() {
val TAG = "MY_MainAct"
fun proggy(){
Log.d(TAG, "!!!!!!!!!!!!!!!!!! proggy() !!!!!!!!!!!!!!!!")
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val permReqLuncher = registerForActivityResult(ActivityResultContracts.RequestPermission()){
Log.d(TAG,"!!!!!!!++++++++++++++!!!!!CALL BACK!!!!!!!!!!+++++++++++++++!!!!!!!!!!!!!!!!!!!!!")
if(it) {
Log.d(TAG,"registerForActivityResult(ActivityResultContracts.RequestPermission()){ it: $it")
proggy();
}
else
Log.d(TAG,"Perm Not Granted")
}
when(checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
PackageManager.PERMISSION_GRANTED -> {
Log.d(TAG,"in onCreate when checkSelfPerm grated")
proggy()
}
PackageManager.PERMISSION_DENIED -> {
Log.d(TAG,"!!!!!!!!!!! in onCreate when checkSelfPermistion DENIED")
permReqLuncher.launch( Manifest.permission.WRITE_EXTERNAL_STORAGE )
}
}
}
}
I added
implementation 'androidx.fragment:fragment-ktx:1.3.0-alpha07'
Callback runs.
Exceptions gone.
I am learning android MVVM pattern android development. I wanted to create a recycclerview activity to show data from server. But it's not accepting my viewmodel class. Bellow is my logcat errors.
LOGCAT
2020-04-12 21:03:18.026 2008-2008/com.shovon.mvvm E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.shovon.mvvm, PID: 2008
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.shovon.mvvm/com.shovon.mvvm.activity.RecyclerActivity}: java.lang.RuntimeException: Cannot create an instance of class com.shovon.mvvm.viewmodel.RecyclerActivityViewModel
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)
Caused by: java.lang.RuntimeException: Cannot create an instance of class com.shovon.mvvm.viewmodel.RecyclerActivityViewModel
at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:221)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:187)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)
at com.shovon.mvvm.activity.RecyclerActivity.onCreate(RecyclerActivity.kt:23)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
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)
**Caused by: java.lang.InstantiationException: java.lang.Class<com.shovon.mvvm.viewmodel.RecyclerActivityViewModel> has no zero argument constructor**
at java.lang.Class.newInstance(Native Method)
at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:219)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:187)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)
at com.shovon.mvvm.activity.RecyclerActivity.onCreate(RecyclerActivity.kt:23)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
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)
VIEWMODEL
Here is my viewmodel class. I am passing some data to activity from this viewmodel. But problem is activity does not accept this viewmodel. Maybe viewmodel has any issue.
class RecyclerActivityViewModel(application: Application) : AndroidViewModel(application) {
val repository = RecyclerActivityRepository(application)
fun userData(): LiveData<ArrayList<UserResponse>>{
return repository.getUserData()
}
}
REPOSITORY
class RecyclerActivityRepository(application: Application) {
var liveData = MutableLiveData<ArrayList<UserResponse>>()
private val userList = ArrayList<UserResponse>()
private var adapter: UserAdapter? = null
fun getUserData(): MutableLiveData<ArrayList<UserResponse>>{
if (liveData == null){
liveData = MutableLiveData<ArrayList<UserResponse>>()
}
RetrofitClient.instance.getUsers()
.enqueue(object : Callback<List<UserResponse>> {
override fun onFailure(call: Call<List<UserResponse>>, t: Throwable) {
Log.v("Retrofit", t.message.toString())
}
override fun onResponse(
call: Call<List<UserResponse>>,
response: Response<List<UserResponse>>
) {
Log.v("Retrofit", "Success")
Log.v("response_code", response.code().toString())
try {
userList.addAll(response.body()!!)
adapter?.notifyDataSetChanged()
}catch (e: Exception){
Log.v("error", e.message.toString())}
}
})
return liveData
}
}
ACTIVITY
Here is my logic code
class RecyclerActivity : AppCompatActivity() {
private lateinit var viewModel: RecyclerActivityViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_recycler)
viewModel = ViewModelProvider(this).get(RecyclerActivityViewModel::class.java)
}
}
this might help
viewModel= ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(this.getApplication())).get(RecyclerActivityViewModel::class.java)