I'm integrating stripe in android java. Can anyone help me sample stripe code in java.
As I'm new to kotlin.
Using default stripe classfor add card "PaymentMethodsActivityStarter"
I'm unable to get response in onActivityResult
You must look at the code carefully, analyze it and then proceed. If you concentrate, it will become simple for you in no time. Let's go through an example:
class MyApp : Application() {
We know that this is a class, and its name is MyApp. A quick Google search would tell you that the : here just means that the class is extending whatever's on the right of it. Therefore the corresponding Java code should be
public class MyApp extends Application {
Now we go to the next line
override fun onCreate() {
States that an override tag is involved - a method is being overridden. fun in Kotlin means the following should be a function name. Once again, a Google search away. We see that it has no parameters, and no returns. Now you can make the Java code
#Override
public void onCreate() {
Now we move on
super.onCreate()
Which is calling the superclass method, same in Java.
PaymentConfiguration.init(applicationContext,"pk_test_TYooMQauvdEDq54NiTphI7jx")
I'm not familiar with Kotlin, but I can see that some form of init is being called. Parameters are a Context of some sort and a String. A Google search told me that init is basically calling the consturctor of a class. Therefore I can immediately try out
new PaymentConfiguration(getApplicationContext(), "pk_test_TYooMQauvdEDq54NiTphI7jx");
If that doesn't work, then Google more to understand what the code means instead of searching for a specific Java equivalent. If you still have problems, then post on here again with examples and some code and include the solutions you have tried and offer thoughts on what you think the issue could be. This is how we become better programmers that can adapt to and overcome problems.
Related
method does not override method from superclass. how can i fix this error.
im making and android native app for my website.and my website contain a button which amy app defining as unknown url error so i add the code (shared in screenshote) but after that is showing a error and its not working, im doing making this app without any pre knowledge of java or android app development please help me out to solve this issue . everything is working fine instead of whatsapp url
With this code, you're saying that there is a method shouldOverrideUrlLoading in your superclass.
Superclass is the class you extend. Example:
public class MainActivity extends ExampleClass { }
To override a method, there needs to be a
protected void shouldOverrideUrlLoading(){ }
method in ExampleClass.
If you didn't override any class, remove #Override.
If you did and must extend a class, you can't go further without inheritance knowledge.
MainActivity and this superclass not has shouldOverrideUrlLoading method
Android Studio Intermediate here with what I hope is not a redundant question:
I encountered an issue while building a splash screen for the very first time. I'm following the process posted here by David Medenjak, which encourages to use a theme in order to have it be displayed while the app is in the process of booting up:
https://blog.davidmedenjak.com/android/2017/09/02/splash-screens.html
I followed this very closely and am certain everything should be in order.
The very last step for basic functionality, titled "Registering the Splash Screen", suggests building a java file to handle switching to your application once it finishes loading. The java code provided on the blog implements an abstract class called Application.ActivityLifecycleCallbacks, yet the code provided does not implement the obstract methods of this superclass.
I did some research on how to implement this superclass but I'm not familiar with it enough to know how to do so and retain the functionality I need. My java class looks just like the one described in the blog.
Here is another guide on this superclass I found to help with implementing, which implements very few methods:
https://medium.com/#mobile_develop/android-application-development-detecting-when-your-app-enters-the-background-or-foreground-bbced47ad8a5
I apologize if the answer is obvious. My hope is to communicate with David, since he's active on StackOverflow. Maybe #'ing him to this thread would work. Any tips on how to implement the class (if necessary) would be greatly appreciated.
For the abstract methods that are not implemented in the super class, simply implement them as empty method definitions. The Example you can refer to is the following from the blog author's included source.
In Java, the first method to be run is
public static void main(String[] args)
In android, the first method to be run is
protected void onCreate(Bundle savedInstanceState)
I was always wondering why does the first method in Java have to be static but in android doesn't? Would anyone please help me to get this thing clear in my mind?
These are two different scenarios. We have a general java language run by a java virtual machine. This jvm is designed to call a specific entry point for all applications. Thus this have to be as generic as possible.
Android runtime and many other like WARs, Applets, Servlets are "higher" apis. Thus they have all their specific entry points. Since these apis are already running within general jvm with an own main() method, they are able to implement more complex and domain specific entry points. Thus in this case the android runtime creates an object of a given class and invokes the methods designed by the api.
See also:
Java Web Application specify entry point
Entry point for Java applications: main(), init(), or run()?
And many other resource.
Because Android studio is based on a framework.
If you take a look at the callstack when breaking on 'onCreate', you can see that a static main() method is called & creates an instance of your MainActivity class.
http://picpaste.com/stack-FOnB69o6.png
I have a method inside of a class called
ChopraWisdom.GetQuote()
that pulls in some data from the interwebs. To use it I have to use an AsyncTask in my activity. Thats fine and all, but my Activity code now gets cluttered with the AsyncTask code.
I was hoping that I could hide the AsyncTask in my Class and then create another method called
ChopraWisdom.GetQuoteAsync()
But I'm not sure how to pass in "Actions" (I come from a .Net background) in order to handle the various UI updating that needs to take place.
In C# I would define the method signature as something like:
ChopraWisdom.GetQuoteAsync(Action preExecute, Action postExecute, Action updateProgress)
Questions:
Does java have something comparable to 'Action'?
What is the acceptable pattern for 'wrapping' Async functionality like this in Java?
I like clean code, but am I being to picky here?
Please provide examples.
EDIT - Added Example class
public class ChopraWisdom
{
public string GetQuote()
{
//...do stuff and return a string
}
}
You should really think about using Loaders instead of AsynkTask(with android support lib).
If you still want to use AsyncTask in your situation best way would be to create new interface Action(something like https://github.com/ReactiveX/RxJava/blob/1.x/src/main/java/rx/functions/Action0.java)
You could use RxJava in your project and use all they have https://github.com/ReactiveX/RxJava/tree/1.x/src/main/java/rx/functions
You could use https://github.com/evant/gradle-retrolambda in combination with (2) option to provide C# like lambdas in your java code.
Java does have something comparable to Action. It is called Function and only available in Java 8. The standard way for passing a function as parameter is to create an interface and provide it as a parameter. That way you can either pass in an instance of a class implementing that interface or create an anonymous class inline. You encounter the latter everywhere in Android (OnClickListener, etc ...)
I would highly recommend you to take a look at Android Annotations. It provides useful features like:
Dependency injection
View injection
OnClickListener via annotation
AsyncTask via annotation
...
And the best thing: everything is done at compile time through subclassing, therefore there is no performance impact and you can check what the framework is doing at any given point.
You are not too picky at all. Clean code is very important in Android development as you have to write a lot of boilerplate- / gluecode anyway.
Here are some other handy android frameworks that are definitely worth checking out:
GreenDao
Eventbus
Recently I ran into a problem when trying to load an bitmap outside of my main class. I found that within my main class I could pass this as a Context to any given function allowing me to call getResources() from within that function. There are a couple of things about this which don't make sense to me.
The keyword "this" simply refers to the current object the function is running in, right? If so, how can I pass my main class as a Context by using "this"? It doesn't even have a Context in it. I am using "extends Activity", but Activity doesn't seem to contain any function called getResources() in it either.
I found a workaround which allows me to do what I want a bit easier which is to declare a public static Context appContext; within my main class. Then, within onCreate() I set appContext = this; Then, from elsewhere I can call MainActivity.appContext.getResources() whenever I need it. So, I really have a few questions here.
Why are MainActivity(my main class) and appContext not essentially the same thing when appContext is set to "this" from inside MainActivity (There is no such thing as MainActivity.getResources())
Is this unsafe to do? Could this cause any potential problems in my program?
Is there a way to load images without having to use getResources()?
What is the proper way to show my code in this website? The standard I am used to ([code][/code] tags) don't seem to work properly in the preview, so I am assuming it is handled differently here. The formatting help page says to simply use four spaces, but that doesn't seem to show any difference in the preview section either.
EDIT:
I just read in another thread somebody said
now everything depends on your main activity's onCreate method having been called.
That got me thinking. Under what circumstances would onCreate not be called? It seems like if it wasn't called the program would stop working properly in many ways not related to having a static variable initialized inside it(ie. setContentView would not be called).
So assuming that there is nothing wrong with doing it this way, the only drawback I can find, is that "MainActivity.appContext.getResources()" is a lot to type to call a function. Is there any way to create a sort of alias for this? I suppose I could create a function which simply calls that, but that just seems silly.
If you look here you see that activity is a subclass of Context.
You can format code with a backtic, or just use the {} buttons on the editor. See the markdown manual for more info.