我希望我的应用程序在按钮点击时打开WiFi设置,但是当我这样做时,它强制关闭。这是我的代码:
public class Settings extends Activity {
private static Intent ACTION_WIFI_SETTINGS;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.settings, menu);
return true;
}
public void click(View view) {
Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
startActivity(intent);
}
}
XML布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Settings" >
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="14dp" >
</RadioGroup>
<RadioButton
android:id="@+id/radio0"
android:onClick="togglewifi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:checked="true"
android:text="Toggle Wifi" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/radio0"
android:text="Toggle Data" />
<Button
android:onClick="click"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/radio1"
android:layout_below="@+id/radio1"
android:layout_marginTop="16dp"
android:text="Configure" />
<Button
android:id="@+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Done" />
</RelativeLayout>
logcat的:
12-22 12:29:36.429: E/AndroidRuntime(2813): FATAL EXCEPTION: main
12-22 12:29:36.429: E/AndroidRuntime(2813): java.lang.IllegalStateException: Could not execute method of the activity
12-22 12:29:36.429: E/AndroidRuntime(2813): at android.view.View$1.onClick(View.java:2144)
12-22 12:29:36.429: E/AndroidRuntime(2813): at android.view.View.performClick(View.java:2485)
12-22 12:29:36.429: E/AndroidRuntime(2813): at android.view.View$PerformClick.run(View.java:9080)
12-22 12:29:36.429: E/AndroidRuntime(2813): at android.os.Handler.handleCallback(Handler.java:587)
12-22 12:29:36.429: E/AndroidRuntime(2813): at android.os.Handler.dispatchMessage(Handler.java:92)
我该如何解决这个问题?
答案 0 :(得分:1)
您确实意识到您将Intent操作设置为null。当你执行你的点击方法时,它当然会得到一个NPE。解决了这个问题。
答案 1 :(得分:0)
在您的XML中,您在onClick
RadioButton
上声明radio0
回调:
android:onClick="togglewifi"
但您的Activity
没有方法public void togglewifi(View)
,导致异常:
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:2144)