user3011821
user3011821

Reputation:

android calling method from library project force closed

Hi I have an android library project A. Its Main Activity contains method abc(String).When I am trying to call this method from other project after integrating project A,It force closed. I used the code

 MainActivity a=new MainActivity();
 a.abc("");

Am I do the right way to call a method from library project activity from other project?? please help me I am new to android,thanks in advance.

Upvotes: 0

Views: 407

Answers (1)

Raghunandan
Raghunandan

Reputation: 133560

You should not instantiate a Activity class. Activity has a lifecycle and you should declare the same in manifest and use startActivity(intent).

Check Declaring library components in the manifest file

http://developer.android.com/tools/projects/projects-eclipse.html

Once declared you can start the Activity.

Can i Create the object of a activity in other class?

Quoting Raghav Sood

By treating an Activity as a normal Java class, you end up with a null context. As most methods in an Activity are called on its Context, you will get a null pointer exception, which is why your app crashes.

Instead, move all such methods which need to be called from other classes into a Utility class which accepts a valid context in its constructor, and then use that context in the methods to do the work.

Upvotes: 3

Related Questions