How do i resolve NullPointerException when calling

2019-08-05 05:35发布

问题:

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)

回答1:

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:

public class GlobalUtil
{
    public static void startUrlActivity(Context context, String url) {
    try {
        Intent callIntent = new Intent(Intent.ACTION_CALL);
        callIntent.setData(Uri.parse(url));
        context.startActivity(callIntent); //<-- pass current activity or context
        } catch (ActivityNotFoundException activityException) {
        Log.e("dialing-example", "Call failed", activityException);
        }
    }
}

And inside any other activity class you can call it:

GlobalUtil.startUrlActivity(this, "tel:1234567890"/*+phonenumber0*/");

OR

GlobalUtil.startUrlActivity(getBaseContext().getApplicationContext(), "your_url");


回答2:

This will work if call0() is a method of the same activity class.

public void call0() {
    try {
        Intent callIntent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:1234567890"));
        startActivity(callIntent); 
    } catch (ActivityNotFoundException activityException) {
        Log.e("dialing-example", "Call failed", activityException);
    }
}

And dont forget to add this in your manifest file:

<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>

And I dont think this a good thing:

BottlesActivity  inst = new BottlesActivity();
inst.call0();

Instead when the button is clicked, call call0() like below:

public void onClick(View v) {
     switch(v.getId()){
          case R.id.idButton:
                call0();
                break;

      }
}


回答3:

You can't create an Activity yourself. You could use a static method in another Activity class.

from any Activity:

BottlesActivity.call0(this);

in BottlesActivity

public static void call0(Context context) {
    try {
        Intent callIntent = new Intent(Intent.ACTION_CALL);
        callIntent.setData(Uri.parse("tel:1234567890"/*+phonenumber0*/));
        context.startActivity(callIntent); //<--This line causes nullpointerexception
    } catch (ActivityNotFoundException activityException) {
        Log.e("dialing-example", "Call failed", activityException);
    }
}