I have two different class(class A and Class B). i want to use the method of Class A in Class B. i normally used object for class A and called method in class B. but unfortunately i am getting Force close error. Is that any thing different to call a method of another class in android. I referred many articles in stackoverflow. but i cant understand properly. pls help me to find out the solution.
You should not create object like this, you should use context to call object like as below
((Class A) contextObject).function();
it runs perfectly on my system,
earlier class A and class B both are extending Activity and now only A extends Activity and B extends A and now B can call functions of A
this works for me:-
public class A extends Activity
{
functionOfA(){}
}
public class B extends A
{
//calling function of class A
functionOfA();
}
In case of Android, class which extends Activity will maintain its life cycle methods. if method which is defined in different class other current running activity may be killed or in pause state. so it is suggested that if method which is reusable in application should in different class for example (AppManager singleton class) rather than being in single activity class
u have to create constructor of class A
& in class B make an obj to class A
initialize it with
ClassA obj=new classA();
obj.method_A();
hope this will help
As I said in my comment, you shouldnt instantiate activities.
If your method uses some method that are called from a Context object, you can create a new class ( Class NewClass) that accepts a context parameter and implements your methods in it.
So this way, you can call your class from any activity:
NewClass nc = new NewClass(this);
Look up for some example of how to use a database in Android. It uses the same way.
Related
I want to call a method that is in my MainActivity class and I don't know how to do it if I want to call it in another class. How can I do it?
you can call it using the main activity context this might help : Calling method of another class using contexts
Unable to access the methods from one class to another class in android.
Actually, what I am trying is
ClassA.java
class ClassA extends Activity
{
method_1();
}
ClassB.java
class ClassB extends BroadCastReciever
{
// I need to access the method_1 from the class ClassA.
}
How can I do this?
First Solution
Declare your method1() as follows -
public static void method_1()
{
}
Then you can access it from ClassB as follows -
ClassA.method1();
Second Solution
Create an object of ClassA and access method1() from ClassB as follows-
ClassA classA = new ClassA();
classA.method1();
EDIT:
If both the classes are in the same package then you can use protected instead of public in the First Solution.
inside ClassA declare public method from ClassB create object of ClassA and access method using object
write below code inside ClassB
Like
ClassA classa = new ClassA();
classa.method_1();
Note :
if possible don't create class as static if it is not necessary.
provide class modifier to public.
mark method_1(); as public and static
Eg:
class ClassA extends Activity
{
public void static method_1();
}
Then you should be able to access method_1(); without creating a object of class A.
i guess you cant access a method from a class that extends an activity in android , however you can add an inner or helper class to the activity class like this example :
Call a public method in the Activity class from another class?
there is two way to access it
1) you need to create method as public static method() so that you can access it anywhere by calling class name dot method name like Aaa.method();
2)or you need a object to call method like passing Aaa's object to broadcast class and call method on it.
EDIT:
if you are passing context from activity A to brodcast class then you can convert context into activity object to call its public methods.
like:
in brodcast class:
((yourActivityName)context).method();
Basically you can call any method from another class, as long as it's public. If the method is static, you don't even need to instantiate it. But read some tutorials to get the basic idea of OOP instead of just looking for a solution for your specific problem, it'll help a lot more!
You should never be able to to that ... How do you know if the activity is still alive? The best way to send messages from BroadcastReceivers to Activities (where you register a custom receiver in onCreate/onresume and unregister in onDestroy/onPause) is to send another broadcast message from BroadcastReceiver.
But you could catch the initial broadcast already in activity. Consider using OrderedBroadcastReceivers.
I have a class which extends Application and i want to call it from code, it have
#Override
public void onCreate()
I need to call this from an Activity. I know how to call it when app starts for that i need to include in manifest:
android:name=""
Thanks.
You should avoid Calling Applications onCreate manually, as it will get started automatically if anything is configured correctly. However, If you want to call Methods from your overridden Application you can do it like this:
public class MyApplication extends Application{
public void someMethod(){}
}
then inside any Activity:
MyApplication app = (MyApplication)getApplication()
app.someMethod();
Try this :
public class YourApplication extends Application
{
public void sayHello {
System.out.println("Hello")
}
}
Then call it in any activity by:
YourApplication appState = ((YourApplication)this.getApplication());
appState.sayHello();
Application class onCreate() gets called when the Application starts. If you want to call a method that you have declared in your Application class you can call it like,
((Application_Class_Name)getApplicationContext()).calling_method();
From any other class that extends Activity, else you have to use context to get the instance of getApplicationContext() to call from Non Activity class.
Eg - If you want to call it from Adapter class you need to pass the context of the Activity to adapter class and get the instance of Application,
((Application_Class_Name)mContext.getApplicationContext()).calling_method();
From Activity you simply call ((YourAppName)getApplicationContext()).
And also, you don't need onCreate() in your Application (unless you know you do). You can set some methods there and then call them with e.g. ((YourAppName)getApplicationContext()).myMethod(). Your app is alive as long as any of its activities is.
You should't call onCreate() method by yourself... Android does it for you... The main purpose of such a class is to keep global variable commom to whole application, since Application itself is a single instance...
And it lets you override onCreate() because you may need your Custom things in an Appliucation/ACtivity that the OS does create..
I am making an app in which i have to run an activity in background until the app is running.each activity related to this app is using first activity.how can it possible?
can i use the inheritance for this?
can anyone tell me any example of multilevel interitance in android?
You can create a BaseActivity class that extends Activity and all other Activities will extend this BaseActivity. Then what ever happened in all other activities (like resume and pause) will also effect the actions of BaseActivity.
If you have to accomplish background task you better to see android service
You are already extending the Activity class in a base class, and again you are extending this base class in other class. This is itself an example of multilevel inheritance. I am posting an example that may be relevant for your question:
public class basecls extends Activity{
/*The base class*/
}
public class secondcls extends basecls{
/* basecls extended by secondcls */
}
You can extend the secondcls in another class, and the new class will inherit all super classes such that you can use the methods of its super classes.
Your main activity is already using inheritance, since it extends the Activity class.
Colleagues, I have the such question:
1. In my first class I have the
public class parseYouTubeAndYahoo extends AsyncTask<String, Void, List<VideoDataDescription>>
to parse data from internet. But I need to call execute() method of this class from another class. While trying to right such code:
new MainActivity.parseYouTubeAndYahoo().execute("someURL");
I have the next error message from Eclipse
No enclosing instance of type MainActivity is accessible. Must qualify the allocation with an enclosing instance of type MainActivity (e.g. x.new A() where x is an instance of MainActivity).
and really this problem is shrouded in fog for me. So, how to call this method from another class?
In terms of the actual error here, if parseYouTubeAndYahoo class is a non-static inner class inside of your Activity, then you need an instance of the enclosing class in order to instantiate the inner class. So, you'll need:
MainActivity myActivity = new MainActivity();
MainActivity.parseYouTubeAndYahoo asyncTask = myActivity.new parseYouTubeAndYahoo();
However....
You really shouldn't be instantiating non-static inner classes of your Activities from outside of the Activity because in order to instantiate a non-static innner class, you have to actually instantiate the enclosing class, which, in this case, is the Activity. Activities are meant to be started, not instantiated via new. If you have an AsyncTask that you'd like to use in different places, then create a new top-level class that extends from AsyncTask.
(For an example of creating reusable AsyncTasks, see: https://github.com/levinotik/ReusableAsyncTask)
Note that the syntax you've tried to use WOULD work if you needed to grab a static nested class. This is because in such a case, the outer class is really just acting as a namespace, but the nested class, because its static, does not actually need a reference to an instance of the outer class. Thus:
OuterClass.StaticNestedClass nestedObject =
new OuterClass.StaticNestedClass();
is the proper syntax for getting an instance of a static nested class.
I guess your parseYouTubeAndYahoo class is an inner class in class MainActivity, in Java, you should instaniate an object of the inner class by new MainActivity().new parseYouTubeAndYahoo(), so call that method like this new MainActivity().new parseYouTubeAndYahoo().execute("someURL");
I also guess MainActivity extends the Activity class, so I think the answer should be this.new parseYouTubeAndYahoo().execute("someURL"); when you just call this method inside your MainActivity class.
It would be easier just to place AsuncTask into another file as a separete class.
But if you really want to have is an inner class, then either it has to be static or you need to obtain a reference to its parent class first, which could be done like this:
in onCreate of MainActivity:
static MainActivity activityInstance = getContext();
New method in MainActivity:
public static MainActivity getActivityInstance(){
return activityInstance;
}
Then in another activity you can get the instance and access its public methods
MainActivity instanceOfMainActivity = MainActivity.getInstance();
Then
new instanceOfMainActivity.parseYouTubeAndYahoo().execute("someURL");
Reference the context of activity to other class and use it.
Like that: public oneofconstructer(Context ctx, .....)