如何调用方法时,我解决NullPointerException异常? [英] How do i resolve NullPointerException when calling methods?

查看:289
本文介绍了如何调用方法时,我解决NullPointerException异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个菜鸟到Android和我有打电话从其他类中的方法的问题。该方法效果优良工程从自己的类中调用的时候,但我得到一个NullPointerException异常,当我从另一个类调用它。任何帮助将大大AP preciated。这里是我的code;

从Class2中调用方法class1的:

  BottlesActivity研究所=新BottlesActivity();
inst.call0();

在1类方法:

 公共无效call0(){
    尝试{
        意图callIntent =新意图(Intent.ACTION_CALL);
        callIntent.setData(Uri.parse(电话:1234567890/ * + * phonenumber0 /));
        startActivity(callIntent); //< - 这行导致NullPointerException异常
    }赶上(ActivityNotFoundException activityException){
        Log.e(拨号-榜样,呼叫失败,activityException);
    }
}

下面是我的logcat

  20 09-06:51:33.237:E / AndroidRuntime(18192):显示java.lang.NullPointerException
09-06 20:51:33.237:E / AndroidRuntime(18192):在android.app.Activity.startActivityForResult(Activity.java:2893)
09-06 20:51:33.237:E / AndroidRuntime(18192):在android.app.Activity.startActivity(Activity.java:3003)
09-06 20:51:33.237:E / AndroidRuntime(18192):在com.bryanjrichardson.GSCC.BottlesActivity.call0(GoldandSilverCoinCalculatorActivity.java:857)
09-06 20:51:33.237:E / AndroidRuntime(18192):在com.bryanjrichardson.GSCC.SimpleItemizedOverlay $ 1.onClick(SimpleItemizedOverlay.java:204)
09-06 20:51:33.237:E / AndroidRuntime(18192):在com.android.internal.app.AlertController $ ButtonHandler.handleMessage(AlertController.java:174)
09-06 20:51:33.237:E / AndroidRuntime(18192):在android.os.Handler.dispatchMessage(Handler.java:99)
09-06 20:51:33.237:E / AndroidRuntime(18192):在android.os.Looper.loop(Looper.java:130)
09-06 20:51:33.237:E / AndroidRuntime(18192):在android.app.ActivityThread.main(ActivityThread.java:3859)
09-06 20:51:33.237:E / AndroidRuntime(18192):在java.lang.reflect.Method.invokeNative(本机方法)
09-06 20:51:33.237:E / AndroidRuntime(18192):在java.lang.reflect.Method.invoke(Method.java:507)
09-06 20:51:33.237:E / AndroidRuntime(18192):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
09-06 20:51:33.237:E / AndroidRuntime(18192):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-06 20:51:33.237:E / AndroidRuntime(18192):在dalvik.system.NativeStart.main(本机方法)


解决方案

如果您的2类 2分开活动,你的情况会导致空指针异常,因为当第2个活动正在运行,第一个活动停止(终止) - >这是Android操作系统的机制,不能更改

对于你的情况的解决方案:


  • (1)活动1是别人的父母,这种情况下是适用于应用类或TabHost(复杂)。


  • (2)创建像静态方法的 @ Zapl的答案;但它仍然不是相当不错的,因为这种静态的方法应该是在共同使用,而不是里面的1级 - >修改:创建像下面的一个共同的类别:


这是工具类:

 公共类GlobalUtil
{
    公共静态无效startUrlActivity(上下文的背景下,字符串URL){
    尝试{
        意图callIntent =新意图(Intent.ACTION_CALL);
        callIntent.setData(Uri.parse(URL));
        context.startActivity(callIntent); //< - 通过当前活动或上下文
        }赶上(ActivityNotFoundException activityException){
        Log.e(拨号-榜样,呼叫失败,activityException);
        }
    }
}

和任何其他活动类中可以称之为:

  GlobalUtil.startUrlActivity(这一点,电话:1234567890/ * + * phonenumber0 /);

  GlobalUtil.startUrlActivity(getBaseContext()getApplicationContext(),YOUR_URL);

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:

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");

这篇关于如何调用方法时,我解决NullPointerException异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆