I am using android studio 8.0. and I made a simple activity to go to another activity here are these errors are shown
You don't need to call onCreate() explicitely. Once you declare an instance of a class, if it has the onCreate() class, it will be automatically called at the appropiate time.
If you override that class, always be sure to declare the #Override statement above it, this will make sure you're overriding the correct function because if you don't, and you don't specify the correct parameters that the former method has, it will not be called and you'll think it does.
Also, it's a good idea to call super.onCreate(savedInstance) as the first line of your overriden method.
---- EDIT ----
As said above, onCreate() is called on the object creation, i.e., when you declare: alertFunction alertClass = new alertFunction();.
If this is something you want to call several times, put the content in a public function inside your class and call it from onCreate() and from the outside when needed. For instance:
public void myFunction() {
// Put here the current code of onCreate()
...
}
#Override
public void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
myFunction();
}
And then if you need to call it again from outside your class, do the following:
alertClass.myFunction();
Source: ERROR: The method onCreate(Bundle) in the type alertFunction is not applicable for the arguments ()
By the way, there is no Android Studio 8.0. Check out this link: https://developer.android.com/studio/releases/index.html
Hope this helps! Good luck!
Related
I am new to both Android and Java (as well as OOP), so forgive me if this is super noobish. In going through the Android tutorial on a first app, the following code is written:
#Override
protected void onCreate(Bundle savedInstanceState){...}
I have read that #Override is overriding the default onCreate call and that, if you don't override, you can't specify which activity to use. That's all well and good, but in the interest of understanding, what exactly is the default activity if activity_main is not in the case that you don't override onCreate?
I think what you are asking is what does the base class onCreate method do, and what happens if you don't override it. You can look at the source code for Activity here, or the AppCompatActivity here to look at what is in the base class. Notice that a typical implementation in an app looks like
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// other custom setup code for your activity
}
That first call to super.onCreate still calls the base class onCreate method (the one you are overriding), so you're not replacing what's in the base class, but adding your customizations specific to the Activity you are creating. That call to super does a number of Activity setup things that you always have to do. It doesn't associate the activity with any layout file (which is done by the setContentView call), so it wouldn't display anything from your xml files without you adding the setContentView call. There is no default xml layout file it would use.
I believe you mean the default Activity layout?
If you don't set one in onCreate with setContentView I think you simply won't have one.
in my app, there are 5 classes and 1 activity. This activity has, as you all know, an OnDestroy() method. In this method I need to remove a test provider which is set up in another class called "mockingclass".
In "mockingclass" I have a method similar to this:
public void mocker()
{
xxx
location.RemoveTestProvider(location.GpsProvider);
xxx
}
While xxx stands for many other functions in this method, when the app is being destroyed I need to call ONLY for that ONE function in within this whole method.
Is there any way to do that at all? If not, what would be your suggestions?
THANKS!
Obviously you can't choose a single line from method to be executed. You need to extract it separate method which will be called separately. Maybe you should look at some injection? Let this call be injected as an delegate for example. In that way you could manipulate what should be called depending on situation.
I have created an Android Application Project and in MainActivity.java > onCreate() it is calling super.onCreate(savedInstanceState).
As a beginner, can anyone explain what is the purpose of the above line?
Every Activity you make is started through a sequence of method calls. onCreate() is the first of these calls.
Each and every one of your Activities extends android.app.Activity either directly or by subclassing another subclass of Activity.
In Java, when you inherit from a class, you can override its methods to run your own code in them. A very common example of this is the overriding of the toString() method when extending java.lang.Object.
When we override a method, we have the option of completely replacing the method in our class, or of extending the existing parent class' method. By calling super.onCreate(savedInstanceState);, you tell the Dalvik VM to run your code in addition to the existing code in the onCreate() of the parent class. If you leave out this line, then only your code is run. The existing code is ignored completely.
However, you must include this super call in your method, because if you don't then the onCreate() code in Activity is never run, and your app will run into all sorts of problem like having no Context assigned to the Activity (though you'll hit a SuperNotCalledException before you have a chance to figure out that you have no context).
In short, Android's own classes can be incredibly complex. The code in the framework classes handles stuff like UI drawing, house cleaning and maintaining the Activity and application lifecycles. super calls allow developers to run this complex code behind the scenes, while still providing a good level of abstraction for our own apps.
*Derived class onCreate(bundle) method must call superclass implementation of this method. It will throw an exception SuperNotCalledException if the "super" keyword is not used.
For inheritance in Java, to override the superclass method and also to execute the above class method, use super.methodname() in the overriding derived class method;
Android class works in the same way. By extending the Activity class which have onCreate(Bundle bundle) method in which meaningful code is written and to execute that code in the defined activity, use the super keyword with the method onCreate() like super.onCreate(bundle).
This is a code written in Activity class onCreate() method and Android Dev team might add some more meaningful code to this method later. So, in order to reflect the additions, you are supposed to call the super.onCreate() in your Activity class.
protected void onCreate(Bundle savedInstanceState) {
mVisibleFromClient = mWindow.getWindowStyle().getBoolean(
com.android.internal.R.styleable.Window_windowNoDisplay, true);
mCalled = true;
}
boolean mVisibleFromClient = true;
/**
* Controls whether this activity main window is visible. This is intended
* only for the special case of an activity that is not going to show a
* UI itself, but can't just finish prior to onResume() because it needs
* to wait for a service binding or such. Setting this to false prevents the UI from being shown during that time.
*
* <p>The default value for this is taken from the
* {#link android.R.attr#windowNoDisplay} attribute of the activity's theme.
*/
It also maintains the variable mCalled which means you have called the super.onCreate(savedBundleInstance) in your Activity.
final void performStart() {
mCalled = false;
mInstrumentation.callActivityOnStart(this);
if (!mCalled) {
throw new SuperNotCalledException(
"Activity " + mComponent.toShortString() +
" did not call through to super.onStart()");
}
}
See source code here.
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/1.5_r4/android/app/Activity.java#Activity.onCreate%28android.os.Bundle%29
Because upon super.onCreate() it will reach the Activity (parent class of any activity) class to load the savedInstanceState,and we normaly don't set any saved instance state, but
android framework made such a way that, we should be calling that.
It is information you want returned to your application, via onCreate(),
if the activity is destroyed and restarted due to some implicit reason
(e.g., not because the user pressed the back button). The most common
use of onSaveInstanceState() is to handle screen rotations, as by
default, activities are destroyed and recreated when the user slides out
the G1 keyboard.
The reason to call super.onCreate(savedInstanceState) is because your
code will not compile otherwise. ;-)
I am fairly new in android and Java and this question might seem a little bit a naive question ,
I am trying to write a method called getDispalay() , this will be a public with no retuen.
I will print a message saying I am in the getDisplay method .
In java it is very simple , It will be called in main method , But I do not know how i can use main method in activity class ? I mean how can I write a method and call it as I need it ?
Thanks
Go through this article
As you said in java we call the function in main method .
In android if java class extends activity you can call the method in Any of the life cycle event of activity , It depends on your need . For example if you want to call method when you enter into activity follow this
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main)//To set up ui for activity
getDispalay();
}
public void getDispalay(){
//your actions
}
What you really need to do is understand the activity lifecycle:
Once you have read through that then you will know how your Android app runs and what states and stages it transitions through.
You should read the documentation about Activity Lifecycle.
The onCreate() method would be comparable to main().
There are two classes. MainActivity, in which i set the view, and ClassX from which i want to update a view in MainActivity. ClassX is an AsyncTask called from MainActivity, if that's relevant.
What i want to do is to change the text of a view called mainTextLog. I've declared a global TextView variable, and in the onCreate() method i set it to the view using findViewById().
private TextView logger;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
logger = (TextView) findViewById(R.id.mainTextLog);
}
By now i should be able to change the text from onCreate(), and i can. But since i want to change it from another class (ClassX) i need to create a method for it:
public void setLogText(String text) {
logger.setText(text);
}
But it doesn't work. I've tried making logger and the setLogText() method static, but it still doesn't work. The app just crashes.
It's probably pretty easy, but i'm out of ideas.
If you are using an AsyncTask you need to set the value in either onProgressUpdate or in onPostExecute.
You really should read the documentation for AsyncTasks
You CANNOT update the UI from the doInBackground method as it is not run in the UI thread and will give you an exception.
Also, you should post the exception you are getting when the application crashes so we have a better idea what the problem is. But I'd guess you are trying to update the text from the wrong thread.
I've done this plenty in the app im working on, its sort of an MDI type app on the android tablet.
To do what you're asking....
in MainActivity have
public static void setText(String txt){
((TextView)findViewById(R.id.mainTextLog)).setText(txt);
}
then in the child (or calling class) call it like...
MainActivity.setText("myTextToShow");
That's it... im using android api level 12... If i remember correcty it worked in api level 7 as well though.
Hope this helps...
One possibility is that: when you call setLogText in another Class X. The MainActivity may not be existing anymore, which makes the logger a null reference?