检查移动数据状态时发生错误

时间:2015-02-19 06:16:03

标签: java android

我想在android中检查/关闭移动数据状态。 我在按钮点击代码中编写了以下代码..

@SuppressWarnings("rawtypes")
public void check_mobile_data(View view) { // this is my button clicking event.
    Context context = null;

    boolean mobileDataEnabled = false; // Assume disabled
    @SuppressWarnings("null")
    ConnectivityManager cm = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    try {

        Class cmClass = Class.forName(cm.getClass().getName());

        @SuppressWarnings("unchecked")
        Method method = cmClass.getDeclaredMethod("getMobileDataEnabled");
        method.setAccessible(true); // Make the method callable
        // get the setting for "mobile data"
        mobileDataEnabled = (Boolean) method.invoke(cm);
    } catch (Exception e) {
        e.printStackTrace();
        // Some problem accessible private API
        // TODO do whatever error handling you want here
    }

    if (mobileDataEnabled == true) {
        Toast.makeText(getApplicationContext(), "ON", Toast.LENGTH_LONG)
                .show();
    }
    if (mobileDataEnabled == false) {
        Toast.makeText(getApplicationContext(), "OFF", Toast.LENGTH_LONG)
                .show();
    }
}

logcat

显示以下错误
02-19 11:08:29.338: E/AndroidRuntime(14900): FATAL EXCEPTION: main
02-19 11:08:29.338: E/AndroidRuntime(14900): Process: com.example.checkmobiledata, PID: 14900
02-19 11:08:29.338: E/AndroidRuntime(14900): java.lang.IllegalStateException: Could not execute method of the activity
02-19 11:08:29.338: E/AndroidRuntime(14900):    at android.view.View$1.onClick(View.java:4012)
02-19 11:08:29.338: E/AndroidRuntime(14900):    at android.view.View.performClick(View.java:4761)
02-19 11:08:29.338: E/AndroidRuntime(14900):    at android.view.View$PerformClick.run(View.java:19767)
02-19 11:08:29.338: E/AndroidRuntime(14900):    at android.os.Handler.handleCallback(Handler.java:739)
02-19 11:08:29.338: E/AndroidRuntime(14900):    at android.os.Handler.dispatchMessage(Handler.java:95)
02-19 11:08:29.338: E/AndroidRuntime(14900):    at android.os.Looper.loop(Looper.java:135)
02-19 11:08:29.338: E/AndroidRuntime(14900):    at android.app.ActivityThread.main(ActivityThread.java:5312)
02-19 11:08:29.338: E/AndroidRuntime(14900):    at java.lang.reflect.Method.invoke(Native Method)
02-19 11:08:29.338: E/AndroidRuntime(14900):    at java.lang.reflect.Method.invoke(Method.java:372)
02-19 11:08:29.338: E/AndroidRuntime(14900):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
02-19 11:08:29.338: E/AndroidRuntime(14900):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
02-19 11:08:29.338: E/AndroidRuntime(14900): Caused by: java.lang.reflect.InvocationTargetException
02-19 11:08:29.338: E/AndroidRuntime(14900):    at java.lang.reflect.Method.invoke(Native Method)
02-19 11:08:29.338: E/AndroidRuntime(14900):    at java.lang.reflect.Method.invoke(Method.java:372)
02-19 11:08:29.338: E/AndroidRuntime(14900):    at android.view.View$1.onClick(View.java:4007)
02-19 11:08:29.338: E/AndroidRuntime(14900):    ... 10 more
02-19 11:08:29.338: E/AndroidRuntime(14900): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
02-19 11:08:29.338: E/AndroidRuntime(14900):    at com.example.checkmobiledata.MainActivity.check_mobile_data(MainActivity.java:28)
02-19 11:08:29.338: E/AndroidRuntime(14900):    ... 13 more

请尽量给我回复。

1 个答案:

答案 0 :(得分:2)

  

NullPointerException:尝试调用虚方法... Context.getSystemService ...

因为context对象是null。在使用context对象访问getSystemService方法之前不进行初始化。

使用view.getContext()获取上下文:

 Context context = view.getContext();