I'm a noob to android and i am having an issue calling a method from another class. The method works works fine when called from within its own class, but i get a nullpointerexception when i call it from another class. Any help would be greatly appreciated. Here is my code;
Calling method in class1 from class2:
BottlesActivity inst = new BottlesActivity();
inst.call0();
Method in class 1:
public void call0() {
try {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:1234567890"/*+phonenumber0*/));
startActivity(callIntent); //<--This line causes nullpointerexception
} catch (ActivityNotFoundException activityException) {
Log.e("dialing-example", "Call failed", activityException);
}
}
Here is my Logcat
09-06 20:51:33.237: E/AndroidRuntime(18192): java.lang.NullPointerException
09-06 20:51:33.237: E/AndroidRuntime(18192): at android.app.Activity.startActivityForResult(Activity.java:2893)
09-06 20:51:33.237: E/AndroidRuntime(18192): at android.app.Activity.startActivity(Activity.java:3003)
09-06 20:51:33.237: E/AndroidRuntime(18192): at com.bryanjrichardson.GSCC.BottlesActivity.call0(GoldandSilverCoinCalculatorActivity.java:857)
09-06 20:51:33.237: E/AndroidRuntime(18192): at com.bryanjrichardson.GSCC.SimpleItemizedOverlay$1.onClick(SimpleItemizedOverlay.java:204)
09-06 20:51:33.237: E/AndroidRuntime(18192): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:174)
09-06 20:51:33.237: E/AndroidRuntime(18192): at android.os.Handler.dispatchMessage(Handler.java:99)
09-06 20:51:33.237: E/AndroidRuntime(18192): at android.os.Looper.loop(Looper.java:130)
09-06 20:51:33.237: E/AndroidRuntime(18192): at android.app.ActivityThread.main(ActivityThread.java:3859)
09-06 20:51:33.237: E/AndroidRuntime(18192): at java.lang.reflect.Method.invokeNative(Native Method)
09-06 20:51:33.237: E/AndroidRuntime(18192): at java.lang.reflect.Method.invoke(Method.java:507)
09-06 20:51:33.237: E/AndroidRuntime(18192): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-06 20:51:33.237: E/AndroidRuntime(18192): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-06 20:51:33.237: E/AndroidRuntime(18192): at dalvik.system.NativeStart.main(Native Method)
If your 2 classes are 2 separated Activities, your case will cause Null Pointer Exception, because when 2nd activity is running, 1st activity is stopped (terminated) -> this is Android OS mechanism, you can not change.
Solutions for your case:
(1) Activity 1 is parent of others, this case is suitable for Application class or TabHost (complicated).
(2) Create static method like @Zapl's answer; But it's still not quite nice because this static method should be in common use instead of inside Class 1 -> modify: Create a common Class like following:
This is utility class:
And inside any other activity class you can call it:
OR
You can't create an
Activity
yourself. You could use astatic
method in another Activity class.from any Activity:
in BottlesActivity
This will work if call0() is a method of the same activity class.
And dont forget to add this in your manifest file:
And I dont think this a good thing:
Instead when the button is clicked, call call0() like below: