【问题标题】:Get NullPointerException when calling getSystemService()调用 getSystemService() 时获取 NullPointerException
【发布时间】:2015-10-20 07:30:40
【问题描述】:

我的问题是,在我的 MainActivity 的 onCreate() 方法中,我正在创建新的 Thread 对象,我想向该对象传递对 this 活动的引用,然后在该线程中使用它来调用 getSystemService()。但最后,当我启动应用程序时它崩溃了,我得到了 NullPointerException。

我已经发现问题可能是我在 super.onCreate() 之前传递了对活动的引用,但是在我的代码中 super.onCreate() 是在传递引用之前执行的。

这是我的 MainActivity 的 onCreate() 方法

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Instance which contains thread for obtaining wifi info
    final WifiInfoThread wifi_info = new WifiInfoThread(this);
....
}

这是 Thread 类,我试图在其中获取对系统服务的引用

public class WifiInfoThread extends Thread {
// Constructor for passing context to this class to be able to access xml resources
Activity activity;
WifiInfoThread(Activity current) {
    activity = current;
}

// Flag for stopping thread
boolean flag = false;
// Obtain service and WifiManager object
WifiManager current_wifi = (WifiManager) activity.getSystemService(Context.WIFI_SERVICE);

// Runnable object passed to UIThread
Runnable uirunnable = new Runnable() {
    @Override
    public void run() {
        // Get current wifi status
        WifiInfo wifi_info = current_wifi.getConnectionInfo();

        // Things with showing it on screen
        TextView tv_output = (TextView) activity.findViewById(R.id.tv_output);
        String info = "SSID: " + wifi_info.getSSID();
        info += "\nSpeed: " + wifi_info.getLinkSpeed() + " Mbps";
        tv_output.setText(info);
    }
};

public void run() {
    flag = true;

    for(; flag; ) {
        activity.runOnUiThread(uirunnable);
        try {
            this.sleep(500);
        }
        catch(InterruptedException e) {}
    }
}

}

【问题讨论】:

  • 尊敬的投票者,用户刚刚创建了一个帐户并提出了一个问题,不要急于投票。也许编辑或评论会更受欢迎。

标签: android android-activity nullpointerexception


【解决方案1】:

在初始化 activity 之前,您正在使用 activity.getSystemService。要解决这个问题,请在下面移动到 Constructor

// Obtain service and WifiManager object
WifiManager current_wifi = (WifiManager) activity.getSystemService(Context.WIFI_SERVICE);

喜欢

WifiManager current_wifi;
WifiInfoThread(Activity current) {
    activity = current;
    current_wifi = (WifiManager) activity.getSystemService(Context.WIFI_SERVICE);
}

【讨论】:

    【解决方案2】:

    将初始化current_wifi 移动到您的线程的Constructor 中。

    // Obtain service and WifiManager object
    WifiManager current_wifi = (WifiManager) activity.getSystemService(Context.WIFI_SERVICE);
    

    在您的情况下,activity 仍然是 null 引用。在构造函数中赋值后它会得到一个有效的

    【讨论】:

      【解决方案3】:

      其他答案向您展示了如何解决此问题。您还应该知道 whatNullPointerException 的原因:在 java 中,您的代码不会按照您编写的顺序执行。在成员函数(方法)之外编写的每一件事都会首先执行(有点)。然后调用构造函数。因此,您在activity 上调用Conetxt.getSystemService(),即null

      对于后台工作,android 有AsyncTaskIntentService。查找它们。

      【讨论】:

      • 谢谢你的解释,也谢谢你的建议,这些东西我找了好久
      • 欢迎来到stackoverflow!
      猜你喜欢
      • 1970-01-01
      • 2013-01-05
      • 2015-08-13
      • 1970-01-01
      • 2020-04-19
      • 2017-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多