I'm admittedly new to Scala and Android programming and in all my searching I haven't been able to find answer to help me understand and resolve my problem.
Here's a gist of my two scala classes https://gist.github.com/Daikamar/f15288a7bf732cd5b55c
I'm running through the tutorial found here: http://developer.android.com/training/basics/firstapp/starting-activity.html
which I'm trying to adapt to scala code (I have need for understanding Scala for work and a personal desire to mess around with Android development so I figured I'd try and combine these efforts).
The problem is seen in DisplayMessageActivity.scala in which the IDE reports that it cannot resolve MyActivity in this line:
val message = intent.getStringExtra(MyActivity.ExtraMessage)
I feel like this should work. I can get it to resolve if I change MyActivity to an object, but then that breaks other pieces of the application that expects MyActivity to be a class.
An help in getting me to understand my problem would be appreciated.
You cannot reference the ExtraMessage field from MyActivity as though it was a 'static' field (in Java terminology). To access ExtraMessage in your other activity you will need to either obtain an instance of MyActivity that you can then de-reference, or add a companion object (defined using the object keyword in the same file in which the class is defined) for MyActivity and define the field there:
object MyActivity {
val ExtraMessage = "net.daikamar.myfirstapp.MESSAGE"
// any other 'static' declarations
}
class MyActivity() extends ...
then your call as above will work.
Related
I making project in Kotlin with MVP design pattern first time. Why I have got java.lang.OutOfMemoryError.
This is my error from debuger: https://pastebin.com/U1nwjaf6
This is my class DbManager: https://pastebin.com/KRpa2eRF
And this is my class LoginPresenter: https://pastebin.com/Vryf0kBH
I am beginner on making apps using Kotlin.
Please help me.
Welcome to StackOverflow!
Some tips:
You should post everything related to your code. It makes easier for people to help you and give better answers. You should also paste code in the question itself instead of giving links to pastebin. Only use pastebin like services when your code is too big. Which is not the case here (but you did right with the error message, it is big).
Now, to the problem itself.
You seem to be instantianting the class DbManager, according to the error messages. This class instantiates your other class, LoginPresenter here:
class DbManager
{
...
val loginPresenter: LoginPresenter = LoginPresenter()
...
}
LoginPresenter will instantiate the class DbManager:
class LoginPresenter
{
...
val dbManager: DbManager = DbManager()
...
}
DbManager creates LoginPresenter which creates DbManager which creates LoginPresenter which creates...
See the pattern here? It creates a infinite loop, with recursion, that will create classes until you run out of memory. Hence, the error.
It is hard to tell you what you should do when I only have this part of the code. But, I think that you should instantiate the DbManager class only once, globally and use that instance everytime you want to interact with it.
You are getting this because of StackOverflow :)
DBManager & LoginPresenter instances are getting created in a loop and causing this, you should move instance creation into some method
So I'm trying to follow the simple read.me for Kotshi and get it set up for my project but I seem to be hitting a little snag.
I'm currently at this portion of the read.me
#KotshiJsonAdapterFactory
object ApplicationJsonAdapterFactory: KotshiApplicationJsonAdapterFactory()
but KotshiApplicationJsonAdapterFactory seems to give me an unresolved reference error. Now this sounds like an absolutely silly question but is KotshiApplicationJsonAdapterFactory supposed to be a custom class that I set up? If so I don't see anywhere in the documentation regarding it. My gradle has the two dependencies added so I'm absolutely baffled.
#KotshiJsonAdapterFactory makes Kotshi generate a JsonAdapter factory.
Should be placed on an abstract class that implements
JsonAdapter.Factory.
So yes, KotshiApplicationJsonAdapterFactory is a custom class that you've to setup and which fulfills the above condition.
KotshiJsonAdapterFactory is an annotation that you apply to a class that you write. This will make Kotshi generate a class with the same name as your class + the Kotshi prefix. That class will implement the actual factory.
When you write your class it will look like you have a compile error, but it will not prevent compilation.
I am new to android. If my question is wrong please forgive me,
My Question is:
Can I write a method in android which can be accessed from anywhere inside my application?
I've studied VB for all these years and now I am trying to program in android, I couldn't stop comparing them when I write code.
In VB we can create modules and access it from anywhere. Is there anything I can do in Android...??
Answers and advises are needed!
You have to create a class with a static method:
public class MyClass {
public static void myMethod() {
// Your code here...
}
}
And you can call it like this: MyClass.myMethod();
You can try extending Application and put your common functions there.
In all activities you can access this via context.
For reference follow this:
http://www.devahead.com/blog/2011/06/extending-the-android-application-class-and-dealing-with-singleton/
It is a long topic to discuss , but as you have simply ask whether method is accessible or not , then answer is yes but with some respect of java rules.
Android is again like java coding, You can do same thing what we can do with java.
Same Data type
Method Format
Class structure
Inheritance, Public , private , protected, etc.
So while you write your code you should care for that all things and these all thing you know because you are working in VB.
VB module equivalent is not there in java(android),
but some how you can mock them to some extend by using final classes with static methods.
Anyway you need to import the package containing these type of classes wherever you use it.
hey, does anyone have experience in developing Android app with Java reflection feature?
I am stuck with a peculiar problem , looking for help.
I have a Activity and a common class residing in the same package of my Activity,
let's assume they are com.mypkg.MyActivity and com.mypkg.MyClass
my code in MyActivity:
Class clazz = MyClass.class;
Constructor[] constructors1 = clazz.getDeclaredConstructors();
Class cls = java.lang.Object.class;
Constructor[] constructors2 = cls.getDeclaredConstructors();
...
okay, now my problem:
the code runs fine on simulator,
while on real device, constructors of those pre-built classes, such as java.lang.Object or android.widget.FrameLayout, can be retrieved correctly, but constructors of MyClass is always empty(i.e. constructors1.length is always zero)
I am very confused, any help?
This may be something you've tried, but are you sure your class (MyClass) is declared correctly in your AndroidManifest.xml? Perhaps your emulator is more lenient than an actual device.
What's the difference between file, class and activity in android?
File - It is a block of arbitrary information, or resource for storing information. It can be of any type
Class - Its a compiled form of .Java file . Android finally used this .class files to produce an executable apk
Activity - An activity is the equivalent of a Frame/Window in GUI toolkits. It is not a file or a file type it is just a class that can be extended in Android for loading UI elements on view
Class -
A class is a combination of methods, variables and data types. Every Java or Android project must have at least one class.
Example:
public class shape{
public void circle()
{
int A,B,radias;
}
}
Activity -
An Activity is an android class. If we want to use an activity class, we must use extend Activity in your android project.
Example:
public class shape extends Activity{
public void circle()
{
int A,B,radias;
}
}
File is a file on the filesystem. Class is a Java class. Activity is a specific java class commonly used in Android.
1) Class is Blueprint of object and you will create as many object you want from same class. You can create new object by “new” keyword. In example below “ArrayList” is class and “obj” is object.
ArrayList<String> obj=new ArrayList<String>
2) Activity :- Every program has some starting point. In android Activity is starting point of any application you made. It is basically a GUI of the app. In android app every activity have to inherent directly or indirectly from Activity Class which is predefined in the android system. So activity is also a class but a special one. So you can say that “Every activity is a class but every class is not Activity”.
3) File :- file is used to store data so you can reuse again when you app start.
An activity is actually a class (click) and if you want to make your own activity you choose this one as parent class.
And the source code of classes is defined in files, actually every class should be described in its own file.
That's some basic knowledge of object oriented programming - you might want to have a look here to find more information