Reputation: 233
I want to call a method from a android library class which i have imported as a androidlib.jar. As i am able to call a whole class of library but i dont want it, but i want to call a particular method of library class.
I tried something like this, but it is showing java.lang.Nullpointer exception
This is my library class (AndroidLiB.class), where i have imported its jar file
public class AndroidLiB extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.android_li_b);
startGPS();
}
public void startGPS()
{
Toast.makeText(getApplicationContext(),"Your GPS started",Toast.LENGTH_LONG).show();
}
}
This is my application class where i want to call a method from above class
public class AndLib1 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.and_lib1);
AndroidLiB abc = new AndroidLiB();
abc.startGPS();
}
}
But it is not working
Upvotes: 0
Views: 2316
Reputation: 651
If you want simply a method from jar, then why you need to extends Activity. My suggestion is remove extends Activity will fix the NPE error.
Try this,
public class AndroidLiB {
Activity activity;
AndroidLiB(Activity activity){
this.activity = activity;
}
public void startGPS()
{
Toast.makeText(activity,"Your GPS started",Toast.LENGTH_LONG).show();
}
}
And In your main class call like
AndroidLiB lib = new AndroidLiB (this);
lib.startGPS();
Upvotes: 1
Reputation: 358
You can extend library class. For example:
public class YourActivity extends AndroidLib
{
super.onCreate(savedInstanceState);
setContentView(R.layout.and_lib1);
startGPS();
}
Upvotes: 1
Reputation:
I would do something like this:
public class Tool {
private Tool() {
// no direct instantiation
}
public static void startGPS(final Context context) {
Toast.makeText(context, "Your GPS started", Toast.LENGTH_LONG).show();
}
}
then
public class AndroidLiB extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.android_li_b);
Tool.startGPS(this);
}
}
and
public class AndLib1 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.and_lib1);
Tool.startGPS(this);
}
}
Upvotes: 1
Reputation: 2159
What you're trying to do isn't really the best way to do things, but I'm assuming that the question being asked is how to import the jar correctly. If so:
If using eclipse,
androidlib.jar
file is in the libs folder.androidlib.jar
and select Build Path > Add to Build Path
Properties > Java Build Path > Order and Export
and then make sure androidlib.jar
is selected.The problem with your current code is that when you call getApplicationContext()
, the Activity
hasn't been started yet, therefore there is no context. A quick and dirty solution would be to rewrite the startGPS()
method like this:
public static void startGps(Context context, String message) {
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
}
But I would much rather put that method inside some sort of Utilities class or even inside a parent Activity
class.
Upvotes: 0