0

我正在尝试使用 ToggleButton 打开和关闭 AirPlane 模式。我不知道该怎么做。

我的权限是:

<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>

我的 .XML 文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView  
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/hello"
/>
<ToggleButton  
android:id="@+id/Toggle" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"
android:textOn="On"
android:textOff="Off">
</ToggleButton>
</LinearLayout>

我的 Java 有这个用于切换按钮:

AirToggle = (ToggleButton) findViewById(R.id.Toggle);
    AirToggle.setOnClickListener(new OnClickListener(){
        public void onClick(View v){

        if (((ToggleButton)v).isChecked()){
            boolean isEnabled = Settings.System.getInt(context.getContentResolver(),Settings.System.AIRPLANE_MODE_ON, 0) == 1;
            if(isEnabled == false)
            {
            Settings.System.putInt(context.getContentResolver(),Settings.System.AIRPLANE_MODE_ON,1);                                Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
            intent.putExtra("state", 1);
            context.sendBroadcast(intent);
            }

            IntentFilter intentFilter = new IntentFilter("android.intent.action.SERVICE_STATE");

            BroadcastReceiver receiver = new BroadcastReceiver() {
                  @Override
                  public void onReceive(Context context, Intent intent) {
                        Log.d("AirplaneMode", "Service state changed");
                  }
            };

            context.registerReceiver(receiver, intentFilter);


            }

    };
});

}

更新:如果有帮助,这是我的日志猫错误。

04-19 18:43:24.264: ERROR/AndroidRuntime(233): ERROR: thread attach failed

04-19 18:43:25.933: ERROR/AndroidRuntime(241): ERROR: thread attach failed

04-19 18:43:31.543: ERROR/AndroidRuntime(248): Uncaught handler: thread main exiting due         to uncaught exception

04-19 18:43:31.553: ERROR/AndroidRuntime(248): java.lang.NullPointerException

04-19 18:43:31.553: ERROR/AndroidRuntime(248):     at      com.simple.armor.SimpleArmor$1.onClick(SimpleArmor.java:30)

04-19 18:43:31.553: ERROR/AndroidRuntime(248):     at android.view.View.performClick(View.java:2364)

04-19 18:43:31.553: ERROR/AndroidRuntime(248):     at android.widget.CompoundButton.performClick(CompoundButton.java:98)

04-19 18:43:31.553: ERROR/AndroidRuntime(248):     at android.view.View.onTouchEvent(View.java:4179)

04-19 18:43:31.553: ERROR/AndroidRuntime(248):     at android.widget.TextView.onTouchEvent(TextView.java:6541)

04-19 18:43:31.553: ERROR/AndroidRuntime(248):     at android.view.View.dispatchTouchEvent(View.java:3709)

04-19 18:43:31.553: ERROR/AndroidRuntime(248):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)

04-19 18:43:31.553: ERROR/AndroidRuntime(248):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)

04-19 18:43:31.553: ERROR/AndroidRuntime(248):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)

04-19 18:43:31.553: ERROR/AndroidRuntime(248):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)

04-19 18:43:31.553: ERROR/AndroidRuntime(248):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)

04-19 18:43:31.553: ERROR/AndroidRuntime(248):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)

04-19 18:43:31.553: ERROR/AndroidRuntime(248):     at android.app.Activity.dispatchTouchEvent(Activity.java:2061)

04-19 18:43:31.553: ERROR/AndroidRuntime(248):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)

04-19 18:43:31.553: ERROR/AndroidRuntime(248):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)

04-19 18:43:31.553: ERROR/AndroidRuntime(248):     at android.os.Handler.dispatchMessage(Handler.java:99)

04-19 18:43:31.553: ERROR/AndroidRuntime(248):     at android.os.Looper.loop(Looper.java:123)

04-19 18:43:31.553: ERROR/AndroidRuntime(248):     at android.app.ActivityThread.main(ActivityThread.java:4363)

04-19 18:43:31.553: ERROR/AndroidRuntime(248):     at java.lang.reflect.Method.invokeNative(Native Method)

04-19 18:43:31.553: ERROR/AndroidRuntime(248):     at java.lang.reflect.Method.invoke(Method.java:521)

04-19 18:43:31.553: ERROR/AndroidRuntime(248):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)

04-19 18:43:31.553: ERROR/AndroidRuntime(248):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

04-19 18:43:31.553: ERROR/AndroidRuntime(248):     at dalvik.system.NativeStart.main(Native Method)

04-19 18:43:31.573: ERROR/dalvikvm(248): Unable to open stack trace file '/data/anr/traces.txt': Permission denied

请帮忙。

-谢谢。

4

1 回答 1

0

请参阅问题所附的评论。未分配变量“上下文”,导致空引用异常。设置飞行模式设置背后的逻辑没有任何问题。

于 2011-04-20T11:56:38.017 回答