我是 android 的菜鸟,我在调用另一个类的方法时遇到问题。从它自己的类中调用该方法时工作正常,但是当我从另一个类中调用它时,我得到一个空指针异常。任何帮助将不胜感激。这是我的代码;
从class2调用class1中的方法:
BottlesActivity inst = new BottlesActivity();
inst.call0();
第 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);
}
}
这是我的 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)