Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 检查移动数据状态时出错_Java_Android - Fatal编程技术网

Java 检查移动数据状态时出错

Java 检查移动数据状态时出错,java,android,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 @Supp

我想检查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
请给我最好的答复

NullPointerException:尝试调用虚拟方法。。。上下文。getSystemService

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

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

 Context context = view.getContext();

尝试在空对象引用上调用虚拟方法“java.lang.Object android.content.Context.getSystemService(java.lang.String)”
这说明了什么?添加android.permission.ACCESS\u NETWORK\u STATE permission亲爱的ρыσѕρєK,我已经初始化了上下文变量,但按下按钮时总是显示“关闭”当我为手机打开手机数据时network@cheelstar:您是否添加了android.permission.ACCESS_NETWORK_STATE权限?亲爱的ρцσѕρєK,它工作正常,我忘记添加权限了。谢谢你,祝你好运regards@cheelstar:欢迎:)@ρаσѕρєK u我在Listview适配器中遇到了相同的问题。您可以建议我如何修复它吗?请查看我的适配器