飞机切换按钮?
我正在尝试使用 ToggleButton 来打开和关闭飞行模式。我不知道该怎么做。
我的权限是:
<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
请帮忙。
-谢谢。
I am trying to use a ToggleButton to switch AirPlane mode on and off. I am not sure how to go about this.
My permissions are:
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>
My .XML file looks like this:
<?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>
My Java has this for the toggle button:
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);
}
};
});
}
UPDATE:
This is my log cat errors if that helps.
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
Please help.
-Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请参阅问题所附评论。未分配变量“context”,导致空引用异常。设置飞行模式设置背后的逻辑没有任何问题。
See comments attached to question. The variable 'context' was not assigned, resulting in a Null Reference Exception. There isn't anything wrong with the logic behind setting the Air Plane mode setting.