NoSuchMethodException when inherit abstract class in Activity - java

I'm facing this issue on my apps :
Caused by java.lang.NoSuchMethodException: <init> []
at java.lang.Class.getConstructor0(Class.java:2332)
at java.lang.Class.getConstructor(Class.java:1728)
at androidx.fragment.app.Fragment.a(Fragment.java:15)
at androidx.fragment.app.FragmentContainer.instantiate(FragmentContainer.java)
at com.example.DefAct.colorStatusBar(DefAct.java)
at com.example.MainActivity.initWorkers(MainActivity.java)
Which caused this error :
Caused by androidx.fragment.app.Fragment$e: Unable to instantiate fragment f.a.a.a.x.u.a: could not find Fragment constructor
at androidx.fragment.app.Fragment.a(Fragment.java:66)
at androidx.fragment.app.FragmentContainer.instantiate(FragmentContainer.java)
at androidx.fragment.app.FragmentManagerImpl$6.instantiate(FragmentManagerImpl.java:2)
at com.example.DefAct.colorStatusBar(DefAct.java)
at com.example.MainActivity.initWorkers(MainActivity.java)
So, I have an abstract class DefAct.kt :
abstract class DefAct: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
colorStatusBar()
}
private fun colorStatusBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.statusBarColor = ContextCompat.getColor(this, R.color.blue)
}
}
}
Then, I call this abstract class into my MainActivity.kt :
class MainActivity: DefAct(){
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
.....
}
Some ideas that I think might become a solution for my problem :
Changing the colorStatusBar() method on MyDefaultAct.kt into public
Create empty constructor
Things I've done :
Read documentation about the abstract class, fragment and its constructor
Trying to create a constructor but my Activity doesn't have any fragments except for only calling
dialog.show(supportFragmentManager, SomeTags.TAG)
I don't know exactly how to reproduce the test case that caused these logs. But when I read the log, the exception appeared right after calling the colorStatusBar(). Please kindly help me to solve this issue, I've been struggling for 4 hours. If I missed something with my code, please kindly tell me. Thank you

Related

Android registerOnSharedPreferenceChangelistener only triggered a few times after that not working

Hello so I have the problem that I registered a onSharedPreferenceChangeListener in my MainActivity. The only preference I have is a ListPreference with 3 different options. So at the start of the program it still gets triggered the first - 3 times mostly, sometimes it doesn't even trigger at the beginning. I don't think that's how it is supposed to work so my code is down below if more is needed just write a comment of a specific part.
// (from MainActivity)
this.sharedPreferences.registerOnSharedPreferenceChangeListener { sharedPreferences: SharedPreferences, s: String ->
var value = sharedPreferences.getString("location", "")
controller.setLocation(value, this)
}
class SettingsActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.settings_activity)
supportFragmentManager
.beginTransaction()
.replace(R.id.settings, SettingsFragment())
.commit()
supportActionBar?.setDisplayHomeAsUpEnabled(true)
}
class SettingsFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.root_preferences, rootKey)
}
}
}
Found the solution by myself, for everyone who is having an issue like that. It's probably the scope. The listener gets lost when you are switching between activities.
How to fix: put the listener block as a global variable and just register it in your "onCreate" method.

Accidental override: The following declarations have the same JVM signature Android Studio 3.1 Kotlin

Hello I'm getting error when I want to Run App :
error :
C:\Users...\AndroidStudioProjects\projects\HelloWorld\app\src\main\java\training\androidkotlin\helloworld\MainActivity.kt: (8, 5): Accidental override: The following declarations have the same JVM signature (onCreate(Landroid/os/Bundle;)V):
fun onCreate(savedInstanceState: Bundle): Unit defined in training.androidkotlin.helloworld.MainActivity
fun onCreate(p0: Bundle?): Unit defined in training.androidkotlin.helloworld.MainActivity
Code :
package training.androidkotlin.helloworld
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
#Override
protected fun onCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
How to solve it please ?
Thanks
In Kotlin #Override is a keyword, so change #Override fun xx to override fun xx and it will fix the issue.
Thank you so much for all you proposition, I fixed the problem.
1. I try to reinstall Android Studion in case if there was sone failure during the installation and I change my code like this :
package training.androidkotlin.helloworld
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
#Override
protected override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
One more time thank you for every one who took his time to give me their answers.
Cheers !

Passing parameters at runtime dagger 2

I have a module, UserModule that takes in a string and provides a user object.
UserModule:
#Module
class UserModule(val name: String) {
#Provides
fun provideUser() : User = User(name = name)
}
And a ViewModelComponent that has UserModule as one of its components
ViewModelComponent:
#Singleton
#Component(modules = [UserModule::class])
interface ViewModelComponent {
fun inject(activity: MainActivity)
}
Normally I would provide the component in my application like this:
class MainApplication : Application() {
lateinit var component: AppComponent
override fun onCreate() {
super.onCreate()
component = DaggerAppComponent.builder()
.userModule(UserModule("Name"))
.build()
}
}
And reference it in my activity like this:
class MainActivity : AppCompatActivity() {
#Inject lateinit var user: User
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
(application as MainApplication).component.inject(this)
}
However, this assumes that the value of UserModule name is known at runtime and is hardcoded into MainApplication, however, in reality, the value of name is obtained from MainActivity.
The only solution I can think of is to build the dependency graph in MainActivity so I am able to pass name like this:
class MainActivity : AppCompatActivity() {
#Inject lateinit var user: User
val newUserName = "NewName"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
component = DaggerAppComponent.builder()
.userModule(UserModule(newUserName))
.build()
component.inject(this)
}
This solution seems very unefficient but it is the only way I can inject dynamic parameters in Dagger 2. Is there a better way to achieve this
I just ran into this issue aswell, first solution that I thought of was to create some "static holder" class that would hold dynamic parameters and it would be developers responsibility to update this holder at the right time - before injection happens I guess. I didnt actually try it though and it doesnt seem to be too clean either.
Anway after a bit of googling I found this article: https://proandroiddev.com/dagger-2-module-parameters-7c820d944a which seems to be more "dagger oriented" solution, didnt try that either, but it's something you can start with.
I would only comment this but I dont have required reputation...

How to set error label with TextInputLayout in the viewmodel with kotlin

I need to do some input validation for my TextInputEditText that is wrapped with TextInputLayout.
I'd like errors to appear below the line if the input is done in wrong format.
All the logic is done in the viewmodel instead of the view(fragment or activity). But I can't seem to access the view through viewmodel, for instance:
textinputlayout.setError("error") doesn't work in the viewmodel
and layout.findViewbyId(layoutId) doesn't work in the viewmodel either.
Any idea?
used below code to set error in TextInputLayout..
class SpinerActivity :AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.spiner)
setEdittext()
}
private fun setEdittext() {
var textError:TextInputLayout=findViewById(R.id.amEt1)
textError.error="Please Enter Name"
}
}

registerReceiver() shows error for overloaded method

I was trying Kotlin in my Android project. There is a broadcast receiver for network change events in my activity. Code is as below:
BaseActivity.kt
abstract class BaseActivity : AppCompatActivity() {
private val networkChangeReceiver = NetworkChangeReceiver()
override fun onStart() {
super.onStart()
registerReceiver(
receiver = networkChangeReceiver,
intentFilter = IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)
)
}
override fun onStop() {
super.onStop()
unregisterReceiver(receiver = networkChangeReceiver)
}
}
NetworkChangeReceiver.kt
class NetworkChangeReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
Log.e("network changed")
}
}
I don't see any problem in it. But my kotlin plugin shows me following error:
None of following functions can be called with arguments supplied
The arguments supplied for first one is correct ASAFIK. I am not an expert in Kotlin, just learning it for fun. Is this intended behaviour of Kotlin, error in plugin or am I missing something? Can anyone explain?
First the parameter is named filter and not intentFilter and if you correct this, you get another error. Which says: "Named arguments are not allowed for non-Kotlin functions" Reason for this is that the method public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) is from Android and written in java. Removing the names should work fine:
registerReceiver(
networkChangeReceiver,
IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)
)

Categories