Kotlin not recognized in Android Studio - android

I am writing an app and just redo it from a youtube video to get back into app writing.
All this Kotlin is new to me as it didn't exist last time I programmed some apps.
When creating the new App I chose Kotlin instead of java.
Now I want to create a Constants class with a companion:
public class Constants {
companion object {
const BASE_URL = "https://whatever.com"
}
}
But too many errors are shown:Shown errors
companion: cannot resolve
object: missing ;
const: unexpected token
BASE_URL: cannot resolve
string: missing ;
Furthermore, I have created a new class which should be a data class, but it doesn't recognize it as well:
data class
I did install the KOTLIN plugins but it didn't help.

Related

Room-based code is causing the following error: android.app.Application cannot be cast to com.example.mathreps.MathRepsApplication

I am working on adding data persistence to an app using the Room library, based on this documentation. My database only has one column where a String corresponding to a user-selected radio button is to be placed. However, as soon as I attempt to insert a new object (in this case, an Attempt), it gives me the following error:
java.lang.ClassCastException: android.app.Application cannot be cast to com.example.mathreps.MathRepsApplication.
I believe this error comes from the following block of code within my Rating fragment:
private val dataViewModel: MathRepsViewModel by activityViewModels {
MathRepsViewModel.MathRepsViewModelFactory(
(activity?.application as MathRepsApplication).database
.attemptDao()
)
}
Which relates to a different class called MathRepsApplication
class MathRepsApplication: Application() {
val database: AttemptRoomDatabase by lazy {AttemptRoomDatabase.getDatabase(this)}
}
Since this problem most likely relates to multiple classes and packages, here is the link to the repository for the project.
My attempts to fix this have been unsuccessful, with them mostly consisting of heavily reviewing the database implementation and not much else, as I don't exactly know where to start.

How to rename realm object in iOS

I am using realm in our iOS and Android app. For some reason i want to rename one of my realm object.
Initially we name the object Demo and now I want to change it to RealmDemo
In android we achieved it by using #RealmClass annotation
#RealmClass(name = "Demo")
open class RealmDemo : RealmObject() {
}
On iOS side i am not sure how exactly i can do similar as i did in android.
class RealmDemo: Object {
override static func className() -> String {
"Demo"
}
}
I tried above ^ but getting following error "Terminating app due to uncaught exception 'RLMException', reason: 'Object type 'Demo' not managed by the Realm'"
Two things.
First, You can name an object anything you want and change its name at any time.
HOWEVER, that's a destructive change, and Realm doesn't have any way to know the the newly named object 'is the same object' as the prior object.
How that's handled depends on what the use case is:
If this is a development situation, delete your local Realm files and run the app and the object with the new name will be created automatically.
If this is production then a migration block is needed (as on any platform) to migrate the data from the old object to the new one.
Secondly, The other important thing is the name of the object is now RealmDemo, whereas the prior object is Demo
class RealmDemo: Object {
so technically those are two separate objects. To Realm, you've abandoned the Demo object totally and that's a destructive change. Demo is still hanging around but is not referenced in your code so an error is thrown
On a possibly unrelated note, the className function references Demo
override static func className() -> String {
"Demo"
}
But the object name is RealmDemo.
It's not clear why the className function exists but it's not required or really needed. See the documentation for objects to get a feel for their structure - they may need a Primary Key
Seems like realm does not support className overriding for cocoa/ios.
https://github.com/realm/realm-cocoa/issues/2194
https://github.com/realm/realm-cocoa/issues/6624

Combining `by lazy` and `object` results in compiler-error "cannot find symbol"

I cannot compile anymore after the update to Kotlin 1.3.0 (works in 1.2.71) when trying to use by lazy and object. This seems to happen only on my project. A demo-project is working fine.
I want to add an interface to a given class and lazy-load its values.
I've created a small example which is not working in my project but working fine in any other:
open class Foo
interface Bar {
val lazyLoadedString : String
}
class Test {
private val foo by lazy {
object : Foo(), Bar {
override val lazyLoadedString = "Demo"
}
}
}
As soon as I combine object and by lazy, it cannot compile anymore and shows the following error. Using each one alone works.
Test.java:9: error: cannot find symbol
private final my.package.Test$foo$2$1 getFoo()
symbol: class Test$foo$2$1
location: package my.package
When looking closer, you'll see that the generated java file shows this error and not the kotlin-code.
Any ideas on this?
It looks like kapt is broken in Kotlin 1.3.0 for this particular kind of code.
In the code above, it was the annotation processor registered by Realm that triggered it, but any other annotation processor would have resulted in the same error.
The issue is being tracked here: https://youtrack.jetbrains.net/issue/KT-28053

MVVM with multiple projects Xamarin

Hellow , i am a student and i am working on a project. It is a Xamarin native project (cross-platform). I Use MVVM. This was working perfect when i was using 1 shared project and my 3other projects (android,iOS,winphone). Now I have different projects for my shared code.
-Appname.Repositories
-Appname.Services
-Appname.Viewmodel
Now the problem is that when i start my app using an constructor in my viewmodel with an interface he is given me the next error :
//Constructor
public LoginViewModel(IMvxMessenger messenger, IUserDataService userDataService) : base(messenger)
{
_userDataService = userDataService;
}
Error:
MvvmCross.Platform.Exceptions.MvxException: Failed to construct and initialize ViewModel for type C_Tracker.Mobile.Domain.ViewModel.LoginViewModel from locator MvxDefaultViewModelLocator - check InnerException for more information
InnerException :
MvvmCross.Platform.Exceptions.MvxIoCResolveException: Failed to resolve parameter for parameter userDataService of type IUserDataService
But when i use a constructor without this interface it is working.
public LoginViewModel(IMvxMessenger messenger) : base(messenger)
{
}
Please help me.
Probably you forgot about Dependencies Injection.
Add to Setup.cs something like this Mvx.RegisterType<IUserDataService, UserDataService>();
For more information about ServiceLocator please check https://github.com/MvvmCross/MvvmCross/wiki/service-location-and-inversion-of-control

invalid 'this' to a Java object in Nativescript Plugin

I am trying to develop a nativescript plugin to perform some functionality with the Azure SDK. The docs for the SDK show below:
So I have added the following function to my plugin:
MobileServiceUser.newUser = function (userId, client) {
var userObject = com.microsoft.windowsazure.mobileservices.authentication.MobileServiceUser(userId); // causing the error
client.setCurrentUser(userObject);
};
UserId is a string.
However, the top line throws the error:
JS: Error: Trying to link invalid 'this' to a Java object
I have a full repo showing the minimal implimentation to create this issue on Github.
I would be really grateful of any suggestions how to fix this.
This answer is a little late but I recently ran into this issue myself. Hopefully this answer helps someone in the future!
The error is a bit plain but if you take a look at the code source, you'll see that it's actually telling you that you cannot link a variable to the Object/Class itself as a reference:
MobileServiceUser.newUser = function (userId, client) {
var userObject = com.microsoft.windowsazure.mobileservices.authentication.MobileServiceUser(userId);
};
When you want to instantiate a class, you need to use the keyword new to signify this action. Without new listed before the class constructor call, the program just sees that you're making a direct link between userObject and the class itself. This should fix your problem:
var userObject = new com.microsoft.windowsazure.mobileservices.authentication.MobileServiceUser(userId);
Cheers
~Px

Categories

Resources